android 定时器的实现

系统 1904 0

 


在Android上常用的定时器有两种,一种是Java.util.Timer,一种就是系统的AlarmService了。 


实验1:使用Java.util.Timer。 
在onStart()创创建Timer,每5秒更新一次计数器,并启动。 


mTimer = new   Timer();       

mTimer.schedule( new   TimerTask() {           

             @Override

             public   void   run() {

                 ++mCount;

                 mHandler.sendEmptyMessage( 0 );               

             }

         }, 5 * 1000 , 5 * 1000 );
  

当连接USB线进行调试时,会发现一切工作正常,每5秒更新一次界面,即使是按下电源键,仍然会5秒触发一次。 
当拔掉USB线,按下电源键关闭屏幕后,过一段时间再打开,发现定时器明显没有继续计数,停留在了关闭电源键时的数字。 

实验2:使用AlarmService: 
2.1通过AlarmService每个5秒发送一个广播,setRepeating时的类型为AlarmManager.ELAPSED_REALTIME。 


AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);  

am.setRepeating(AlarmManager.ELAPSED_REALTIME, firstTime, 5 * 1000 , sender);

拔掉USB线,按下电源键,过一段时间再次打开屏幕,发现定时器没有继续计数。 
2.2setRepeating是的类型设置为AlarmManager.ELAPSED_REALTIME_WAKEUP 


AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);   

am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, 5 * 1000 , sender);

拔掉USB线,按下电源键,过一点时间再次打开屏幕,发现定时器一直在计数。 

如此看来,使用WAKEUP才能保证自己想要的定时器一直工作,但是肯定会引起耗电量的增加



AlarmManager的使用机制有的称呼为全局定时器,有的称呼为闹钟。通过对它的使用,个人觉得叫全局定时器比较合适,其实它的作用和Timer有点相似。都有两种相似的用法:(1)在指定时长后执行某项操作(2)周期性的执行某项操作

AlarmManager对象配合Intent使用,可以定时的开启一个Activity,发送一个BroadCast,或者开启一个Service.

下面的代码详细的介绍了两种定时方式的使用:

 (1)在指定时长后执行某项操作

 

Java代码   收藏代码
  1.       //操作:发送一个广播,广播接收后Toast提示定时操作完成   
  2. <!--  
  3.   
  4. Code highlighting produced by Actipro CodeHighlighter (freeware)  
  5. http: //www.CodeHighlighter.com/   
  6.   
  7. -->     Intent intent = new  Intent(Main. this , alarmreceiver. class );  
  8.     intent.setAction( "short" );  
  9.     PendingIntent sender=  
  10.         PendingIntent.getBroadcast(Main. this 0 , intent,  0 );  
  11.       
  12.      //设定一个五秒后的时间   
  13.     Calendar calendar=Calendar.getInstance();  
  14.     calendar.setTimeInMillis(System.currentTimeMillis());  
  15.     calendar.add(Calendar.SECOND,  5 );  
  16.       
  17.     AlarmManager alarm=(AlarmManager)getSystemService(ALARM_SERVICE);  
  18.     alarm.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);  
  19.      //或者以下面方式简化   
  20.      //alarm.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+5*1000, sender);   
  21.       
  22.     Toast.makeText(Main. this "五秒后alarm开启" , Toast.LENGTH_LONG).show();  
 

//注意:receiver记得在manifest.xml注册


Java代码   收藏代码
  1. <!--  
  2.   
  3. Code highlighting produced by Actipro CodeHighlighter (freeware)  
  4. http: //www.CodeHighlighter.com/   
  5.   
  6. -->     public   static   class  alarmreceiver  extends  BroadcastReceiver{  
  7.   
  8.          @Override   
  9.          public   void  onReceive(Context context, Intent intent) {  
  10.              // TODO Auto-generated method stub   
  11.              if (intent.getAction().equals( "short" )){  
  12.                 Toast.makeText(context,  "short alarm" , Toast.LENGTH_LONG).show();  
  13.             } else {  
  14.                 Toast.makeText(context,  "repeating alarm" ,   
  15.                       Toast.LENGTH_LONG).show();  
  16.             }  
  17.         }  
  18.     }  
 

(2)周期性的执行某项操作

 

Java代码   收藏代码
  1. <!--  
  2.   
  3. Code highlighting produced by Actipro CodeHighlighter (freeware)  
  4. http: //www.CodeHighlighter.com/   
  5.   
  6. -->    Intent intent = new  Intent(Main. this , alarmreceiver. class );  
  7.     intent.setAction( "repeating" );  
  8.     PendingIntent sender=PendingIntent  
  9.         .getBroadcast(Main. this 0 , intent,  0 );  
  10.       
  11.   
  12.      //开始时间   
  13.      long  firstime=SystemClock.elapsedRealtime();  
  14.   
  15.     AlarmManager am=(AlarmManager)getSystemService(ALARM_SERVICE);  
  16.   
  17. //5秒一个周期,不停的发送广播   
  18.     am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP  
  19.             , firstime,  5 * 1000 , sender);  
 

 AlarmManager的setRepeating()相当于Timer的Schedule(task,delay,peroid);有点差异的地方时Timer这个方法是指定延迟多长时间

以后开始周期性的执行task;

AlarmManager的取消:(其中需要注意的是取消的Intent必须与启动Intent保持绝对一致才能支持取消AlarmManager)

 

 

Java代码   收藏代码
  1. <!--  
  2.   
  3. Code highlighting produced by Actipro CodeHighlighter (freeware)  
  4. http: //www.CodeHighlighter.com/   
  5.   
  6. -->  Intent intent = new  Intent(Main. this , alarmreceiver. class );  
  7.   intent.setAction( "repeating" );  
  8.   PendingIntent sender=PendingIntent  
  9.          .getBroadcast(Main. this 0 , intent,  0 );  
  10.   AlarmManager alarm=(AlarmManager)getSystemService(ALARM_SERVICE);  

android 定时器的实现


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论