关于定时执行任务的解决方法,PendingIntent alarm braocastRecevier等等
在需要執(zhí)行定時(shí)任務(wù)的類中定義如下:
? ? ? ? ? ? ?Intent intent=new Intent(SimpleWakefulController.this,SimpleWakefulReceiver.class);
? ? ? ? ? ? ?Pending intent=PedingIntent.getBroadcast(SimpleWakefulController.this,0,intent,0);
? ? ? ? ? ? ?
? ? ? ? ? ? ? Calendar calendar=Calendar.getInstance();
? ? ? ? ? ? ? calendar.setTimeInMillis(System.currentTimeMills());
? ? ? ? ? ? ?calendar.add(Calenda.Second,30);
? ? ? ? ?
? ? ? ? ? ? ?//Schedule the alarm
? ? ? ? ? ? AlarmManager am=(AlarmManager)getSystemService(AlARM_SERVICE);
? ? ? ? ? am.set(AlarmManager.PTC_WAKEUP,calendar.getTimeINMills(),sender);
? ? ? ? ??
? ? ? ? ? ? //Tell the user about what we did
? ? ? ? ? ?if(mToast!=null)
? ? ? ? ? ? ? ? ? ?mToast.cancel();
? ?在Receiver類繼承WakefulBroadcastReceiver中定義:
? ? ? ?Intent service=new Intent(context,SimpleWakefulService.class);
? ? ? ?
? ? ? ? startWakefulService(context,service);
在service類中:
? ? ? ? ? ? ?onHandleIntent來處理相應(yīng)的房
為了避免消耗電池,一個(gè)空閑的設(shè)備很快就會(huì)進(jìn)入休眠,但是有的時(shí)候需要應(yīng)用保持屏幕或CPU喚醒來完成一些工作。
? ? ? 實(shí)現(xiàn)的方法取決于你的APP的要求,一般是采取最輕量級(jí)的方法,以減少你的APP對(duì)系統(tǒng)資源的影響。下面講述如何處理設(shè)備默認(rèn)的休眠行為和應(yīng)用需求之間的矛盾。
? ? ?某些APP需要保持屏幕一直亮起,如游戲和看電影。最好的方法就是在Activity(只是Activity,而不是services或其他App的組件)中使用FLAG_KEEP_SCREEN_ON標(biāo)志。例如:
public?class?MainActivity?extends?Activity?{
??@Override
??protected?void?onCreate(Bundle?savedInstanceState)?{
????super.onCreate(savedInstanceState);
????setContentView(R.layout.activity_main);
????getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
??}
? ? ? 和wakelock(下面會(huì)討論到)比較,這個(gè)方法好處就是不需要申請(qǐng)權(quán)限,平臺(tái)就能正確地管理移動(dòng)于應(yīng)用之間的用戶,也不需要你的APP去考慮無用資源的釋放。
? ? ? 另一個(gè)方法去實(shí)現(xiàn)設(shè)備亮起就是在應(yīng)用的布局文件中使用?Android:keepScreenOn屬性:
<RelativeLayout?xmlns:android="http://schemas.android.com/apk/res/android"
????android:layout_width="match_parent"
????android:layout_height="match_parent"
????android:keepScreenOn="true">
????...
</RelativeLayout>
? ? ? 使用android:keepScreenOn="true"和使用?FLAG_KEEP_SCREEN_ON是一樣的,你可以選擇較適合的一種。在Activity設(shè)置的好處是可以清除?FLAG_KEEP_SCREEN_ON標(biāo)志,關(guān)閉屏幕。
? ? ? 注意:正常情況下,你是不需要清除?FLAG_KEEP_SCREEN_ON標(biāo)志的,除非你不想屏幕在應(yīng)用還運(yùn)行時(shí)保持亮起(比如程序有一段時(shí)間都沒有發(fā)生活動(dòng),你想把屏幕關(guān)閉)。當(dāng)你的APP跑到后臺(tái)或恢復(fù)前臺(tái)時(shí),Window?manager會(huì)進(jìn)行相應(yīng)的設(shè)置。但是如果你想顯式地進(jìn)行清除這個(gè)標(biāo)志,關(guān)閉屏幕,可以使用clearFlag():
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON).
2.保持CPU運(yùn)行
? ? ?有時(shí)候?yàn)榱嗽谠O(shè)備休眠之前能完成一些工作,必須保持CPU運(yùn)行。你可以使用一個(gè)PowerManager系統(tǒng)服務(wù)特性——WakeLock實(shí)現(xiàn)。Wakelock允許APP控制設(shè)備的電源狀態(tài)。
? ? ?創(chuàng)建并持有Wakelock對(duì)設(shè)備電池壽命有很大影響。所以你應(yīng)該在確實(shí)很必要的時(shí)候才使用,且使用時(shí)間盡可能少。比如在Acitivity中就沒必要用了,如前面所述,可以使用FLAG_KEEP_SCREEN_ON標(biāo)志。
? ? ?使用WakeLock一個(gè)合適的例子就是,當(dāng)屏幕關(guān)閉的時(shí)候,后臺(tái)服務(wù)中需要持有WakeLock來保持CPU運(yùn)行,以繼續(xù)完成工作。但還是那句話,應(yīng)盡量避免WakeLock的使用,否則會(huì)影響電池壽命。
? ? ?使用WakeLock的第一步就是在應(yīng)用的manifest文件中添加WAKE_LOCK權(quán)限
<uses-permission?android:name="android.permission.WAKE_LOCK"?/>
? ? ? 如果你的APP包含有broadcast?Receiver,且在broadcast?Receiver中開啟了服務(wù)去完成一些工作,那么可以通過?WakefulBroadcastReceiver來管理WakeLock,下面將會(huì)講述,這是我們推薦的方法,如果你的APP不符合這個(gè)模式,下面是直接設(shè)置WakeLock的方法:
PowerManager?powerManager?=?(PowerManager)?getSystemService(POWER_SERVICE);
Wakelock?wakeLock?=?powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
????????"MyWakelockTag");
wakeLock.acquire();
? ? ? 調(diào)用wakelock.release()來釋放WakeLock,這會(huì)釋放CPU。當(dāng)你的APP執(zhí)行完成時(shí)不要忘了釋放WakeLock,這樣能避免消耗電池。
3.使用WakefulBroadcastReceiver
? ? ? 使用Broadcast?Receiver和service能讓你很好地管理后臺(tái)任務(wù)的生命周期。
? ? ? WakefulBroadcastReceiver是BroadcastReceiver的一種特例。它會(huì)為你的APP創(chuàng)建和管理一個(gè)PARTIAL_WAKE_LOCK?類型的WakeLock。WakefulBroadcastReceiver把工作交接給service(通常是IntentService),并保證交接過程中設(shè)備不會(huì)進(jìn)入休眠狀態(tài)。如果不持有WakeLock,設(shè)備很容易在任務(wù)未執(zhí)行完前休眠。最終結(jié)果是你的應(yīng)用不知道會(huì)在什么時(shí)候能把工作完成,相信這不是你想要的。
? ? ? 像其他BroadcastReceiver使用WakefulBroadcastReceiver的第一步就是把它加到manifest中:
<receiver?android:name=".MyWakefulReceiver"></receiver>
? ? ? 下面的代碼通過startWakefulService()開啟MyIntentService,該方法和?startService()相似,不同之處在于WakefulBroadcastReceiver會(huì)在服務(wù)開啟的時(shí)候持有一個(gè)WakeLock。startWakefulService傳遞的intent封裝了WakeLock的標(biāo)識(shí)符。
public?class?MyWakefulReceiver?extends?WakefulBroadcastReceiver?{
????@Override
????public?void?onReceive(Context?context,?Intent?intent)?{
????????//?Start?the?service,?keeping?the?device?awake?while?the?service?is
????????//?launching.?This?is?the?Intent?to?deliver?to?the?service.
????????Intent?service?=?new?Intent(context,?MyIntentService.class);
????????startWakefulService(context,?service);
????}
}
? ? ? 當(dāng)服務(wù)結(jié)束的時(shí)候,會(huì)調(diào)用?MyWakefulReceiver.completeWakefulIntent()釋放WakeLock,completeWakefulIntent的參數(shù)是WakefulBroadcastReceiver傳來的intent:
public?class?MyIntentService?extends?IntentService?{
????public?static?final?int?NOTIFICATION_ID?=?1;
????private?NotificationManager?mNotificationManager;
????NotificationCompat.Builder?builder;
????public?MyIntentService()?{
????????super("MyIntentService");
????}
????@Override
????protected?void?onHandleIntent(Intent?intent)?{
????????Bundle?extras?=?intent.getExtras();
????????//?Do?the?work?that?requires?your?app?to?keep?the?CPU?running.
????????//?...
????????//?Release?the?wake?lock?provided?by?the?WakefulBroadcastReceiver.
????????MyWakefulReceiver.completeWakefulIntent(intent);
????}
}
總結(jié)
以上是生活随笔為你收集整理的关于定时执行任务的解决方法,PendingIntent alarm braocastRecevier等等的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于LocalBroadcastMana
- 下一篇: 关于自定义可以点击的的布局