android 定時(shí)啟動(dòng)\取消小例子
更新時(shí)間:2013年06月20日 15:02:03 作者:
本文為大家講解下android實(shí)現(xiàn)定時(shí)啟動(dòng)\取消的具體實(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對(duì)象,發(fā)送廣播
AlarmManager am=(AlarmManager)getSystemService(ALARM_SERVICE);
//獲取AlarmManager對(duì)象
// 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);
//收到廣播后啟動(dòng)Activity,簡(jiǎn)單起見,直接就跳到了設(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使用方法和頁(yè)面定時(shí)跳轉(zhuǎn)應(yīng)用
- Android三種實(shí)現(xiàn)定時(shí)器的方法
- Android定時(shí)器和Handler用法實(shí)例分析
- Android基于廣播事件機(jī)制實(shí)現(xiàn)簡(jiǎn)單定時(shí)提醒功能代碼
- Android 實(shí)現(xiàn)定時(shí)任務(wù)的過程詳解
- Android 使用viewpager實(shí)現(xiàn)無(wú)限循環(huán)(定時(shí)+手動(dòng))
- Android后臺(tái)定時(shí)提醒功能實(shí)現(xiàn)
相關(guān)文章
Android使用GPS獲取用戶地理位置并監(jiān)聽位置變化的方法
這篇文章主要介紹了Android使用GPS獲取用戶地理位置并監(jiān)聽位置變化的方法,實(shí)例分析了Android編程中GPS定位的實(shí)現(xiàn)與使用技巧,需要的朋友可以參考下2015-12-12
Ubuntu中為Android系統(tǒng)實(shí)現(xiàn)內(nèi)置Java應(yīng)用程序測(cè)試Application Frameworks層的硬件服務(wù)
本文主要介紹Ubuntu中為Android系統(tǒng)內(nèi)置應(yīng)用訪問Application Frameworks層的硬件服務(wù),這里提供了詳細(xì)的流程和代碼實(shí)例,有興趣的朋友可以參考下2016-08-08
android APP登陸頁(yè)面適配的實(shí)現(xiàn)
這篇文章主要介紹了android APP登陸頁(yè)面適配的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2018-09-09
Android?控件自動(dòng)貼邊實(shí)現(xiàn)實(shí)例詳解
這篇文章主要為大家介紹了Android?控件自動(dòng)貼邊實(shí)現(xiàn)實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
擁抱kotlin之如何習(xí)慣使用kotlin高階函數(shù)
這篇文章主要給大家介紹了關(guān)于擁抱kotlin之如何習(xí)慣使用kotlin高階函數(shù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用kotlin具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
Android識(shí)別預(yù)裝的第三方App方法實(shí)例
這篇文章主要給大家介紹了關(guān)于Android如何識(shí)別預(yù)裝的第三方App的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01

