【问题标题】:getIntent() Extras always NULLgetIntent() Extras 总是 NULL
【发布时间】:2011-06-15 02:15:12
【问题描述】:

我编写了一个简单的 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 始终为 NULL!

我尝试过以下任意组合:

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

Bundle extra = new Bundle();
extra.putString(key, value);
notificationIntent.putExtra(extra);

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

【问题讨论】:

  • 您是否尝试在通知中设置应用程序上下文?

标签: android notifications android-intent


【解决方案1】:

这是场景:
getIntent() 方法返回第一个意图而不是启动活动。

因此,当活动关闭(终止)并且用户单击通知时,它将运行活动的新实例并且getIntent() 按预期工作(附加是 not @987654323 @)。

但如果 Activity 处于“睡眠”状态(它在后台)并且用户点击通知,getIntent() 总是返回启动 Activity 的第一个 Intent,而不是通知 Intent。

所以要在应用程序运行时捕获通知意图,只需使用这个

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

然后覆盖onNewIntent(Intent newintent)

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

【讨论】:

  • 以下对我有用:PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
  • awsme answer.. 我浪费了很多时间来调试这个问题。非常感谢。
  • 在我的情况下,在启动器活动中添加更多 android:launchMode="singleTask" 。谢谢!
【解决方案2】:

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

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

【讨论】:

  • 此解决方案在活动中为我工作,未在服务中测试。
  • 你是救生员!
  • 2 天后保存我的一天 :))))
【解决方案3】:

问题:您正在为待处理的 Intens 发送相同的请求代码。改变这个。

解决方案:设置全局变量 int UNIQUE_INT_PER_CALL =0 并在创建如下所示的 pendingIntent 调用时。

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

【讨论】:

  • 这对我有用。我用过这个int notificationId = new Random().nextInt();
  • 好的,这解决了我的问题,你能详细说明为什么这很重要吗?就我而言,我只发送一个意图。
【解决方案4】:

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

【讨论】:

  • 但是我的应用程序中没有“前一个”意图。这段代码只会被调用一次,之后不会创建或更新其他意图。
  • 所有活动都是由意图启动的,因此必须有一个先前的意图。哈,刚看到你自己的回答,就是这个意思!
  • 这就是为我解决的问题。这是设置该标志的位置:developer.android.com/reference/android/app/…
【解决方案5】:

查看Shared Preferences 以了解传递和检索持久键/值对。

【讨论】:

  • 我不需要将数据存储到首选项中。我将共享偏好用于其他目的。我需要通过 Intent 传递 ExtraData。
  • 实际上,这是唯一对我有用的答案。在我的例子中,意图是从 url 使用 activity:scheme 开始的,我尝试了一切,包括 savedInstanceState 没有运气。
猜你喜欢
  • 1970-01-01
  • 2017-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-16
  • 1970-01-01
相关资源
最近更新 更多