由NotificationManager设置的意图不会反映在activity的onResume()中
我有一个广播接收器,在接收到某些内容后,它将创建一个挂起的意图,将其与一些数据打包,并使用它通过NotificationManager创建通知。当NotificationManager恢复活动时,挂起意图中指定的活动总是读出原始挂起意图的数据——当后续的onReceive()从广播接收器设置时,它永远不会读出任何新数据。这是广播接收器的片段:
public void onReceive( Context context, Intent intent ) {
String action = intent.getAction();
if( action.equals( "someaction" ) ) {
long now = System.currentTimeMillis();
int icon = R.drawable.icon;
CharSequence tickerText = context.getString( R.string.tickerText );
CharSequence contentTitle = context.getString( R.string.contentTitle );
CharSequence contentText = context.getString( R.string.contentText );
Intent notificationIntent = new Intent( context, MyActivity.class );
Log.d( TAG, "Creating notification with data = " + intent.getStringExtra( "somestring" ) );
notificationIntent.putExtra( "somestring", intent.getStringExtra( "somestring" ) );
notificationIntent.addFlags( Intent.FLAG_ACTIVITY_SINGLE_TOP );
notificationIntent.addFlags( Intent.FLAG_ACTIVITY_NEW_TASK );
PendingIntent contentIntent = PendingIntent.getActivity( context, 0, notificationIntent, 0 );
Notification notification = new Notification( icon, tickerText, now );
NotificationManager notificationmgr = (NotificationManager)context.getSystemService( Context.NOTIFICATION_SERVICE );
notification.flags |= Notification.FLAG_AUTO_CANCEL |
Notification.DEFAULT_SOUND |
Notification.DEFAULT_VIBRATE;
notification.setLatestEventInfo( context, contentTitle, contentText, contentIntent );
notificationmgr.cancelAll();
notificationmgr.notify( 0, notification );
}
}
这是活动的片段:
protected void onStart() {
super.onStart();
Intent intent = getIntent();
String somestring = intent.getStringExtra( "somestring" );
Log.d( TAG, "onStart(), somestring = " + somestring );
}
protected void onResume() {
super.onResume();
Intent intent = getIntent();
String somestring = intent.getStringExtra( "somestring" );
Log.d( TAG, "onResume(), somestring = " + somestring );
}
protected void onNewIntent( Intent intent ) {
Log.d( TAG, "onNewIntent(), intent = " + intent );
super.onNewIntent( intent );
setIntent( intent );
}
所以这是场景: 从主屏幕,我的接收器收到一些意图,例如将 somestring 设置为“hello world”。通知栏显示通知,我向下滑动通知栏,然后点击通知以启动(创建或恢复)活动。该活动正确读出“hello world”。我将活动保留在前台或使用主页键将其置于后台。第二次,我的广播接收器收到“hello Again”作为somestring。它再次创建通知,这次是“hello Again”,除了通过通知恢复活动时,我在调试中看到 getIntent() 和 onNewIntent() 都没有反映 的更新值em>somestring(即“你好”)。也就是说,它仍然保留着“hello world”的旧值。
有什么想法吗?我似乎找不到强制更新意图数据的方法。任何帮助表示赞赏;提前致谢。
I have a broadcast receiver that, upon receiving something, will create a pending intent, package it with some data, and use it to create a notification with NotificationManager. The activity that is specified in the pending intent, when NotificationManager resumes the activity, always reads out the original pending intent's data -- it never reads out any new data when set by subsequent onReceive()'s from the broadcast receiver. Here's a snippet from the broadcast receiver:
public void onReceive( Context context, Intent intent ) {
String action = intent.getAction();
if( action.equals( "someaction" ) ) {
long now = System.currentTimeMillis();
int icon = R.drawable.icon;
CharSequence tickerText = context.getString( R.string.tickerText );
CharSequence contentTitle = context.getString( R.string.contentTitle );
CharSequence contentText = context.getString( R.string.contentText );
Intent notificationIntent = new Intent( context, MyActivity.class );
Log.d( TAG, "Creating notification with data = " + intent.getStringExtra( "somestring" ) );
notificationIntent.putExtra( "somestring", intent.getStringExtra( "somestring" ) );
notificationIntent.addFlags( Intent.FLAG_ACTIVITY_SINGLE_TOP );
notificationIntent.addFlags( Intent.FLAG_ACTIVITY_NEW_TASK );
PendingIntent contentIntent = PendingIntent.getActivity( context, 0, notificationIntent, 0 );
Notification notification = new Notification( icon, tickerText, now );
NotificationManager notificationmgr = (NotificationManager)context.getSystemService( Context.NOTIFICATION_SERVICE );
notification.flags |= Notification.FLAG_AUTO_CANCEL |
Notification.DEFAULT_SOUND |
Notification.DEFAULT_VIBRATE;
notification.setLatestEventInfo( context, contentTitle, contentText, contentIntent );
notificationmgr.cancelAll();
notificationmgr.notify( 0, notification );
}
}
and here's that of the activity:
protected void onStart() {
super.onStart();
Intent intent = getIntent();
String somestring = intent.getStringExtra( "somestring" );
Log.d( TAG, "onStart(), somestring = " + somestring );
}
protected void onResume() {
super.onResume();
Intent intent = getIntent();
String somestring = intent.getStringExtra( "somestring" );
Log.d( TAG, "onResume(), somestring = " + somestring );
}
protected void onNewIntent( Intent intent ) {
Log.d( TAG, "onNewIntent(), intent = " + intent );
super.onNewIntent( intent );
setIntent( intent );
}
So here's the scenario:
From the home screen, my receiver receives some intent with, say, somestring set to "hello world". The notification bar shows the notification, I slide down the notification bar, and tap on the notification to launch (create or resume) the activity. The activity reads out "hello world" correctly. I leave the activity in the foreground or background it with the home key. The second time around my broadcast receiver receives, say, "hello again" as somestring. It again creates the notification, this time with "hello again", except when resuming the activity via the notification, I see in the debug that neither getIntent() nor onNewIntent() reflects the updated value of somestring (i.e. "hello again"). That is, it's still holding onto the old value of "hello world".
Any ideas? I can't seem to find a way to force an update of the intent's data. Any help is appreciated; thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
修好了。创建待处理意图时,应按如下方式传递
PendingIntent.FLAG_UPDATE_CURRENT
:Fixed it. When creating the pending intent, one should pass
PendingIntent.FLAG_UPDATE_CURRENT
as such:今天我为此苦苦挣扎了几个小时,如果不需要,您也可以将标志设置为 0!我真的不需要它,所以帮助我:)!希望它对其他人有帮助!
I struggled with this for a couple of hours today, you can ALSO set the flag to 0 if you don't need it! I don't really need it, so helped me :)! Hope it helps someone else!