星空网 > 软件开发 > 操作系统

Android之AlarmManager

  Android平台中,Alarm Manager Service控制着闹钟和唤醒功能。和其他系统服务一样,提供了一个辅助管理类-AlarmManager,我们只需要使用AlarmManager即可调用Alarm Manager Service。

  在AlarmManager提供了如下方法:

Android之AlarmManagerimages/loading.gif' data-original="http://images2015.cnblogs.com/blog/708076/201510/708076-20151019092630114-1662898031.png" />

1、void cancel(pendingIntent operatioin):取消一个已注册的定时器

2、void set(int type,long triggerAtTime,PendingIntent operation):设置一个新的定时器。

3、void setInecactRepeating(int type,long triggerAtMillis,long intervalMills,PendingIntent operation):设置一个不精确的重复类型的定时器。

4、void setRepeating(int type,long triggerAtMills,long intervalMills,PendingIntent operation):设置一个重复类型的定时器

5、void setTime(long millis):设置系统时钟时间。

6、void setTimeZone(String timeZone):设置时区

在上面设置时钟的方法中,第一个参数要设置一个闹钟的类型,在系统中提供了如下类型:

1、ELAPSED_REALTIME:此类型的闹钟在系统休眠状态下是不可用的,不能唤醒系统。使用的时间是相对于系统的启动时间,该时间可以通过SystemClock.elapsedRealTime()来获取。

2、ELAPSED_REALTIME_WAKEUP:此类型的闹钟在系统休眠状态下是可用的,使用的时间是相对于系统的启动时间。

3、RTC:此类型在系统休眠状态下是不可用的,使用的是真实时间。

4、RTC_WAKEUP:此类型在系统休眠状态下可用,使用的是真实时间。

在上面的设置可重复的时钟的方法中,intervalMillis参数的取值可以如下:

1、INTERVAL_FIFTEEN_MINUTES 

2、INTERVAL_HALF_HOUR 

3、INTERVAL_HOUR 

4、INTERVAL_HALF_DAY 

5、INTERVAL_DAY

Android之AlarmManager

       具体如何使用AlarmManager呢,我们通过一个案例来说明。

public class AlarmManagerActivity extends ActionBarActivity {  private AlarmManager alarmManager;  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_alarm_manager);    alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);    IntentFilter filter = new IntentFilter("com.jredu.action.MyAlarm");    registerReceiver(receiver,filter);  }  public void setClock(View v){    Intent i = new Intent("com.jredu.action.MyAlarm");    PendingIntent intent=        PendingIntent.getBroadcast(this,0,i,PendingIntent.FLAG_UPDATE_CURRENT);    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,        System.currentTimeMillis(),        30*1000,intent);  }  public void cancleClock(View v){    Intent i = new Intent("com.jredu.action.MyAlarm");    PendingIntent intent=        PendingIntent.getBroadcast(this,0,i,PendingIntent.FLAG_UPDATE_CURRENT);    alarmManager.cancel(intent);  }  private BroadcastReceiver receiver = new BroadcastReceiver() {    @Override    public void onReceive(Context context, Intent intent) {      if(intent.getAction().equals("com.jredu.action.MyAlarm")){        Toast.makeText(AlarmManagerActivity.this,"这是我设置的闹钟!",Toast.LENGTH_LONG).show();      }    }  };  @Override  protected void onDestroy() {    super.onDestroy();    unregisterReceiver(receiver);  }}

运行效果图如下:

Android之AlarmManagerAndroid之AlarmManager

 

作者:杰瑞教育
出处:http://www.cnblogs.com/jerehedu/ 
版权声明:本文版权归烟台杰瑞教育科技有限公司和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
技术咨询:Android之AlarmManager
 



原标题:Android之AlarmManager

关键词:Android

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

中国外贸环境巨变:海水还是火焰:https://www.kjdsnews.com/a/658434.html
今年的亚马逊好像没那么火了:https://www.kjdsnews.com/a/658435.html
卖家请注意!eBay推出系列新工具:https://www.kjdsnews.com/a/658436.html
eBay推出新平台全面助力卖家!:https://www.kjdsnews.com/a/658437.html
哪些产品会在排灯节期间大卖?:https://www.kjdsnews.com/a/658438.html
年轻人的社交宠儿,看 Snapchat 如何解锁 Z 世代流量密码!:https://www.kjdsnews.com/a/658439.html
夹江千佛岩景区门票(夹江千佛岩景区门票价格):https://www.vstour.cn/a/411232.html
武陵山大裂谷周围景点 武陵山大裂谷周围景点图片:https://www.vstour.cn/a/411233.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流