android 定時(shí)啟動\取消小例子
更新時(shí)間:2013年06月20日 15:02:03 作者:
本文為大家講解下android實(shí)現(xiàn)定時(shí)啟動\取消的具體實(shí)現(xiàn)方式,感興趣的朋友可以參考下哈
復(fù)制代碼 代碼如下:
Intent intent = new Intent("cn.pocketdigi.update.alarm");
intent.setClass(this, AlarmReceiver.class);
PendingIntent pi=PendingIntent.getBroadcast(this, 0, intent,0);
//設(shè)置一個(gè)PendingIntent對象,發(fā)送廣播
AlarmManager am=(AlarmManager)getSystemService(ALARM_SERVICE);
//獲取AlarmManager對象
// am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+3500, pi);//只執(zhí)行一次
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+3500, 10000, pi); //重復(fù)執(zhí)行
復(fù)制代碼 代碼如下:
<receiver android:name=".AlarmReceiver">
<intent-filter>
<action android:name="cn.pocketdigi.update.alarm" />
</intent-filter>
</receiver>
復(fù)制代碼 代碼如下:
public class AlarmReceiver extends BroadcastReceiver {
private static final String TAG = "AlarmReceiver";
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.v(TAG, "收到廣播");
// Intent it=new Intent(context,AlarmActivity.class);
// it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// context.startActivity(it);
//收到廣播后啟動Activity,簡單起見,直接就跳到了設(shè)置alarm的Activity
//intent必須加上Intent.FLAG_ACTIVITY_NEW_TASK flag
}
}
取消方式:
復(fù)制代碼 代碼如下:
Intent intent = new Intent("cn.pocketdigi.update.alarm");
intent.setClass(this, AlarmReceiver.class);
PendingIntent pi=PendingIntent.getBroadcast(this, 0, intent,0);
AlarmManager alarm=(AlarmManager)getSystemService(ALARM_SERVICE);
alarm.cancel(pi);
另外一種intent方式
代碼
復(fù)制代碼 代碼如下:
Intent intent =new Intent(Main.this, alarmreceiver.class);
intent.setAction("repeating");
PendingIntent sender=PendingIntent
.getBroadcast(Main.this, 0, intent, 0);
//開始時(shí)間
long firstime=SystemClock.elapsedRealtime();
AlarmManager am=(AlarmManager)getSystemService(ALARM_SERVICE); //5秒一個(gè)周期,不停的發(fā)送廣播
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP
, firstime, 5*1000, sender);
您可能感興趣的文章:
- 基于Android中實(shí)現(xiàn)定時(shí)器的3種解決方法
- Android定時(shí)器實(shí)現(xiàn)的幾種方式整理及removeCallbacks失效問題解決
- Android中應(yīng)用界面主題Theme使用方法和頁面定時(shí)跳轉(zhuǎn)應(yīng)用
- Android三種實(shí)現(xiàn)定時(shí)器的方法
- Android定時(shí)器和Handler用法實(shí)例分析
- Android基于廣播事件機(jī)制實(shí)現(xiàn)簡單定時(shí)提醒功能代碼
- Android 實(shí)現(xiàn)定時(shí)任務(wù)的過程詳解
- Android 使用viewpager實(shí)現(xiàn)無限循環(huán)(定時(shí)+手動)
- Android后臺定時(shí)提醒功能實(shí)現(xiàn)
相關(guān)文章
Android 矢量室內(nèi)地圖開發(fā)實(shí)例
這篇文章主要介紹了Android 矢量室內(nèi)地圖開發(fā)實(shí)例的相關(guān)資料,這里提供代碼實(shí)例,及實(shí)現(xiàn)效果圖,矢量室內(nèi)對圖簡單DEMO,需要的朋友可以參考下2016-11-11android仿360加速球?qū)崿F(xiàn)內(nèi)存釋放
本篇文章實(shí)現(xiàn)了Android仿360加速球?qū)崿F(xiàn)內(nèi)存釋放,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-10-10Android 判斷當(dāng)前網(wǎng)絡(luò)是否可用簡單實(shí)例
這篇文章主要介紹了Android 判斷當(dāng)前網(wǎng)絡(luò)是否可用簡單實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-06-06Android實(shí)現(xiàn)EditText控件禁止輸入內(nèi)容的方法(附測試demo)
這篇文章主要介紹了Android實(shí)現(xiàn)EditText控件禁止輸入內(nèi)容的方法,涉及Android針對EditText控件屬性設(shè)置的相關(guān)技巧,需要的朋友可以參考下2015-12-12android實(shí)現(xiàn)簡單的畫畫板實(shí)例代碼
畫畫板實(shí)現(xiàn)起來其實(shí)很簡單,我們只需要利用android給我們提供的Canvas類來操作就可以實(shí)現(xiàn)簡單的畫畫功能2014-01-01Android自定義水波紋底部導(dǎo)航的實(shí)現(xiàn)
TabLayout作為導(dǎo)航組件來說,使用場景非常的多,也意味著要滿足各種各樣的需求,這篇文章主要介紹了Android自定義水波紋底部導(dǎo)航的實(shí)現(xiàn)2022-08-08Android無需權(quán)限調(diào)起系統(tǒng)相機(jī)
在進(jìn)行一些小型APP的開發(fā),或者是對拍照界面沒有自定義要求時(shí),我們可以用調(diào)起系統(tǒng)相機(jī)的方式快速完成拍照需求2023-03-03Android實(shí)現(xiàn)漸變圓環(huán)、圓形進(jìn)度條效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)漸變圓環(huán)、圓形進(jìn)度條效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10