【问题标题】:Android. Can't clear or replace extras安卓。无法清除或更换额外物品
【发布时间】:2013-12-24 20:05:06
【问题描述】:

通知中的我午餐活动。

Intent notificationIntent = new Intent(context, MainActivity.class); 
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP );
notificationIntent.putExtra(GCM_EXTRA_ID, id);
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND;
notificationManager.notify(0, notification); 

如果 Activity 被销毁,intent 会创建新的 Activity 并在 oncreate Activity 中调用这段代码

Bundle extras = getIntent().getExtras();

if(extras != null){
    if(extras.containsKey(GCMIntentService.GCM_EXTRA_ID))
    {
        int id = extras.getInt(GCMIntentService.GCM_EXTRA_ID);
        Toast.makeText(this, "GCM_EXTRA_ID ACTIVITY ON CREATE:" + Integer.toString(id) , Toast.LENGTH_LONG).show();
        dbClassItem = new DBClass(this);
        db =  dbClassItem.getWritableDatabase();
        DBClass.setLocateValue(db, id);
        db.close();
        getIntent().putExtra(GCMIntentService.GCM_EXTRA_ID, 0);
    }
}

如果我通过后退按钮关闭活动并再次启动活动,应用程序仍然可以看到,该意图具有额外的密钥 GCMIntentService.GCM_EXTRA_ID(这是一个公共静态字符串)

如何清除或替换此额外内容以供下次应用启动。 getIntent().removeExtra()getIntent().getExtras().clear(),getIntent().putExtra(GCMIntentService.GCM_EXTRA_ID, 0) 不起作用。我能做些什么 ? Activity 有 android:launchMode ="singleTop" 键。

【问题讨论】:

    标签: android android-intent extra


    【解决方案1】:

    这个问题是通过在Activity中使用onNewIntent而不是使用onCrete解决的。

    【讨论】:

      【解决方案2】:

      你可以这样做

             notificationIntent.putExtra(GCM_EXTRA_ID, null);
      

      【讨论】:

      • 但我需要在 OnCreate 中清除额外内容。我会尝试使用像“getIntent().putExtra(GCMIntentService.GCM_EXTRA_ID, null);”这样的代码。希望它会起作用,但我不这么认为......
      猜你喜欢
      • 1970-01-01
      • 2019-10-09
      • 2010-10-29
      • 2013-07-11
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 2018-08-26
      相关资源
      最近更新 更多