当读取额外值的活动在顶部时,通知会传递旧意图



通知给出了旧值。我阅读了stackoverflow链接,但仍然对我不起作用:通知通过旧意图

我有一个活动。当我在活动b上并触摸通知时,将正确给出额外的参数,并显示具有使用getExtras(..);

的CRORCEC值读取的活动A

然后,活动A仍在顶部 - 在屏幕上显示:我单击 second 带有Putextras(NewValue)新值的通知,以创建 new 活动A,但具有新值。

问题:intent.getExtras()

我制作了许多待处理意图的标志和顶部链接的组合,但是第二个通知仍在使用旧值(第一次接触通知的值)。我尝试了标志: pendingIntent.flag_update_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。

代码段读取值。

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

我试图将值重新加载到onresume()中,但是读取读取getExtras()的旧值仍在读取。我知道,Android的操作系统不是创建新活动,而只是将其赋予顶部。

使用Commonsware的Annesser,它帮助了Override OnnewIntetn和链接:http://www.helloandroid.com/tutorials/communicating-betnunning-activities

1进入XMLFILE:放置Android:lainingMode =" singletask"因为该活动将通过getExtras接收额外的参数。

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

2. into您将通过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将额外的参数获取到命令getExtras(...

内包含的函数
 getExtraParameterActual();
  1. 写下顶部getExtraparametiactal();

    的功能

    private void getExtraparametActual(){ 意图意图= getIntent();//取回pass 2.1的//setIntent的值 user = getIntent()。getExtras()。getString(user);// }

5。进入onCreate()调用e getExtraparametiactual();如有必要

  1. in resume()再次重新加载您的视图,并通过Pass 5 ReloadMyViews()
  2. 的相同功能重新加载您的视图

7我使用的notificatio代码请注意旗帜

 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);
    }

在您的活动中覆盖onNewIntent()

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

最新更新