zoukankan      html  css  js  c++  java
  • Android之AlarmManager

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

      在AlarmManager提供了如下方法:

    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

           具体如何使用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);
        }
    }

    运行效果图如下:

    作者:杰瑞教育
    出处:http://www.cnblogs.com/jerehedu/ 
    版权声明:本文版权归杰瑞教育技有限公司和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
    技术咨询:JRedu技术交流
     
  • 相关阅读:
    【JavaScript】函数(未完全)
    Spring集成Jersey开发(附demo)
    Tomcat中work目录的作用
    Lucene全文检索(一)
    JS放大镜特效(兼容版)
    S2SH整合
    EL表达式
    JSP和JavaBean
    Cookie和Session
    request对象和response对象
  • 原文地址:https://www.cnblogs.com/jerehedu/p/4890986.html
Copyright © 2011-2022 走看看