首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >getIntent()附加项始终为空

getIntent()附加项始终为空
EN

Stack Overflow用户
提问于 2011-06-15 10:15:13
回答 3查看 41.9K关注 0票数 77

我写了一个简单的Android应用程序,它显示了一个自定义通知,如下所示:

代码语言:javascript
复制
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事件。

代码语言:javascript
复制
public void onResume()
{
    super.onResume();
    // TODO: Extras è SEMPRE NULL!!! impossibile!
    Intent callingintent = getIntent(); 
    Bundle extras = callingintent.getExtras();

但是extras总是为空的!

我尝试过以下任何组合:

代码语言:javascript
复制
notificationIntent.putExtra("com.mysecure.lastpage", "SECURECODE");

代码语言:javascript
复制
Bundle extra = new Bundle();
extra.putString(key, value);
notificationIntent.putExtra(extra);

但是getIntent().getExtras()总是返回NULL。

EN

回答 3

Stack Overflow用户

发布于 2013-01-12 08:29:51

只需将此代码写在Resume()方法的上方即可。这就是它所需要的。这刷新了意图-我真的不知道,但它是有效的。

代码语言:javascript
复制
@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    setIntent(intent);
}
票数 99
EN

Stack Overflow用户

发布于 2011-06-15 10:41:31

由于您的活动似乎已经在运行,我认为您需要指定FLAG_UPDATE_CURRENT,否则getIntent()调用将返回前一个调用。参见this answer

票数 3
EN

Stack Overflow用户

发布于 2011-06-15 10:40:45

查看用于传递和检索持久键/值对的Shared Preferences

票数 -9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6352281

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档