简体   繁体   中英

Intent extra fields not updated

I create a notification with an activity like this,

 public static void buildNotification(Context context, int id, String notiContent){
          NotificationManager notificationManager 
          = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
          NotificationCompat.Builder builder = new NotificationCompat.Builder(context);

          Intent intent = new Intent(context, NotifyActivity.class);
          intent.putExtra("notiContent", notiContent);
          intent.putExtra("notiId", id);
          intent.setFlags(Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);


          PendingIntent pendingIntent 
          = PendingIntent.getActivity(context, 0, intent, 0);

          builder
          .setSmallIcon(R.drawable.ic_launcher)
          .setContentTitle("")
          .setContentText(notiContent)
          .setContentInfo(""+Math.random())
          .setTicker("!")
          .setLights(0xFFFF0000, 500, 500) //setLights (int argb, int onMs, int offMs)
          .setContentIntent(pendingIntent)
          .setAutoCancel(true);

          Notification notification = builder.build();
          nc++;
          notificationManager.notify(nc, notification);
 }

however the onCreate method allways gets the same extra data ( notiId ).

how to force recreation of Intent ? or is there any other way to pass extra data to activity ?

You can use onResume() method and use intent.setAction("intentAction"); and get it in onResume()

use SharedPreferences in onPause() in your activity where u want to get updated value of notid as below:

 @Override
protected void onPause() 
{
  super.onPause(); 
  SharedPreferences preferences = getSharedPreferences("sharedPrefs", 0);
  SharedPreferences.Editor editor = preferences.edit();
  editor.putString("notid", "Your notid value"); // value to store
  editor.commit();    
}

and get the value of notid as below:

  SharedPreferences preferences = getSharedPreferences("sharedPrefs", 0);
  String notid= preferences.getString("notid");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM