<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                > 編寫:[jdneo](https://github.com/jdneo) - 原文:[http://developer.android.com/training/scheduling/wakelock.html](http://developer.android.com/training/scheduling/wakelock.html) 為了避免電量過度消耗,Android設備會在被閑置之后迅速進入睡眠狀態。然而有時候應用會需要喚醒屏幕或者CPU并且保持它們的喚醒狀態,直至一些任務被完成。 想要做到這一點,所采取的方法依賴于應用的具體需求。但是,通常來說,你應該使用最輕量級的方法,減小其對系統資源的影響。在接下來的部分中,我們將會描述在設備默認的睡眠行為與應用的需求不相符合的情況下,你應該如何進行對應的處理。 ### 保持屏幕常亮 某些應用需要保持屏幕常亮,比如游戲與視頻應用。最好的方式是在你的[Activity](# "An activity represents a single screen with a user interface.")中(且僅在[Activity](# "An activity represents a single screen with a user interface.")中,而不在Service或其他應用組件里)使用[FLAG_KEEP_SCREEN_ON](https://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_KEEP_SCREEN_ON),例如: ~~~ 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); } ~~~ 該方法的優點與喚醒鎖(Wake Locks)不同(喚醒鎖的內容在本章節后半部分),它不需要任何特殊的權限,系統會正確地管理應用之間的切換,且不必關心釋放資源的問題。 另外一種方法是在應用的XML布局文件里,使用[android:keepScreenOn](https://developer.android.com/reference/android/R.attr.html#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_SCRRE_ON](https://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_KEEP_SCREEN_ON)等效。你可以選擇最適合你的應用的方法。在[Activity](# "An activity represents a single screen with a user interface.")中通過代碼設置常亮標識的優點在于:你可以通過代碼動態清除這個標示,從而使屏幕可以關閉。 > **Notes**:除非你不再希望正在運行的應用長時間點亮屏幕(例如:在一定時間無操作發生后,你想要將屏幕關閉),否則你是不需要清除[FLAG_KEEP_SCRRE_ON](https://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_KEEP_SCREEN_ON) 標識的。WindowManager會在應用進入后臺或者返回前臺時,正確管理屏幕的點亮或者關閉。但是如果你想要顯式地清除這一標識,從而使得屏幕能夠關閉,可以使用[clearFlags()](https://developer.android.com/reference/android/view/Window.html#clearFlags(int)): ~~~ getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON). ~~~ ### 保持CPU運行 如果你需要在設備睡眠之前,保持CPU運行來完成一些工作,你可以使用[PowerManager](https://developer.android.com/reference/android/os/PowerManager.html)系統服務中的喚醒鎖功能。喚醒鎖允許應用控制設備的電源狀態。 創建和保持喚醒鎖會對設備的電源壽命產生巨大影響。因此你應該僅在你確實需要時使用喚醒鎖,且使用的時間應該越短越好。如果想要在[Activity](# "An activity represents a single screen with a user interface.")中使用喚醒鎖就顯得沒有必要了。如上所述,可以在[Activity](# "An activity represents a single screen with a user interface.")中使用[FLAG_KEEP_SCRRE_ON](https://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_KEEP_SCREEN_ON)讓屏幕保持常亮。 使用喚醒鎖的一種合理情況可能是:一個后臺服務需要在屏幕關閉時利用喚醒鎖保持CPU運行。再次強調,應該盡可能規避使用該方法,因為它會影響到電池壽命。 > **不必使用喚醒鎖的情況**: > 1. 如果你的應用正在執行一個HTTP長連接的下載任務,可以考慮使用[DownloadManager](http://developer.android.com/reference/android/app/DownloadManager.html)。 > 1. 如果你的應用正在從一個外部服務器同步數據,可以考慮創建一個[SyncAdapter](http://developer.android.com/training/sync-adapters/index.html) > 1. 如果你的應用需要依賴于某些后臺服務,可以考慮使用[RepeatingAlarm](http://developer.android.com/training/scheduling/alarms.html)或者[Google Cloud Messaging](http://developer.android.com/google/gcm/index.html),以此每隔特定的時間,將這些服務激活。 為了使用喚醒鎖,首先需要在應用的Manifest清單文件中增加[WAKE_LOCK](https://developer.android.com/reference/android/Manifest.permission.html#WAKE_LOCK)權限: ~~~ <uses-permission android:name="android.permission.WAKE_LOCK" /> ~~~ 如果你的應用包含一個BroadcastReceiver并使用Service來完成一些工作,你可以通過[WakefulBroadcastReceiver](https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver.html)管理你喚醒鎖。后續章節中將會提到,這是一種推薦的方法。如果你的應用不滿足上述情況,可以使用下面的方法直接設置喚醒鎖: ~~~ PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE); Wakelock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakelockTag"); wakeLock.acquire(); ~~~ 可以調用[wakelock.release()](https://developer.android.com/reference/android/os/PowerManager.WakeLock.html#release())來釋放喚醒鎖。當應用使用完畢時,應該釋放該喚醒鎖,以避免電量過度消耗。 ### 使用WakefulBroadcastReceiver 你可以將BroadcastReceiver和Service結合使用,以此來管理后臺任務的生命周期。[WakefulBroadcastReceiver](https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver.html)是一種特殊的BroadcastReceiver,它關注于創建和管理應用的[PARTIAL_WAKE_LOCK](https://developer.android.com/reference/android/os/PowerManager.html#PARTIAL_WAKE_LOCK)。[WakefulBroadcastReceiver](https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver.html)會將任務交付給[Service](https://developer.android.com/reference/android/app/Service.html)(一般會是一個[IntentService](https://developer.android.com/reference/android/app/IntentService.html)),同時確保設備在此過程中不會進入睡眠狀態。如果在該過程當中沒有保持住喚醒鎖,那么還沒等任務完成,設備就有可能進入睡眠狀態了。其結果就是:應用可能會在未來的某一個時間節點才把任務完成,這顯然不是你所期望的。 要使用[WakefulBroadcastReceiver](https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver.html),首先在Manifest文件添加一個標簽: ~~~ <receiver android:name=".MyWakefulReceiver"></receiver> ~~~ 下面的代碼通過[startWakefulService()](https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver.html#startWakefulService(android.content.Context, android.content.Intent))啟動`MyIntentService`。該方法和[startService()](https://developer.android.com/reference/android/content/Context.html#startService(android.content.Intent))類似,除了[WakeflBroadcastReceiver](https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver.html)會在Service啟動后將喚醒鎖保持住。傳遞給[startWakefulService()](https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver.html#startWakefulService(android.content.Context, android.content.Intent))的Intent會攜帶有一個Extra數據,用來標識喚醒鎖。 ~~~ 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); } } ~~~ 當Service結束之后,它會調用[MyWakefulReceiver.completeWakefulIntent()](https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver.html#completeWakefulIntent(android.content.Intent))來釋放喚醒鎖。[completeWakefulIntent()](https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver.html#completeWakefulIntent(android.content.Intent))方法中的Intent參數是和[WakefulBroadcastReceiver](https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver.html)傳遞進來的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); } } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看