当前位置: 首页 > news >正文

青州建设局网站网站360优化

青州建设局网站,网站360优化,珠海网站建设培训学校,自已建外贸网站AlarmManager是Android中常用的一种系统级别的提示服务#xff0c;在特定的时刻为我们广播一个指定的Intent#xff0c;为你的应用设定一个在未来某个时间唤醒的功能。 当闹钟响起#xff0c;实际上是系统发出了为这个闹钟注册的广播#xff0c;会自动开启目标应用。 注册…AlarmManager是Android中常用的一种系统级别的提示服务在特定的时刻为我们广播一个指定的Intent为你的应用设定一个在未来某个时间唤醒的功能。 当闹钟响起实际上是系统发出了为这个闹钟注册的广播会自动开启目标应用。 注册的闹钟在设备睡眠的时候仍然会保留可以选择性地设置是否唤醒设备但是当设备关机和重启后闹钟将会被清除。 对于常规的短时间计时操作(ticks, timeouts, etc)使用Handler处理更加方便和有效率。 在alarm的receiver的onReceive()方法被执行的时候AlarmManager持有一个CPU唤醒锁这样就保证了设备在处理完广播之前不会sleep。一旦onReceive()方法返回AlarmManager就会释放这个锁表明一些情况下可能onReceive()方法一执行完设备就会sleep。如果你的alarmreceiver中调用了Context.startService()那么很可能service还没起来设备就sleep了。为了阻止这种情况你的BroadcastReceiver和Service需要实现不同的唤醒锁机制来确保设备持续运行到service可用为止。 如果onReceive()方法里确实需要异步操作的话可以用goAsync方法然后在新开一个线程去执行。 如果onReceive()有过于耗时的操作建议使用PendingIntent.getService()启动Service方式来完成。 BroadcastReceiver onReceive()使用goAsync 执行异步操作例子 Overridepublic void onReceive(final Context context, final Intent intent) {final PendingResult result goAsync();final PowerManager.WakeLock wl AlarmAlertWakeLock.createPartialWakeLock(context);try {wl.acquire();} catch (Exception e) {e.printStackTrace();}AsyncHandler.post(new Runnable() {Overridepublic void run() {LogUtils.writeAlarmLog(AlarmStateManager received intent intent);try {handleIntent(context, intent);result.finish();wl.release();} catch (Exception e) {e.printStackTrace();}}});}Alarm接口 1.cancel(PendingIntent operation) Remove any alarms with a matching Intent. 2.changeAlarmType(String pkgName, boolean wakeup) 3.getNextAlarmClock() Gets information about the next alarm clock currently scheduled. 4.set(int type, long triggerAtMillis, PendingIntent operation) Schedule an alarm. 5.setAlarmClock(AlarmManager.AlarmClockInfo info, PendingIntent operation) Schedule an alarm that represents an alarm clock. 6.setExact(int type, long triggerAtMillis, PendingIntent operation) Schedule an alarm to be delivered precisely at the stated time. 7.setInexactRepeating(int type, long triggerAtMillis, long intervalMillis, PendingIntent operation) Schedule a repeating alarm that has inexact trigger time requirements; for example, an alarm that repeats every hour, but not necessarily at the top of every hour. 8.setRepeating(int type, long triggerAtMillis, long intervalMillis, PendingIntent operation) Schedule a repeating alarm. 9.setTime(long millis) Set the system wall clock time. 10.setTimeZone(String timeZone) Set the system default time zone. 11.setWindow(int type, long windowStartMillis, long windowLengthMillis, PendingIntent operation) Schedule an alarm to be delivered within a given window of time. 常用接口 1.public void set/setExact(int type, long triggerAtMillis, PendingIntent operation) 该方法用于设置一次性闹钟。 第一个参数int type 指定定时服务的类型该参数接受如下值 ELAPSED_REALTIME 在指定的延时过后发送广播但不唤醒设备闹钟在睡眠状态下不可用。如果在系统休眠时闹钟触发它将不会被传递直到下一次设备唤醒。 ELAPSED_REALTIME_WAKEUP 在指定的延时过后发送广播并唤醒设备即使关机也会执行operation所对应的组件。延时是要把系统启动的时间SystemClock.elapsedRealtime()算进去的具体用法看代码。 RTC 指定当系统调用System.currentTimeMillis()方法返回的值与triggerAtTime相等时启动operation所对应的设备在指定的时刻发送广播但不唤醒设备。如果在系统休眠时闹钟触发它将不会被传递直到下一次设备唤醒闹钟在睡眠状态下不可用。 RTC_WAKEUP 指定当系统调用System.currentTimeMillis()方法返回的值与triggerAtTime相等时启动operation所对应的设备在指定的时刻发送广播并唤醒设备。即使系统关机也会执行 operation所对应的组件。 第二个参数triggerAtMillis表示触发闹钟的时间。 第三个参数PendingIntent pi表示闹钟响应动作 PendingIntent pi是闹钟的执行动作比如发送一个广播、给出提示等等。PendingIntent是Intent的封装类。需要注意的是 启动服务:如果是通过启动服务来实现闹钟提示的话PendingIntent对象的获取就应该采用Pending.getService(Context c,int i,Intent intent,int j)方法 启动广播:如果是通过广播来实现闹钟提示的话PendingIntent对象的获取就应该采用PendingIntent.getBroadcast(Context c,inti,Intent intent,int j)方法 启动activity:如果是采用Activity的方式来实现闹钟提示的话PendingIntent对象的获取就应该采用PendingIntent.getActivity(Context c,inti,Intent intent,int j)方法。 如果这三种方法错用了的话虽然不会报错但是看不到闹钟提示效果。 注意AndroidL开始设置的alarm的触发时间必须大于当前时间 5秒 AlarmManagerService中是通过PendingItent来标示一个Alarm的 2.public void cancel (PendingIntent operation) 移除intent相匹配的alarm只要actiondata typepackagecomponentcategories相等就会被取消。另外应用被forcestoppackage杀掉之后所有的alarm都会被移除。 3.public void setRepeating(int type,long startTime,long intervalTime,PendingIntent pi) 设置一个周期性执行的定时服务。第一个参数表示闹钟类型第二个参数表示闹钟首次执行时间第三个参数表示闹钟两次执行的间隔时间第三个参数表示闹钟响应动作。 注意该方法提供了设置周期闹钟的入口闹钟执行时间严格按照startTime来处理使用该方法需要的资源更多不建议使用。 4.public void setInexactRepeating(int type,long startTime,long intervalTime,PendingIntent pi) 该方法也用于设置重复闹钟与第二个方法相似不过其两个闹钟执行的间隔时间不是固定的而已。它相对而言更省电power-efficient一些因为系统可能会将几个差不多的闹钟合并为一个来执行减少设备的唤醒次数。第三个参数intervalTime为闹钟间隔内置的几个变量如下 INTERVAL_DAY 设置闹钟间隔一天 INTERVAL_HALF_DAY 设置闹钟间隔半天 INTERVAL_FIFTEEN_MINUTES设置闹钟间隔15分钟 INTERVAL_HALF_HOUR 设置闹钟间隔半个小时 INTERVAL_HOUR 设置闹钟间隔一个小时 AndroidL开始repeat的周期必须大于60秒 设置一个闹钟和取消一个闹钟的例子 private static class AlarmManagerStateChangeScheduler implements StateChangeScheduler {Overridepublic void scheduleInstanceStateChange(Context context, Calendar time,AlarmInstance instance, int newState) {long timeInMillis time.getTimeInMillis();Intent stateChangeIntent createStateChangeIntent(context, ALARM_MANAGER_TAG, instance,newState, VALUE_CHANGE_STATE_BY_SCHEDULE);// Treat alarm state change as high priority, use foreground broadcastsstateChangeIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);PendingIntent pendingIntent PendingIntent.getService(context, instance.hashCode(),stateChangeIntent, PendingIntent.FLAG_UPDATE_CURRENT);//设置一个闹钟AlarmManager am (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);if (CommonUtils.isMOrLater()) {am.setExactAndAllowWhileIdle(ReflectUtils.getRTC_BOOT_WAKEUP()/*AlarmManager.RTC_WAKEUP*/, timeInMillis, pendingIntent);LogUtils.writeAlarmLog(Scheduling state change success by setExactAndAllowWhileIdle);} else if (CommonUtils.isKitKatOrLater()) {am.setExact(ReflectUtils.getRTC_BOOT_WAKEUP()/*AlarmManager.RTC_WAKEUP*/, timeInMillis, pendingIntent);LogUtils.writeAlarmLog(Scheduling state change success by setExact);} else {am.set(AlarmManager.RTC_WAKEUP, timeInMillis, pendingIntent);LogUtils.writeAlarmLog(Scheduling state change success not KitKatOrLater);}}Overridepublic void cancelScheduledInstanceStateChange(Context context, AlarmInstance instance) {LogUtils.writeAlarmLog(cancelScheduledInstance instance id instance.mId);// Create a PendingIntent that will match any one set for this instancePendingIntent pendingIntent PendingIntent.getService(context, instance.hashCode(),createStateChangeIntent(context, ALARM_MANAGER_TAG, instance, null, null),PendingIntent.FLAG_NO_CREATE);//取消一个闹钟if (pendingIntent ! null) {AlarmManager am (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);am.cancel(pendingIntent);pendingIntent.cancel();ReflectUtils.cancelOnePoweroffAlarm(am, pendingIntent);}}}
http://www.huolong8.cn/news/258596/

