Android 服务通知 onclick
Android Service Notification onclick
我有一个前台服务,我想让通知在用户点击通知时打开 MainActivity。
在自定义服务class中创建通知的方法是:
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);
我有一个前台服务,我想让通知在用户点击通知时打开 MainActivity。
在自定义服务class中创建通知的方法是:
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);