【发布时间】:2014-12-29 01:10:15
【问题描述】:
对于此处回答的问题,我有一个后续问题:如何将参数从通知点击发送到活动?
所以,我有 3 个活动,A、B 和 C。当用户进入活动 C 时,警报(计时器)启动。X 时间后,状态栏中会出现通知。以下是我启动通知的方式。 “MainActivity.class”是活动 A。但是,以这种方式启动通知会在用户关闭应用程序时在任何活动中重新启动应用程序。因此,如果他们在应用关闭时处于活动 C 并单击通知,则应用将恢复到活动 C。
Intent notificationIntent = new Intent( this, MainActivity.class );
notificationIntent.setAction( Intent.ACTION_MAIN );
notificationIntent.addCategory( Intent.CATEGORY_LAUNCHER );
notificationIntent.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED );
notificationIntent.putExtra( NotifyService.NOTIFICATION_INTENT, true );
PendingIntent contentIntent = PendingIntent.getActivity( this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT );
Notification notification = new Notification.Builder( this )
.setContentTitle( title )
.setContentText( text )
.setTicker( ticker )
.setSmallIcon( R.drawable.ic_launcher )
.setWhen( time )
.setContentIntent( contentIntent )
.setAutoCancel( true )
.setDefaults( Notification.DEFAULT_ALL )
.build();
我在活动 A、B 和 C 的清单中有这个:android:launchMode="singleTop"。我在每个活动中也有以下代码。
@Override
public void onNewIntent( Intent intent )
{
super.onNewIntent( intent );
Bundle extras = intent.getExtras();
if( extras != null )
{
Boolean startedFromNotification = extras.getBoolean( NotifyService.NOTIFICATION_INTENT );
if( startedFromNotification )
{
// Do something
}
}
}
现在,如果用户在关闭应用程序时处于活动 A 中,那么当他们单击通知时会在该活动中调用 onNewIntent(按预期工作)。但是,如果他们在活动 B 或 C 中,不仅不会调用这些活动中的 onNewIntent,而且我放在那里的额外内容 (NotifyService.NOTIFICATION_INTENT) 也会丢失。
当应用程序恢复到活动 B 或 C 时,是否有将通知意图附加信息传递给应用程序?
【问题讨论】:
标签: android android-intent notifications bundle lifecycle