我正在使用Intents來保存數據並在我的應用程序的其他位置恢復它們。我已經在其他地方使用過它們,但現在,它並不像我希望的那樣工作。如何正確使用意圖
public class GCMIntentService extends GCMBaseIntentService {
public GCMIntentService() {
super(ConstantsGCM.GCM_SENDER_ID);
}
@Override
protected void onMessage(Context context, Intent intent) {
...
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager notManager = (NotificationManager) context.getSystemService(ns);
String room = intent.getExtras().getString(ConstantsGCM.GCM_ROOM);
Intent notIntent;
PendingIntent contIntent;
Notification notif;
notif = new Notification(icon, textStatus, time);
notIntent = new Intent(contexto,RoomsActivity2.class);
Bundle b2 = new Bundle();
b2.putString(ConstantsRooms.ROOM, room);
notIntent.putExtras(b2);
contIntent = PendingIntent.getActivity(contexto, 0, notIntent, 0);
notif.setLatestEventInfo(contexto, tittle, description, contIntent);
notif.flags |= Notification.FLAG_AUTO_CANCEL;
notManager.notify((int)(Math.random()*1000), notif);
該代碼在通知即將到來時執行。當我點擊這個通知時,它會執行Activity RoomsActivities2.class。在這裏,我只是稱這個代碼:
public String getMessageString(String cod){
String result = "";
bundle = getIntent().getExtras();
if (bundle != null){
result = bundle.getString(cod);
}
return result;
}
但是,我沒有得到最後的數據保存在意圖。有什麼不對?我想我沒有正確使用它。爲什麼我無法從活動中獲取數據?
我認爲它發生的是: 該應用程序獲得大量通知,第一個正常工作。但是,如果我繼續收到更多通知,數據不會被覆蓋,並且我總是獲得第一個通知,但是當我調試代碼時,我正在設置另一個數據。
歡迎來到Stackoverflow!我建議你使用Eclipse調試器或者在你的代碼中加入'Log.d()'調用來確定發生了什麼。學習調試你自己的代碼是每個程序員的關鍵技能。 –
呵呵,我沒有,我在意圖中設置了正確的數據。但是,當我恢復它時,它似乎是另一個數據。我一直在尋找參考以及ConstantsRooms.ROOM,找出我可以在不知情的情況下改變價值的地方,但是,一切似乎都是正確的。我認爲這可能是一個問題,我沒有理解Android的生命週期。我會繼續調查。 – Guille
是什麼讓你覺得「看起來像另一個數據」。請編輯您的問題,以包括您在調試中看到的具體數據。 –