2010-05-21 30 views
36

從Intent啓動的getExtra我想在用戶從列表中選擇一段時間並在給定時間爲它創建通知之後發出警報。我的問題是,我的意圖putExtra無法在廣播接收器上收到的「showname」。它總是得到空值。 這是我爲我的大部分意圖做的方式,但我認爲這一次可能是因爲pendingIntent或broadcastReceiver需要做不同的事情。 謝謝從一個pendingIntent

穿過未決意圖

public void setAlarm(String showname,String time) { 

    String[] hourminute=time.split(":"); 
    String hour = hourminute[0]; 
    String minute = hourminute[1]; 
    Calendar rightNow = Calendar.getInstance(); 
    rightNow.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hour)); 
    rightNow.set(Calendar.MINUTE, Integer.parseInt(minute)); 
    rightNow.set(Calendar.SECOND, 0); 
    long t=rightNow.getTimeInMillis(); 
    long t1=System.currentTimeMillis(); 

    try { 

    Intent intent = new Intent(this, alarmreceiver.class); 
    Bundle c = new Bundle();    
    c.putString("showname", showname);//This is the value I want to pass 
    intent.putExtras(c); 
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 12345, intent, 0); 

    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
    alarmManager.set(AlarmManager.RTC_WAKEUP, rightNow.getTimeInMillis(),pendingIntent); 
    //Log.e("ALARM", "time of millis: "+System.currentTimeMillis()); 
    Toast.makeText(this, "Alarm set", Toast.LENGTH_LONG).show(); 

    } catch (Exception e) { 
     Log.e("ALARM", "ERROR IN CODE:"+e.toString()); 
    } 
} 

發送意圖的功能和這是接收端

public class alarmreceiver extends BroadcastReceiver { 

@Override 
public void onReceive(Context context, Intent intent) { 
    // Toast.makeText(context, "Alarm worked.", Toast.LENGTH_LONG).show();  
    Bundle b = intent.getExtras(); 
    String showname=b.getString("showname");//This is where I suppose to receive it but its null 
    NotificationManager manger = (NotificationManager) context 
      .getSystemService(context.NOTIFICATION_SERVICE); 

    Notification notification = new Notification(R.drawable.icon, 
      "TVGuide Υπενθύμιση", System.currentTimeMillis()); 
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, 
      new Intent(context, tvguide.class), 0); 

    notification.setLatestEventInfo(context, "Το Πρόγραμμα Ξεκίνησε", 
      showname, contentIntent); 

    notification.flags = Notification.FLAG_ONLY_ALERT_ONCE; 

    notification.sound = Uri.parse("file:///sdcard/dominating.mp3"); 
    notification.vibrate = new long[]{100, 250, 100, 500}; 
    manger.notify(1, notification); 
}   
} 

回答

23

意圖被重用在系統中,除非它們對上下文不同/我相信的行動。 Documentation Link。也就是說,如果你已經構建了一個意圖,那麼這個意圖也可能在以後使用。

作爲調試測試,您可以嘗試在intent.putExtras(c)以下添加intent.setAction("" + Math.random())並查看是否在另一端收到了您的附加信息。

相關文檔

由於這種行爲的,重要的是要知道當兩個意圖被認爲是用於檢索的PendingIntent的目的是相同的。人們犯的一個常見錯誤是創建多個PenttentIntent對象,其Intents只在其「額外」內容中有所不同,期望每次都得到不同的PendingIntent。這不會發生。用於匹配的Intent部分與Intent.filterEquals定義的部分相同。如果您使用兩個與Intent.filterEquals等效的Intent對象,那麼您將爲它們獲得相同的PendingIntent。

+2

這是正確的答案......... – Necronet 2012-10-11 15:38:12

+1

+1,這是正確的答案,它應該被接受。這真的很有用,Thx。 (順便說一下,在創建待定目標時,您還可以使用不同的請求代碼,並使用散列,這非常方便)。 – Snicolas 2013-07-25 09:11:44

50

如果您在意圖中更改Extra的值,那麼在創建掛起的意圖時,應該使用標誌PendingIntent.FLAG_CANCEL_CURRENT。

一個簡單的例子是

PendingIntent pi = PendingIntent.getBroadcast(context, 0,intentWithNewExtras,PendingIntent.FLAG_CANCEL_CURRENT); 

這是正確的做法,並確保新的值傳遞。

希望它有幫助。

+0

一旦意圖啓動,系統嘗試重新利用它並且沒有額外的改變,這似乎無法在小部件中使用鰭狀肢。 – Necronet 2012-10-11 15:37:56

+3

爲我工作,我被困了幾個小時! – Ali 2012-12-03 11:31:26

+3

解決了2小時的問題.... – 2013-08-21 11:31:03

9

對不同的報警通知使用不同的請求代碼,以避免覆蓋相同的報警時間。

+0

這是這類問題的最佳答案!針對問題的非常簡單和美觀的解決方法。 – 2012-03-10 18:01:33

+0

我同意,非常簡單的解決方法。 – jp36 2012-12-18 22:43:04

0

您應該使用intent.putExtra()方法。未將捆綁包設置爲額外細分受衆羣。如果你設置了字符串,你應該嘗試這個將會完成。我用它。

0

我有同樣的問題。我按照@aioobe的建議在這裏設置了一個動作來解決它,而我的意圖就像一個魔法。

這裏我做了什麼

` intent.putExtra("Name", mName); 
    intent.putExtra("dateTime", newdate); 
    intent.setAction("" + Math.random());` 

希望它會幫助別人,快樂編碼..! :)