我写了一个简单的Android应用程序,它显示了一个自定义通知,如下所示:
Context context = getApplicationContext();
NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification( R.drawable.icon, title, System.currentTimeMillis());
Intent notificationIntent = new Intent( context, this.getClass());
notificationIntent.putExtra("com.mysecure.lastpage", "SECURECODE");
PendingIntent pendingIntent = PendingIntent.getActivity( context , 0, notificationIntent, 0);
notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;
notification.contentView = new RemoteViews(context.getPackageName(), R.layout.notifypbar);
notification.contentIntent = pendingIntent;
notification.contentView.setTextViewText(R.id.notifypb_status_text, text);
notification.contentView.setProgressBar(R.id.notifypb_status_progress, 100, (int)(100*progress), false);
manager.notify(104, notification);
这段代码在我的应用程序中只被调用一次,它会显示一个带有进度条的通知(全部正确)。
现在,当用户单击此通知时,我的应用程序将处理onResume
事件。
public void onResume()
{
super.onResume();
// TODO: Extras è SEMPRE NULL!!! impossibile!
Intent callingintent = getIntent();
Bundle extras = callingintent.getExtras();
但是extras总是为空的!
我尝试过以下任何组合:
notificationIntent.putExtra("com.mysecure.lastpage", "SECURECODE");
或
Bundle extra = new Bundle();
extra.putString(key, value);
notificationIntent.putExtra(extra);
但是getIntent().getExtras()总是返回NULL。
发布于 2013-01-12 08:29:51
只需将此代码写在Resume()方法的上方即可。这就是它所需要的。这刷新了意图-我真的不知道,但它是有效的。
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
}
发布于 2011-06-15 10:41:31
由于您的活动似乎已经在运行,我认为您需要指定FLAG_UPDATE_CURRENT
,否则getIntent()
调用将返回前一个调用。参见this answer。
发布于 2011-06-15 10:40:45
查看用于传递和检索持久键/值对的Shared Preferences。
https://stackoverflow.com/questions/6352281
复制相似问题