我使用此类活动
创建通知 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);
}
但onCreate
方法总是获得相同的额外数据(notiId
)。
如何强制娱乐意图?或者是否有其他方法将额外数据传递给活动?
答案 0 :(得分:1)
您可以使用onResume()
方法并使用intent.setAction("intentAction");
并在onResume()
中获取
答案 1 :(得分:0)
在您的活动SharedPreferences
中使用onPause()
,您希望获得notid
的更新值,如下所示:
@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();
}
并获取notid的值如下:
SharedPreferences preferences = getSharedPreferences("sharedPrefs", 0);
String notid= preferences.getString("notid");