android实现计时器
為什么80%的碼農都做不了架構師?>>> ??
一、應用于發送驗證碼的倒計時
????? 1.使用高級控件:Chronometer
????????初始化,并實現ChronometerTickListener接口
chronometer1?=?(Chronometer)?findViewById(R.id.chronometer1); chronometer1.setOnChronometerTickListener(this);????????點擊按鈕“發送驗證碼”,觸發Chronometer的計時
chronometer1.start();注意,要在activity的onDestroy()方法中停止計時器
@Overrideprotected?void?onDestroy()?{super.onDestroy();chronometer1.stop();}關鍵代碼,如下:
@Overridepublic?void?onChronometerTick(Chronometer?chronometer)?{if?(timeLeftInS?<=?0)?{chronometer1.stop();button_sendcode.setText("發送驗證碼");button_sendcode.setEnabled(true);button_sendcode.setSelected(false);button_sendcode.setBackgroundResource(R.drawable.selector_btn);textSwitcher_sendcode.setText("發送驗證碼");textSwitcher_sendcode.setEnabled(true);textSwitcher_sendcode.setSelected(false);textSwitcher_sendcode.setBackgroundResource(R.drawable.selector_btn);timeLeftInS?=?60;return;}button_sendcode.setText("("?+?timeLeftInS?+?")s");textSwitcher_sendcode.setText("("?+?timeLeftInS?+?")s");timeLeftInS--;}2.使用CountdownTimer類
構造函數
???????? public CountDownTimer (long millisInFuture, long countDownInterval)
????????參數? millisInFuture? 從開始調用start()到倒計時完成并onFinish()方法被調用的毫秒數。(譯者注:倒計時時間,單位毫秒) countDownInterval? 接收onTick(long)回調的間隔時間。(譯者注:單位毫秒)
公共方法
public final void cancel ()????????? 取消倒計時(譯者:取消后,再次啟動會重新開始倒計時)???????????????
public abstract void onFinish ()????????? 倒計時完成時被調用?????
public abstract void onTick (long millisUntilFinished)????????? 固定間隔被調用??
????????????參數? millisUntilFinished?? 倒計時剩余時間。
public synchronized final CountDownTimer start ()
前面兩種方法基本上都是較短時間的計時,小范圍使用,最近用到的一個計時需求是:程序后臺運行10分鐘后注銷賬號,用到的是android提供的鬧鐘管理器-AlarmManager
3.AlarmManager結合BroadCastReceiver
AlarmManager的使用機制有的稱呼為全局定時器,有的稱呼為鬧鐘。通過對它的使用,個人覺得叫全局定時器比較合適,其實它的作用和Timer有點相似。都有兩種相似的用法:(1)在指定時長后執行某項操作(2)周期性的執行某項操作
AlarmManager對象配合Intent使用,可以定時的開啟一個Activity,發送一個BroadCast,或者開啟一個Service.
下面的代碼詳細的介紹了兩種定時方式的使用:
?(1)在指定時長后執行某項
//操作:發送一個廣播,廣播接收后Toast提示定時操作完成??????Intent?intent?=new?Intent(Main.this,?alarmreceiver.class);intent.setAction("short");PendingIntent?sender=PendingIntent.getBroadcast(Main.this,?0,?intent,?0);//設定一個五秒后的時間Calendar?calendar=Calendar.getInstance();calendar.setTimeInMillis(System.currentTimeMillis());calendar.add(Calendar.SECOND,?5);AlarmManager?alarm=(AlarmManager)getSystemService(ALARM_SERVICE);alarm.set(AlarmManager.RTC_WAKEUP,?calendar.getTimeInMillis(),?sender);//或者以下面方式簡化//alarm.set(AlarmManager.RTC_WAKEUP,?System.currentTimeMillis()+5*1000,?sender);Toast.makeText(Main.this,?"五秒后alarm開啟",?Toast.LENGTH_LONG).show();(2)在manifest.xml種注冊,并用廣播接受
?public?static?class?alarmreceiver?extends?BroadcastReceiver{@Overridepublic?void?onReceive(Context?context,?Intent?intent)?{//?TODO?Auto-generated?method?stubif(intent.getAction().equals("short")){Toast.makeText(context,?"short?alarm",?Toast.LENGTH_LONG).show();}else{Toast.makeText(context,?"repeating?alarm",???????????????????????Toast.LENGTH_LONG).show();}}}(3)周期性的執行某項操作
Intent?intent?=new?Intent(Main.this,?alarmreceiver.class);intent.setAction("repeating");PendingIntent?sender=PendingIntent.getBroadcast(Main.this,?0,?intent,?0);//開始時間long?firstime=SystemClock.elapsedRealtime();AlarmManager?am=(AlarmManager)getSystemService(ALARM_SERVICE); //5秒一個周期,不停的發送廣播am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,?firstime,?5*1000,?sender);AlarmManager的setRepeating()相當于Timer的Schedule(task,delay,peroid);有點差異的地方時Timer這個方法是指定延遲多長時間
以后開始周期性的執行task;
(4)AlarmManager的取消:
(其中需要注意的是取消的Intent必須與啟動Intent保持絕對一致才能支持取消AlarmManager)
Intent?intent?=new?Intent(Main.this,?alarmreceiver.class);intent.setAction("repeating");PendingIntent?sender=PendingIntent.getBroadcast(Main.this,?0,?intent,?0);AlarmManager?alarm=(AlarmManager)getSystemService(ALARM_SERVICE);alarm.cancel(sender);轉載于:https://my.oschina.net/Gxhpro/blog/369271
總結
以上是生活随笔為你收集整理的android实现计时器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: winform 配置文件的加密解密
- 下一篇: 彼岸 递推动规