【问题标题】:Notification passes old Intent Extras when the Activity that read the extras values is on top当读取额外值的 Activity 位于顶部时,通知会传递旧的 Intent Extras
【发布时间】:2017-02-28 23:49:09
【问题描述】:

通知提供旧值。 我阅读了 stackoverflow 链接,但仍然无法为我工作: Notification passes old Intent Extras

我有一个活动 A。 当我在活动 B 上并触摸通知时,正确给出了 Extra 参数并显示活动 A 以及使用 getExtras(..); 读取的正确值;

那么 Activity A 仍然在顶部 - 显示在屏幕上: 我单击带有新值 putExtras(newValue) 的 second 通知,以创建一个 new 活动 A,但具有新值。

问题: intent.getExtras()` 正在读取点击的第一个通知的 旧 值,而不是第二个通知给出的新值。

我做了很多挂起意图标志和顶部链接组合的组合,但应用程序仍然采用旧值(第一次触摸通知的值)用于第二次通知。我尝试了标志:PendingIntent.FLAG_UPDATE_CURRENT 来更新值,而不是创建一个新的 Activity 和其他一些标志。

当 Activity A 仍显示在屏幕上时,如何让第二个通知为 Activity A 提供正确的值?

创建通知的代码的 sn-p。

   public void notificationCreateGu(String newMessageUserUidOfSender) {


        Intent it = new Intent(this,ActivityA.class);
        it.putExtra(USER_UID_READER,newMessageUserUidOfSender);
        StoreValuesClass.count=StoreValuesClass.count+2;
        PendingIntent pi = PendingIntent.getActivity(this, StoreValuesClass.count,it, 0);
        Notification notification = new NotificationCompat.Builder(this)
                .setTicker(newMessageUserUidOfSender )
                .setSmallIcon(android.R.mipmap.sym_def_app_icon)
                .setContentTitle("Title Message ")
                .setContentText(String.valueOf(newMessageUserUidOfSender))
                .setContentIntent(pi)
                .setAutoCancel(true)
                .build();



        int m;
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
         m= StoreValuesClass.count=StoreValuesClass.count+2;
        notificationManager.notify((m), notification);
}

//StoreValueClass.count 是一个静态值,Activity 可以读取它来为通知提供唯一的 id。

读取值的代码的 sn-p。

userUidReader = getIntent().getExtras().getString(USER_UID_READER)

我尝试将值重新加载到 onResume() 中,但在 onResume() 中仍然采用第一次读取 getExtras() 时的旧值。 我知道操作系统Android不是创建一个新的Activity,而是把它交给顶部。

【问题讨论】:

    标签: java android notifications


    【解决方案1】:

    在您的活动中覆盖 onNewIntent()。

    getIntent() 返回最初用于创建活动的Intent。如果通过startActivity() 调用将现有活动实例带回前台,则调用onNewIntent() 将用于最近一次startActivity() 调用的Intent 传递给您。

    【讨论】:

      【解决方案2】:

      使用有助于覆盖 onNewIntetn 和链接的 CommonsWare 的答案: http://www.helloandroid.com/tutorials/communicating-between-running-activities

      1 进入 xmlFile:把 android:launchMode="singleTask" 因为活动将使用 getExtras 接收额外的参数。

       <activity android:name=".ActivityWillReceiveWithGetExtras"
         android:launchMode="singleTask"
         android:taskAffinity=""
         android:excludeFromRecents="true">
      </activity>
      

      2.进入您将使用 get_extras(...) 接收值的活动覆盖一个名为 onNewIntent 的方法:

      2.1 观察:放线:

      setIntent(intent);
      

      设置意图的标识符。

         @Override
         protected void onNewIntent(Intent intent) {
             super.onNewIntent(intent);
                    setIntent(intent);//must store the new intent unless getIntent() will return the old one
              getExtraParameterActual();
      

      }

      2.2 将 Extra 参数放入一个函数中,该函数将包含在命令 getExtras(... 中

       getExtraParameterActual();
      
      1. 写上面getExtraParameterActual()的函数;

        私有 void getExtraParameterActual() { Intent intent = getIntent();//取回设置的值 //setintenT of pass 2.1 user = getIntent().getExtras().getString(USER);// }

      5。 进入 OnCreate() 调用 e getExtraParameterActual(); 并在必要时使用 reloadMyViews() 等方法重新加载视图

      1. 进入 onResume() 再次使用与第 5 步 reloadMyViews() 相同的功能重新加载视图

      7 我使用的通知代码注意标志

       public void notificationCreateGu(String User) {
      
              Log.d(TAG,nameOfTheService+"BUG createnotification for received CHAT messages useruidOfTheFriendNear="+newMessageUserUidOfSender);
              Intent it = new Intent(this,ActivityWillReceiveWithGetExtras.class);
              it.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
              it.putExtra(USER,user);
              StoreValuesClass.count=StoreValuesClass.count+2;
              PendingIntent pi = PendingIntent.getActivity(this, StoreValuesClass.count,it, PendingIntent.FLAG_UPDATE_CURRENT);
              Notification notification = new NotificationCompat.Builder(this)
                      .setTicker(newMessageUserUidOfSender )
                      .setSmallIcon(android.R.mipmap.sym_def_app_icon)
                      .setContentTitle("Title Message ")
                      .setContentText(String.valueOf(newMessageUserUidOfSender))
                      .setContentIntent(pi)
                      .setAutoCancel(true)
                      .build();
      
      
      
              int m;
              NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
               m= StoreValuesClass.count=StoreValuesClass.count+2;
              notificationManager.notify((m), notification);
      
          }
      

      【讨论】:

        猜你喜欢
        • 2011-11-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多