2017-02-28 34 views
0

通知正在给出旧值。 我读了计算器链接,但仍然没有工作对我来说: Notification passes old Intent Extras当读取额外值的活动位于顶部时,通知通过旧的意向附加功能

我有一个活动A. 当我的活动B关于然后轻触通知,额外的参数是否正确给出显示活性的有使用getExtras(..)读取correctc值;

随后将活性A仍然在顶部 - 在屏幕上显示: 我点击与putExtras(NEWVALUE)的新值第二通知创建活动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(),但进入的onResume()仍然采取的第一次getExtras()读取旧值。 我明白Android的操作系统并没有创建一个新的Activity,而只是把它放在顶部。

回答

1

利用与覆盖onNewIntetn和链接帮助CommonsWare的answser: http://www.helloandroid.com/tutorials/communicating-between-running-activities

1到XMLFILE:放个Android:该活动launchMode = “singleTask” 将收到g​​etExtras额外的参数。

<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得到额外的参数成函数,将具有该命令内getExtras(...

getExtraParameterActual(); 
  • 收件的顶部的功能getExtraParameterActual();

    私人无效getExtraParameterActual(){ 意图意图= getIntent(); //收回与// setintenT设置的值通2.1 用户= getIntent()getExtras()的getString(USER); // }

  • 5. 成的OnCreate()调用电子getExtraParameterActual(); 并在必要时重新加载与例如reloadMyViews()的方法,你的意见

  • 成的onResume()与通5 reloadMyViews()
  • 的相同功能重新加载你的意见

    7我使用的通知代码注意FLAGS

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

    覆盖您的活动中的onNewIntent()

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