2015-09-07 28 views
1

我已經從這個鏈接學習WakefulBroadcastReceiver:https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver.html混淆WakefulBroadcastReceiver

我有這方面的困惑幾個:

  1. 這是否接收器可以確保在設備是你甚至接收廣播在睡眠模式下? (我認爲不是,只是在收到廣播後才讓設備保持清醒狀態,直到致電完成WakefulIntent()爲止。)
  2. 該文檔說明了在接收方內使用意圖服務,並且在完成工作之後,完成WakefulIntent。

CODE:

import android.app.IntentService; 
import android.content.Intent; 
import android.os.SystemClock; 
import android.util.Log; 

public class SimpleWakefulService extends IntentService { 
    public SimpleWakefulService() { 
     super("SimpleWakefulService"); 
    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 
     // At this point SimpleWakefulReceiver is still holding a wake lock 
     // for us. We can do whatever we need to here and then tell it that 
     // it can release the wakelock. This sample just does some slow work, 
     // but more complicated implementations could take their own wake 
     // lock here before releasing the receiver's. 
     // 
     // Note that when using this approach you should be aware that if your 
     // service gets killed and restarted while in the middle of such work 
     // (so the Intent gets re-delivered to perform the work again), it will 
     // at that point no longer be holding a wake lock since we are depending 
     // on SimpleWakefulReceiver to that for us. If this is a concern, you can 
     // acquire a separate wake lock here. 
     for (int i=0; i<5; i++) { 
      Log.i("SimpleWakefulReceiver", "Running service " + (i+1) 
        + "/5 @ " + SystemClock.elapsedRealtime()); 
      try { 
       Thread.sleep(5000); 
      } catch (InterruptedException e) { 
      } 
     } 
     Log.i("SimpleWakefulReceiver", "Completed service @ " + SystemClock.elapsedRealtime()); 
     SimpleWakefulReceiver.completeWakefulIntent(intent); 
    } 
} 

現在,作爲文檔說,這個接收方持有喚醒鎖,我高度懷疑,這是一個很好的做法,開始像從接收器的意圖的服務。這會阻止接收器釋放喚醒鎖並耗盡大量電池,因爲服務通常用於長操作

即使上面的代碼片斷突出顯示,在釋放鎖之前有25秒的延遲。這是使用該接收機的正確方式,還是隻能用於短時間操作?任何幫助表示讚賞。

+0

我不是一個完美的人說它的好或壞的做法,但它的作品完美 –

+1

基本上沒有對reciver本身的引用......它只是開始持有的意圖鎖...和* *靜態**方法釋放鎖... – Selvin

+0

@Selvin即使沒有參考(我的意思是靜態方法,正如你所建議的),仍然存在喚醒鎖,這將會消耗電池嗎? –

回答

10

這個接收器是否確保您即使在設備處於睡眠模式時也能收到廣播?

(我認爲不是,它只是讓設備接收廣播直到completeWakefulIntent呼叫後甦醒()製成。)

正確的。

我高度懷疑,這是一個很好的做法,從接收機

是的,這是一個很好的做法開始像意圖服務。這就是整個觀點。

這將防止所述接收器從釋放之後鎖定

喚醒鎖處於static數據成員,這就是爲什麼一個static方法(completeWakefulWork())是能夠釋放它保持。

的服務通常用於長時間操作

的「長」的定義各不相同。我開始使用WakefulBroadcastReceiver(或我的WakefulIntentService前任)來處理任何可能超過10秒的事情。然而,任何形式的IntentService實際上只是設計用於事務性工作(例如,下載大文件),因此它不適用於服務可能需要無限期運行的情況(例如,,只要用戶想要與某個聊天服務器交談)。

如果您希望確保CPU保持活動狀態,以便服務完成其工作,則只能使用WakefulBroadcastReceiver。否則,您直接使用IntentService

這是使用此接收器的正確方法,還是隻能用於短操作?

25秒是使用WakefulBroadcastReceiver及其關聯的IntentService的完全合理的時間範圍。

但更復雜的實現可以在這裏拿自己的喚醒鎖釋放接收器的

當然之前,雖然目前還不清楚這會得到你。喚醒鎖是一個喚醒鎖。服務擁有的喚醒鎖對電池的影響與接收機擁有的喚醒鎖相同。

+0

謝謝!清晰簡潔。我現在明白了很多。 –

+0

你可以舉一些什麼時候使用WakefulBroadcastReceiver的例子嗎?警報管理器應用程序使用它嗎? –

+0

@androiddeveloper:「你可以舉一些什麼時候使用WakefulBroadcastReceiver的例子嗎?」 - 任何時候你的代碼在後臺被喚醒,你都需要一個'WakeLock',除非另有說明,因爲你不知道你會醒來多久。 'WakefulBroadcastReceiver'專門設計用於'AlarmManager',因爲'AlarmManager'確保設備保持足夠長的時間以便完成'onReceive()'。 – CommonsWare