Java 当读取额外值的活动位于顶部时,通知将传递旧的意图额外值

Java 当读取额外值的活动位于顶部时,通知将传递旧的意图额外值,java,android,notifications,Java,Android,Notifications,通知正在提供旧值。 我阅读了stackoverflow链接,但仍然不适用于我: 我有一个活动a。 当我在活动B上并触摸通知时,额外参数被正确给出,并用getExtras(..)读取的正确C值显示活动A 然后活动A仍在顶部-在屏幕上显示: 我点击带有putExtras(newValue)新值的第二个通知,创建带有新值的新建活动a 问题是: intent.getExtras()正在读取第一个通知的旧值,而不是第二个通知给出的新值 public void notificationCreateG

通知正在提供旧值。 我阅读了stackoverflow链接,但仍然不适用于我:

我有一个活动a。 当我在活动B上并触摸通知时,额外参数被正确给出,并用getExtras(..)读取的正确C值显示活动A

然后活动A仍在顶部-在屏幕上显示: 我点击带有putExtras(newValue)新值的第二个通知,创建带有新值的新建活动a

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

   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);
}
我对挂起意图的标志和顶部链接的组合进行了很多组合,但应用程序仍然将旧值(第一次触摸的通知的值)用于第二次通知。我尝试了标志:pendingent.flag\u UPDATE\u CURRENT来更新值,而不是创建一个新的活动和一些其他标志

当活动A仍然显示在屏幕上时,如何使第二个通知为活动A提供正确的值

创建通知的代码片段

   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是一个静态值,活动可以读取该值以为通知提供唯一id

   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);
}
读取值的代码片段

userUidReader = getIntent().getExtras().getString(USER_UID_READER)
我试图将这些值重新加载到onResume()中,但在onResume()中,仍然会使用第一次读取getExtras()时的旧值。 我知道操作系统Android并没有创建一个新的活动,只是将其交给了最高层。

在您的活动中覆盖
onNewIntent()


getIntent()
返回最初用于创建活动的
Intent
。如果通过
startActivity()
调用将现有活动实例带回前台,则调用
onNewIntent()
以向您提供用于最近的
startActivity()
调用的
意图。

使用有助于覆盖onNewIntetn和链接的Commonware Answer:

1放入xmlFile:put th-android:launchMode=“singleTask” 对于活动,将使用getExtras接收额外参数

 <activity android:name=".ActivityWillReceiveWithGetExtras"
   android:launchMode="singleTask"
   android:taskAffinity=""
   android:excludeFromRecents="true">
</activity>
设置意图的标识符

   @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将额外参数放入一个函数中,该函数将在命令
getExtras(…

 getExtraParameterActual();
  • 编写top getExtraParameterActual()的函数

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

  • 五,。 调用e getExtraParameterActual(); 如果有必要,可以使用一个方法重新加载视图,例如reloadMyViews()

  • into onResume()使用pass 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);
    
        }