chinese直男口爆体育生外卖, 99久久er热在这里只有精品99, 又色又爽又黄18禁美女裸身无遮挡, gogogo高清免费观看日本电视,私密按摩师高清版在线,人妻视频毛茸茸,91论坛 兴趣闲谈,欧美 亚洲 精品 8区,国产精品久久久久精品免费

定時(shí)器刷新詳解(程序介紹)

來源:電子發(fā)燒友整理 作者:2018年01月26日 16:09
關(guān)鍵詞:程序定時(shí)器

本文為大家介紹定時(shí)器刷新的詳細(xì)程序。

1、定時(shí)刷新 只刷新一次

首先要發(fā)送一個(gè)廣播 PendingTIntent.getBroadcast()就類似于一個(gè)sendBroadcast

里面有四個(gè)參數(shù) 第一個(gè)就是context 第二個(gè)參數(shù)是個(gè)發(fā)送端的私人參數(shù),起區(qū)分作用 第三個(gè)intent 第四個(gè) flags參數(shù)可以指定PendingIntent的一些行為特點(diǎn),是用來針對Intent.fillIn() ,這里面沒有用到0即可。

定時(shí)器刷新詳解(程序介紹)

PendingTIntent 核心就是異步激發(fā) 有興趣的可以看

PnedingTIngtent詳解

am.set()方法用來激發(fā),第一個(gè)參數(shù)是鬧鐘的類型 就不贅述了 第二個(gè)就是開始時(shí)間()

這個(gè)參數(shù)的類型要根據(jù)前一個(gè)鬧鐘的類型來的 , RTC_WAKEUP RTC POWER_OFF_WAKEUP使用的絕對時(shí)間,其他的類型就是相對時(shí)間 ,相對時(shí)間就是相對于開機(jī)時(shí)運(yùn)行的時(shí)間,絕對時(shí)間就是當(dāng)前的時(shí)間。

public staTIc void sendUpdateBroadcast(Context context,long time){

AlarmManager am = context.getSystemService(Context.ALARM_SERVICE);

Intent i = new Intent(conext, UpdateReceiver.class);

i.putExtra(“time”, time);//time參數(shù)是刷新間隔

PendingIntent pendingIntent = PendingIntent.getBroadcast(contexxt, 0, i, 0);

//我這個(gè)是系統(tǒng)現(xiàn)在時(shí)間加上time時(shí)間進(jìn)行刷新

am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + time, pendingIntent);

}

然后需要自定義一個(gè)接受器刷新的動(dòng)作在這里面執(zhí)行

public static class UpdateReceiver extends BroadcastReceiver {

public void onReceive(Context context, Intent intent) {

long times=intent.getLongExtra(“time”,0);

Toast.makeText(context, “開始刷新”+intent.getLongExtra(“time”,0), Toast.LENGTH_SHORT).show();

}

}

2、定時(shí)刷新 根據(jù)間隔時(shí)間一直刷新

類似于鬧鐘

am.setRepeating ()第二個(gè)參數(shù)就是第一次刷新時(shí)間 (如果時(shí)間已經(jīng)過了,會馬上響應(yīng)一次),第三個(gè)就是間隔時(shí)間 。注意此廣播非覆蓋的 如若要改變刷新時(shí)間一定要先取消此廣播

public static void sendBroadcastRepeat(Context ctx,int hour,int minuter){

Intent intent =new Intent(ctx, RepeatReceiver.class);

intent.putExtra(“hour”,hour);

intent.putExtra(“minuter”,minuter);

PendingIntent pendingIntent = PendingIntent.getBroadcast(ctx,0, intent, 0);

Calendar calendar = Calendar.getInstance();

calendar.set(Calendar.HOUR_OF_DAY, hour);

calendar.set(Calendar.MINUTE, minuter);

calendar.set(Calendar.SECOND, 00);

calendar.set(Calendar.MILLISECOND, 0);

AlarmManager am = getAlaramManager(ctx);

am.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(), 1000 * 60 * 60 * 24, pendingIntent);

}

同樣也要寫一個(gè)接收器

public static class RepeatReceiver extends BroadcastReceiver {

public void onReceive(Context context, Intent intent) {

Toast.makeText(context, “定時(shí)刷新”, Toast.LENGTH_SHORT).show();

}

}

關(guān)注電子發(fā)燒友微信

有趣有料的資訊及技術(shù)干貨

下載發(fā)燒友APP

打造屬于您的人脈電子圈

關(guān)注發(fā)燒友課堂

鎖定最新課程活動(dòng)及技術(shù)直播
聲明:電子發(fā)燒友網(wǎng)轉(zhuǎn)載作品均盡可能注明出處,該作品所有人的一切權(quán)利均不因本站而轉(zhuǎn)移。
作者如不同意轉(zhuǎn)載,既請通知本站予以刪除或改正。轉(zhuǎn)載的作品可能在標(biāo)題或內(nèi)容上或許有所改動(dòng)。
收藏 人收藏
分享:

相關(guān)閱讀

發(fā)表評論

elecfans網(wǎng)友

分享到:

用戶評論(0