繁体   English   中英

Android服务通知onclick

[英]Android Service Notification onclick

我有一个前台服务,当用户单击通知时,我想使通知打开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?

如果要通过单击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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM