2011-09-11 59 views
1

我有一個警鈴下面的代碼...問題檢索意圖額外

 public boolean onMenuItemClick(MenuItem item) { 
      Intent intent = new Intent(CalendarViewActivity.this, OneShotAlarm.class); 
      Bundle bun = new Bundle(); 
      bun.putString("data", "hello this is my message..."); 
      intent.putExtras(bun); 
      PendingIntent sender = PendingIntent.getBroadcast(CalendarViewActivity.this, 
        0, intent, 0); 

      // We want the alarm to go off 5 seconds from now. TODO 
      Calendar calendar = Calendar.getInstance(); 
      calendar.setTimeInMillis(System.currentTimeMillis()); 
      calendar.add(Calendar.SECOND, 5); 

      // Schedule the alarm! 
      AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE); 
      am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender); 
      return false; 
     } 
    }); 
} 



public class OneShotAlarm extends BroadcastReceiver { 

@Override 
public void onReceive(Context context, Intent intent) { 
    // TODO 
    Toast.makeText(context, "Alarm! " + intent.getExtras().getString("data"), Toast.LENGTH_SHORT).show(); 
} 

} 

報警正常熄滅,但「數據」額外不獲取檢索出於某種原因,並被設置爲空。

感謝您的幫助!

回答

1

嘗試:

Intent intent = new Intent(CalendarViewActivity.this, OneShotAlarm.class); 
intent.putExtra("data", "hello this is my message..."); 
PendingIntent sender = PendingIntent.getBroadcast(CalendarViewActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
+0

這解決了這個問題,謝謝 – Jonny

0

引文從文檔的putExtra功能:

名稱額外數據的名稱,,包裝前綴爲

難道不是這個原因嗎?