Android getIntent()附加始终为空

Android getIntent()附加始终为空,android,notifications,android-intent,Android,Notifications,Android Intent,我编写了一个简单的Android应用程序,显示如下自定义通知: Context context = getApplicationContext(); NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); Notification notification = new Notification( R.drawable.icon, title, Syst

我编写了一个简单的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();
但是附加值总是空的

我尝试过以下任何组合:

notificationIntent.putExtra("com.mysecure.lastpage", "SECURECODE");


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

查看如何传递和检索持久键/值对

由于您的活动似乎已经在运行,我认为您需要指定
标志\u UPDATE\u CURRENT
,否则
getIntent()
调用将返回上一个。请参阅。

这是一个场景:
方法
getIntent()
返回启动活动之前的第一个意图

因此,当活动关闭(终止)并且用户单击通知时,它将运行活动的一个新实例,
getIntent()
按预期工作(Extras不是
null

但是,如果活动处于“睡眠状态”(在后台),并且用户单击通知,
getIntent()
始终返回启动活动的第一个意图,而不是通知意图

因此,要在应用程序运行时捕获通知意图,只需使用

notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP)

然后重写onNewIntent(Intent newintent)


因此,当应用程序第一次运行时,
getIntent()
可以使用,当应用程序从睡眠状态恢复时,
onNewIntent
可以工作。

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

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    setIntent(intent);
}

问题:您正在为挂起的意图发送相同的请求代码。改变这个

解决方案:将全局变量int UNIQUE_int_PER_CALL设置为0,并在创建PendingEvent调用时,如下所示

PendingIntent contentIntent = PendingIntent.getActivity(context, UNIQUE_INT_PER_CALL, notificationIntent, 0);
UNIQUE_INT_PER_CALL++; // to increment.

您是否尝试在通知中设置应用程序上下文?我不需要将数据存储到首选项中。我将共享首选项用于其他目的。我需要通过Intent传递额外的数据。事实上,这是唯一对我有效的答案。在我的例子中,意图是使用activity:scheme从url开始的,我尝试了一切,包括
savedInstanceState
,但我的应用程序中没有“前一个”意图。这段代码只会被调用一次,之后不会创建或更新其他意图。所有活动都是由意图启动的,因此必须有先前的意图。哈,我刚刚看到了你自己的答案,这就是我的意思!这就是我解决问题的原因。下面是设置该标志的位置:以下内容对我有效:PendingEvent PendingEvent=PendingEvent.getActivity(上下文,0,意图,PendingEvent.flag_UPDATE_CURRENT);答案是。。我浪费了很多时间来调试这个问题。非常感谢。在我的例子中,在launcher活动中添加更多android:launchMode=“singleTask”。非常感谢。这对我有用。我使用了这个
int notificationId=new Random().nextInt()好的,这解决了我的问题,你能解释一下为什么这很重要吗?在我的情况下,我只发送一个意向。这个解决方案在活动中对我有效,而不是在服务中测试。你是一个救生员!2天后保存我的一天:))
PendingIntent contentIntent = PendingIntent.getActivity(context, UNIQUE_INT_PER_CALL, notificationIntent, 0);
UNIQUE_INT_PER_CALL++; // to increment.