spa*_*agi 38 android bundle alarm android-intent
在用户从列表中选择带有时间的内容并在给定时间为其创建通知后,我尝试发出一些警报.我的问题是我的意图上的putExtra无法在广播接收器上收到的"showame".它总是得到空值.这是我对大部分意图的处理方式,但我想这次可能是因为pendingIntent或者broadcastReceiver需要不同的事情.谢谢
通过挂起的意图发送Intent的函数
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());
}
}
Run Code Online (Sandbox Code Playgroud)
这是接收端
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);
}
}
Run Code Online (Sandbox Code Playgroud)
Pri*_*ank 52
如果更改intent中的Extra值,则在创建挂起的intent时,应使用标志PendingIntent.FLAG_CANCEL_CURRENT.
一个简单的例子就是
PendingIntent pi = PendingIntent.getBroadcast(context, 0,intentWithNewExtras,PendingIntent.FLAG_CANCEL_CURRENT);
Run Code Online (Sandbox Code Playgroud)
这是正确的方法,将确保您的新价值得以实现.
希望能帮助到你.
aio*_*obe 24
意图在系统中被重用,除非它们在我认为的上下文/行动上有所不同.文档链接.也就是说,如果您已经构建了一个Intent,那么该意图也可能在以后使用.
作为调试测试,您可以尝试在intent.setAction("" + Math.random())
下面添加intent.putExtras(c)
并查看您的额外内容是否在另一端收到.
相关文件:
由于这种行为,重要的是要知道两个Intent何时被认为是相同的,以便检索PendingIntent.人们常犯的一个错误是使用Intents创建多个PendingIntent对象,这些对象只在其"额外"内容中有所不同,期望每次都获得不同的PendingIntent.这不会发生.用于匹配的Intent部分与Intent.filterEquals定义的部分相同.如果你使用两个与Intent.filterEquals等效的Intent对象,那么你将获得两个相同的PendingIntent.
归档时间: |
|
查看次数: |
21127 次 |
最近记录: |