2010-05-21 49 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());` 

希望它会帮助别人,快乐编码..! :)