相关文章:

  • wordpress 群组插件企业网站优化定制
  • wordpress 支付宝接口外贸seo推广
  • 电商类网站咋做大连连城传媒
  • 网站建设维护管理办法青岛专业做网站
  • wordpress登录栏seo在哪可以学
  • 凡科网站建设之后怎么删除网站建设的背景有哪些
  • 网站开发能作为无形资产吗梦幻西游官网
  • 优度公司做的网站重庆网站制作的网站
  • 安徽茶叶商城网站建设公司网站的服务器
  • 有做公司网站网站调用网页怎么做
  • 天津教育学会网站建设湖南网站建设有限公司
  • 专业制作公司网站公司mvc中手把手做网站
  • 河北网站建设有限公司app推广接单渠道
  • 游戏网站建设多少钱想做个app软件需要什么条件
  • 甘肃省路桥建设集团网站wordpress cosy
  • 做网站有底薪吗字体在线生成器
  • 网站建设合同属于什么合同衡阳网站建设开发价格
  • 可以做点赞的网站杨陵区住房和城乡建设局网站
  • 广西建工集团冶金建设公司的网站书籍网站建设的目的
  • 专门做杂志的网站有哪些为什么做营销型网站
  • 哈尔滨门户网站是什么wordpress主页代码
  • 个体工商户网站备案流程家用电脑搭建服务器
  • 免费购物网站程序好的网站开发公司
  • 扬州建设公司网站关键词排名推广软件
  • 佛山网络公司哪家便宜杭州关键词优化平台
  • 厦门创意互动网站建设淘宝怎么做基础销量什么网站好
  • 江浙沪做网站的公司南阳网站制作怎么样
  • 做商城购物网站哪儿能做邯郸网站建设
  • 网站开发app开发调查问卷 wordpress
  • 门户网站建设服务报价西湖区网站建设