2012-12-20 39 views
1

我創建了一個通知,這樣一個活動,意向額外的字段不更新

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)。

如何強制娛樂的意圖?還是有任何其他方式將額外的數據傳遞給活動?

回答

2

可以在onPause()在活動中使用onResume()方法和使用intent.setAction("intentAction");,並得到它在onResume()

+0

實際上,'intent.setAct ion'似乎更新了意圖,而'putExtra'沒有:) – n00b

0

使用SharedPreferences其中u希望得到如下的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"); 
+0

這將與使用靜態類字段相同,這不是ekhm線程安全:) – n00b