【紧急求助】一个程序不能设置俩闹钟广播么?
如题。
我定义了两个闹钟,set不同的stringextra到一个action,但在recever中获取的时候,发现get到的stringextra总是第一次发送的那个值,比如我第一次发送的test1,那么之后无论我怎么发送test2和test1,得到的永远都是test1。只有删除程序,从新安装了,才会有效,不过也只是有效一次。。。
附代码,请问这个是为什么……为毛会串。。。
/* 闹钟1 */
Intent intent = null;
intent = new Intent(MainActivity.this, MainReceiver.class);
intent.setAction("one_broadcast");
Log.i("tag", "set action with test1");
intent.putExtra("action_key", "test1");
PendingIntent sender = null;
sender = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
Log.i("tag", "action content = " + intent.getStringExtra("test1"));
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
AlarmManager alarm = null;
alarm = (AlarmManager) MainActivity.this.getSystemService(Context.ALARM_SERVICE);
alarm.set(AlarmManager.RTC, calendar.getTimeInMillis(), sender);
/* 闹钟2 */
Intent intent = null;
intent = new Intent(MainActivity.this, MainReceiver.class);
intent.setAction("one_broadcast");
Log.i("tag", "set action with test2");
intent.putExtra("action_key", "test2");
PendingIntent sender = null;
sender = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
Log.i("tag", "action content = " + intent.getStringExtra("test2"));
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
AlarmManager alarm = null;
alarm = (AlarmManager) MainActivity.this.getSystemService(Context.ALARM_SERVICE);
alarm.set(AlarmManager.RTC, calendar.getTimeInMillis(), sender);
随后我在MainReceiver.class中getAction后getStringExtra并将值打印,异常的现象就发生了。
直接发送广播就没问题,通过闹钟就不行,请问是我对闹钟的理解有什么问题么?求助。
谢谢!!!
分享到:
更多
------解决方案--------------------
PendingIntent.getBroadcast(MainActivity.this, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT);
试下