日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android实现计时器

發布時間:2024/4/14 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 ()

private?CountDownTimer?timer?=?new?CountDownTimer(10000,?1000)?{@Overridepublic?void?onTick(long?millisUntilFinished)?{vertifyView.setText((millisUntilFinished?/?1000)?+?"秒后可重發");}@Overridepublic?void?onFinish()?{vertifyView.setEnabled(true);vertifyView.setText("獲取驗證碼");}};

前面兩種方法基本上都是較短時間的計時,小范圍使用,最近用到的一個計時需求是:程序后臺運行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实现计时器的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。