闹钟开发过程中用PendingIntent传送数据丢失解决办法
2014-01-14 22:38
555 查看
当要设置一个闹钟时,可以把数据放在Intent里,再用intent对象生成一个PendingIntent对象,然后用AlarmManager 来邦定PendingIntent对象设置闹钟,具体代码如下:
Intent intent = new Intent(context, AlarmReceiver.class);
intent.putExtra("id", alarm.getId());
intent.putExtra("weekday", getCurrentWeekday());
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, alarm.getId(), intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, triggerAtTime, pendingIntent);
闹钟设置的代码基本上是这样的,但是如果在启动的Broadcast中接收Intent过来的数据,有时会得到一个null值,也就是说,根本没有数据传过来。
因此查看官方api,发现 PendingIntent pendingIntent = PendingIntent.getBroadcast(context, alarm.getId(), intent, 0);的最后一个参数参数是int flag,这个值可以是FLAG_ONE_SHOT, FLAG_NO_CREATE, FLAG_CANCEL_CURRENT, FLAG_UPDATE_CURRENT
简单翻译一下:
int FLAG_CANCEL_CURRENT:如果该PendingIntent已经存在,则在生成新的之前取消当前的。
int FLAG_NO_CREATE:如果该PendingIntent不存在,直接返回null而不是创建一个PendingIntent.
int FLAG_ONE_SHOT:该PendingIntent只能用一次,在send()方法执行后,自动取消。
int FLAG_UPDATE_CURRENT:如果该PendingIntent已经存在,则用新传入的Intent更新当前的数据。
我们需要把最后一个参数改为PendingIntent.FLAG_UPDATE_CURRENT,这样在启动的Activity里就可以用接收Intent传送数据的方法正常接收。
Intent intent = new Intent(context, AlarmReceiver.class);
intent.putExtra("id", alarm.getId());
intent.putExtra("weekday", getCurrentWeekday());
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, alarm.getId(), intent,endingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, triggerAtTime, pendingIntent);
Intent intent = new Intent(context, AlarmReceiver.class);
intent.putExtra("id", alarm.getId());
intent.putExtra("weekday", getCurrentWeekday());
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, alarm.getId(), intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, triggerAtTime, pendingIntent);
闹钟设置的代码基本上是这样的,但是如果在启动的Broadcast中接收Intent过来的数据,有时会得到一个null值,也就是说,根本没有数据传过来。
因此查看官方api,发现 PendingIntent pendingIntent = PendingIntent.getBroadcast(context, alarm.getId(), intent, 0);的最后一个参数参数是int flag,这个值可以是FLAG_ONE_SHOT, FLAG_NO_CREATE, FLAG_CANCEL_CURRENT, FLAG_UPDATE_CURRENT
简单翻译一下:
int FLAG_CANCEL_CURRENT:如果该PendingIntent已经存在,则在生成新的之前取消当前的。
int FLAG_NO_CREATE:如果该PendingIntent不存在,直接返回null而不是创建一个PendingIntent.
int FLAG_ONE_SHOT:该PendingIntent只能用一次,在send()方法执行后,自动取消。
int FLAG_UPDATE_CURRENT:如果该PendingIntent已经存在,则用新传入的Intent更新当前的数据。
我们需要把最后一个参数改为PendingIntent.FLAG_UPDATE_CURRENT,这样在启动的Activity里就可以用接收Intent传送数据的方法正常接收。
Intent intent = new Intent(context, AlarmReceiver.class);
intent.putExtra("id", alarm.getId());
intent.putExtra("weekday", getCurrentWeekday());
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, alarm.getId(), intent,endingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, triggerAtTime, pendingIntent);
相关文章推荐
- 闹钟开发过程中用PendingIntent传送数据丢失解决办法
- 闹钟开发过程中用PendingIntent传送数据丢失解决办法
- Android AppWidget 开发中PendingIntent传送数据丢失解决办法
- Android AppWidget 开发中PendingIntent传送数据丢失解决办法
- 用PendingIntent传送数据丢失解决办法
- 用PendingIntent传送数据丢失解决办法
- PendingIntent传送数据丢失解决办法
- Android 多个 AppWidget PendingIntent 传送数据丢失解决办法
- 关于Android 6.0及以上版本用PendingIntent传送数据丢失问题
- ajax开发过程表单提交数据出现乱码和解决办法
- ssh开发过程中遇到的问题,以及一些解决办法
- socket简单实现ftp的文件传送(C++V2.0版,解决数据丢失问题)
- 一些开发ssh2过程中遇到错误的解决办法
- 微博,VC开发过程中的故障解决办法
- android开发环境 安装ADT 一直pending解决办法
- 应用和系统开发过程中,高并发的访问对同一数据的不一致,读脏数据的处理办法
- iOS开发过程中的一些错误提示和解决办法大全
- .NETC#下AE二次开发过程中出现的属性表不能更新问题解决办法
- 一次导如数据库时主表数据丢失的解决过程
- Android开发中之删除手机电话薄中的一条数据时之删除不成功的解决办法