這篇文章將為大家詳細(xì)講解有關(guān)Android AlarmManager如何實(shí)現(xiàn)定時循環(huán)后臺任務(wù),文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
你所需要的網(wǎng)站建設(shè)服務(wù),我們均能行業(yè)靠前的水平為你提供.標(biāo)準(zhǔn)是產(chǎn)品質(zhì)量的保證,主要從事成都網(wǎng)站制作、成都做網(wǎng)站、外貿(mào)營銷網(wǎng)站建設(shè)、企業(yè)網(wǎng)站建設(shè)、手機(jī)網(wǎng)站開發(fā)、網(wǎng)頁設(shè)計(jì)、高端網(wǎng)站設(shè)計(jì)、網(wǎng)頁制作、做網(wǎng)站、建網(wǎng)站。成都創(chuàng)新互聯(lián)公司擁有實(shí)力堅(jiān)強(qiáng)的技術(shù)研發(fā)團(tuán)隊(duì)及素養(yǎng)的視覺設(shè)計(jì)專才。
AlarmManager簡介
AlarmManager是Android中常用的一種系統(tǒng)級別的提示服務(wù),在特定的時刻為我們廣播一個指定的Intent。簡單的說就是我們設(shè)定一個時間,然后在該時間到來時,AlarmManager為我們廣播一個我們設(shè)定的Intent,通常我們使用 PendingIntent。
項(xiàng)目功能簡介:
AlarmService模擬后臺任務(wù),定時發(fā)起廣播 AlarmReceive啟動AlarmService,達(dá)到循環(huán)啟動Service的效果
通過Service和Receiver的死循環(huán),確保后臺任務(wù)不被系統(tǒng)殺死。
1.AlarmService類
/** * 一個定時任務(wù) */public class AlarmService extends Service { /** * 每1分鐘更新一次數(shù)據(jù) */ private static final int ONE_Miniute=60*1000; private static final int PENDING_REQUEST=0; public AlarmService() { } /** * 調(diào)用Service都會執(zhí)行到該方法 */ @Override public int onStartCommand(Intent intent, int flags, int startId) { //這里模擬后臺操作 new Thread(new Runnable() { @Override public void run() { Log.e("wj","循環(huán)執(zhí)行了,哈哈."+ System.currentTimeMillis()); } }).start(); //通過AlarmManager定時啟動廣播 AlarmManager alarmManager= (AlarmManager) getSystemService(ALARM_SERVICE); long triggerAtTime=SystemClock.elapsedRealtime()+ONE_Miniute;//從開機(jī)到現(xiàn)在的毫秒書(手機(jī)睡眠(sleep)的時間也包括在內(nèi) Intent i=new Intent(this, AlarmReceive.class); PendingIntent pIntent=PendingIntent.getBroadcast(this,PENDING_REQUEST,i,PENDING_REQUEST); alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,triggerAtTime,pIntent); return super.onStartCommand(intent, flags, startId); } @Override public IBinder onBind(Intent intent) { // TODO: Return the communication channel to the service. throw new UnsupportedOperationException("Not yet implemented"); }}
2 AlarmReceive類
public class AlarmReceive extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { //循環(huán)啟動Service Intent i = new Intent(context, AlarmService.class); context.startService(i); }}
3 啟動Service
public void startService(View view){ Intent intent=new Intent(this, AlarmService.class); startService(intent); }
別忘了AndroidMainfest中注冊廣播和服務(wù):
<service android:name=".service.AlarmService" android:enabled="true" android:exported="true"/><receiver android:name=".receive.AlarmReceive"/>
關(guān)于Android AlarmManager如何實(shí)現(xiàn)定時循環(huán)后臺任務(wù)就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
當(dāng)前標(biāo)題:AndroidAlarmManager如何實(shí)現(xiàn)定時循環(huán)后臺任務(wù)
文章出自:http://aaarwkj.com/article36/jeeppg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、域名注冊、網(wǎng)站改版、用戶體驗(yàn)、手機(jī)網(wǎng)站建設(shè)、動態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)