【发布时间】:2013-01-02 12:31:21
【问题描述】:
我正在使用带有各种标签的Activity。从应用程序的不同部分创建通知以告诉用户某些事情发生了变化。当用户点击 Notification 时,我现在设法调用 Activity。但是我如何确定 Activity 是在运行时以“正常”方式创建还是通过单击通知创建?
(根据点击的通知,我想转发到另一个选项卡而不是显示主选项卡。)
Intent intent = new Intent(ctx, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 0, intent, 0);
// TODO: Replace with .Build() for api >= 16
Notification noti = new Notification.Builder(ctx)
.setContentTitle("Notification"
.setContentText(this.getName())
.setSmallIcon(R.drawable.icon)
.setContentIntent(pendingIntent)
.setDefaults(
Notification.DEFAULT_SOUND
| Notification.DEFAULT_LIGHTS)
.setAutoCancel(true)
.getNotification();
NotificationManager notificationManager = (NotificationManager) ctx
.getSystemService(Context.NOTIFICATION_SERVICE);
// Hide the notification after its selected
notificationManager.notify(this.getId(), noti);
这成功调用了我的 MainActivity。但是pendingIntent触发Activity时是否有一些方法被调用?
考虑在 Main Activity 中定义类似的东西:
onTriggeredByNotification(Notification noti){
//determinte tab, depending on Notification.
}
【问题讨论】:
标签: android notifications android-pendingintent