2011-12-21 37 views
1

我得到這個例外,在我的代碼:意圖的標誌和PendingIntent.getBroadcast

...IllegalArgumentException...Cant use FLAG_RECEIVER_BOOT_UPGRADE here... 

展望Android源代碼好像你不能設置標誌,以將通過被解僱的意圖:

PendingIntent.getBroadcast(...); 

這裏的Android源代碼:

... 
if (type == INTENT_SENDER_BROADCAST) { 
    if ((intent.getFlags()&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0) { 
     throw new IllegalArgumentException("Can't use FLAG_RECEIVER_BOOT_UPGRADE here"); 
    } 
} 
... 

這裏我的代碼:

Intent myIntent = new Intent(context, MyReceiver.class); 
//myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); if i remove the comment it doesn't work 
PendingIntent pending = PendingIntent. 
      getBroadcast(context, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

原因尚不清楚,有人可以爲我澄清一下嗎?

回答

4

當您使用getBroadcast()獲得PendingIntent時,此Intent將廣播給BroadcastReceivers。它不會被用來啓動一個活動。所以你不能設置任何與活動相關的標誌。無論如何,他們在這方面都沒有任何意義。

你爲什麼要設置FLAG_ACTIVITY_NEW_TASK在將被廣播的意圖?這是沒有意義的。

Android使用3個完全不同的目的意圖:

  1. 開始/與Activity
  2. 開始溝通/與Service
  3. 廣播傳達給BroadcastReceiver

PendingIntent類提供3種不同方法爲這些不同目的獲得PendingIntent:

  1. getActivity()
  2. 的getService()
  3. getBroadcast()

你需要確保你使用正確的目的,正確的方法。

是的,只要調用getActivity()獲取PendingIntent,就可以在PendingIntent中設置與Activity相關的Intent標誌。

+0

嘿大衛,這是我的壞。當時我遇到這個問題時沒有給予足夠的關注,謝謝你的迴應。 – gwa 2012-06-25 08:44:46