【问题标题】:Android Service Notification onclickAndroid 服务通知 onclick
【发布时间】:2015-03-08 10:36:49
【问题描述】:

我有一个前台服务,我想让通知在用户点击通知时打开 MainActivity。

自定义Service类中创建Notification的方法是:

public void setForeground(final int notificationID, final int notificationIconID,
        final String notificationTickerText,
        final String notificationTitle, final String notificationText){
    Notification notification = new Notification(notificationIconID, notificationTickerText,
            System.currentTimeMillis());
    notification.setLatestEventInfo(this, notificationTitle,
            notificationText, PendingIntent.getActivity(this, 0, new Intent(this, this.getClass()), 0));
    startForeground(notificationID, notification);
}

如何修改它以包含打开(如果 MainActivity 当前已打开)或在通知点击功能上启动 MainActivity?

【问题讨论】:

    标签: java android android-activity android-service


    【解决方案1】:

    如果你想通过点击notification打开任意Activity,那么你需要实现TaskStackBuilder

    Intent resultIntent = new Intent(this, ResultActivity.class);
    
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addParentStack(ResultActivity.class);
    
    // Adds the Intent that starts the Activity to the top of the stack
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent( 0,
                                        PendingIntent.FLAG_UPDATE_CURRENT);
    
    mBuilder.setContentIntent(resultPendingIntent);
    

    【讨论】:

    • 非常感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多