如何判断一个alarm是否已经被创建
2014-11-20 14:07
926 查看
首先是创建一个重复的Alarm:
long nextMillis = System.currentTimeMillis() + 1000;
AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, CoreService.class);
intent.setAction(CoreService.ACTION_SLIENCE_ALARM_BACK);
PendingIntent pendIntent = PendingIntent.getService(context,0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, nextMillis, 1000, pendIntent);
然后如果出现多个地方可能会去设置这个alarm的时候,去判断下这个alarm的pendingintent是否已经被创建,来判断是否需要再次来设置这个alarm(ps:有的手机在你设置同一个alarm的时候覆盖设置会有个系统toast)。
boolean
(PendingIntent.getBroadcast(context.getApplicationContext(), 1,intent, PendingIntent.FLAG_NO_CREATE) != null);
这个就是来判断这个alarm 的pendingintent是否被创建。这个是不能用来判断这个alarm是被cancel过还是被set过的。
FLAG_NO_CREAT能用来处理这个的原因:
The key here is the
as described in the javadoc:
long nextMillis = System.currentTimeMillis() + 1000;
AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, CoreService.class);
intent.setAction(CoreService.ACTION_SLIENCE_ALARM_BACK);
PendingIntent pendIntent = PendingIntent.getService(context,0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, nextMillis, 1000, pendIntent);
然后如果出现多个地方可能会去设置这个alarm的时候,去判断下这个alarm的pendingintent是否已经被创建,来判断是否需要再次来设置这个alarm(ps:有的手机在你设置同一个alarm的时候覆盖设置会有个系统toast)。
boolean
alarmUp=
(PendingIntent.getBroadcast(context.getApplicationContext(), 1,intent, PendingIntent.FLAG_NO_CREATE) != null);
这个就是来判断这个alarm 的pendingintent是否被创建。这个是不能用来判断这个alarm是被cancel过还是被set过的。
FLAG_NO_CREAT能用来处理这个的原因:
The key here is the
FLAG_NO_CREATEwhich
as described in the javadoc:
if the described PendingIntent **does not** already exists, then simply return null(instead of creating a new one)
相关文章推荐
- 如何判断一个SOCKET连接是否已经断开
- php 如何判断一个常量是否已经定义
- sqlite3如何判断一个表是否已经存在于数据库中c++
- 如何判断一个SOCKET连接是否已经断开
- Magento中如何判断一个用户是否已经登陆
- 如何判断一个SOCKET连接是否已经断开
- java中如何判断一个子类方法是否已经被重写
- 如何判断数据库中1个表已经存在,这样不会重复创建一个新表(用sql语句)
- php 如何判断一个常量是否已经定义
- 如何判断一个已知的表中是否存在某个字段,如果不存在就创建它.
- 如何判断一个已知的表中是否存在某个字段,如果不存在就创建它.
- 如何用创建命名对象来判断应用程序是否已有一个实例在运行?
- mysql中如何判断 一个数据表是否已经存在
- mysql创建数据表时如何判断是否已经存在?
- mysql创建数据表时如何判断是否已经存在?
- mysql创建数据表时如何判断是否已经存在?
- magento -- magento中如何判断一个用户是否已经登陆
- JS如何判断一个页面是否已经打开?
- magento -- magento中如何判断一个用户是否已经登陆
- 如何快速判断一个整数是否可以整除另一个整数的理论分析