Android后台處理最佳實踐(Best Practices for Background Jobs)


        本課將告訴你如何通過后台加載來加速應用啟動和降低應用耗電。

后台跑服務

         除非你做了特殊指定,否則在應用中的大部分前台操作都是在一個特殊的UI線程里面進行的。這有可能會導致一些問題,因為長時間運行的操作會影響到你應用的響應速度。為了避免這個問題,android框架提供了一系列幫助你在后台通過線程推遲加載的功能,被使用得最多的非IntentService莫屬了。

         本課將向你描述如何實現一個IntentService,發送請求操作並向其它組件報告結果。

創建一個后台服務

         本課將直觀地告訴你如何通過后台線程執行操作,通過它來執行耗時操作從而避免你的界面無法及時響應的問題。IntentService是不會受窗口生命周期回調的影響的,所以在繼續運行它之前,你需要關閉AsyncTask。

         每個IntentService都是有限制條件的:

(1)它不可以直接和應用的界面進行交互,為了將操作結果返回給界面,你需要將他們發送到窗口;

(2) 工作請求是按順序進行的,當已經有一個操作在IntentService中運行時,如果這是你發送另外一個請求,需要等到第一個操作執行完畢后才會繼續后面的請求;

(3) 在IntentService中運行的操作是不可以被中斷的。

 

        然而,多數情況下一個IntentService是后台操作最簡單的處理方式。

        本課將告訴你如何創建你自己的IntentService子類,還會向你展示如何創建一個必要的onHandleIntent()回調。最后,將告訴你如何在清單文件中定義IntentService。

 

創建一個IntentService

         為了在你的應用中創建一個IntentService組件,需要定義一個繼承於IntentService的類並復寫其onHandleIntent()方法,比如:

 

  1. <span style="font-size:18px">public class RSSPullService extends IntentService {  
  2.     @Override  
  3.     protected void onHandleIntent(Intent workIntent) {  
  4.         // Gets data from the incoming Intent  
  5.         String dataString = workIntent.getDataString();  
  6.         ...  
  7.         // Do work here, based on the contents of dataString  
  8.         ...  
  9.     }  
  10. }  
  11. </span>  

 

         注意,對於任何一個Service都會回調的那些方法,比如onStartCommand(),都會自動地被IntentService引用。在一個IntentService中,你應該避免復寫這些回調方法。

 

在清單文件Manifest中定義IntentService

         IntentService同樣需要在你應用的清單文件中有一個入口,通過在<application>標簽下聲明<service>的方式來為IntentService提供入口:

 
  1. <span style="font-size:18px"><application  
  2.         android:icon="@drawable/icon"  
  3.         android:label="@string/app_name">  
  4.         ...  
  5.         <!--  
  6.             Because android:exported is set to "false",  
  7.             the service is only available to this app.  
  8.         -->  
  9.         <service  
  10.             android:name=".RSSPullService"  
  11.             android:exported="false"/>  
  12.         ...  
  13.     <application/>  
  14. </span>  

         上例中的“android:name”屬性指定了IntentService的類名。

         注意,<service>標簽沒有包含IntentFilter過濾器。該窗口通過一個明確地Intent向服務發送工作請求,所以不需要任何過濾器。也就是說,只有在同一個應用內,或者是有相同ID的其它應用才可以訪問這個服務。

         現在你有了基本的IntentService類,你可以通過Intent對象發送工作請求了。

向后台服務發送工作請求

         之前的課程向我們展示了如何創建一個IntentService類。本課將告訴你如何通過發送Intent來觸發IntentService執行一個操作。這個Intent可以包含IntentService需要處理的可選數據。你可以在Activity或者Fragment的任何一個地方向IntentService傳遞Intent。

 

創建並發送一個工作請求給IntentService

         為了創建一個工作請求並將其發送到IntentService,需要創建一個明確地Intent來添加工作請求數據,然后通過調用IntentService的StartService()方法來發送它。

         具體請看下面實例:

1、為IntentService的子類RSSPullService創建一個新的、明確地Intent。

 
  1. <span style="font-size:18px">/* 
  2.  * Creates a new Intent to start the RSSPullService 
  3.  * IntentService. Passes a URI in the 
  4.  * Intent's "data" field. 
  5.  */  
  6. mServiceIntent = new Intent(getActivity(), RSSPullService.class);  
  7. mServiceIntent.setData(Uri.parse(dataUrl));  
  8. </span>  

2、調用startService()方法

 

  1. <span style="font-size:18px">// Starts the IntentService  
  2. getActivity().startService(mServiceIntent);  
  3. </span>  

 

         注意,你可以在Activity或者Fragment的任何地方發送工作請求。比如,如果你需要首先獲取用戶輸入,你可以在按鈕點擊或者類似於手勢操作的回調中來發送請求。

         一旦你調用了startService()方法,IntentService會處理定義在onHandleIntent()方法中的工作,然后自己停止。

         下一步是向原始的Activity或者Fragment報告工作請求的結果,下一個將告訴你如何通過BroadcastReceiver來實現這個功能。

 

報告工作狀態

         本課將告訴你如何將后台服務的請求工作狀態報告給發送請求的組件。這將允許你,比如報告一個窗口對象的UI更新請求狀態。一般推薦使用LocalBroadcastManager來發送和接收這些狀態,但這僅限於在你自己應用的各組件中廣播Intent。

 

從IntentService報告狀態

         為了在IntentService中向其他組件發送工作請求狀態,首先你需要創建一個包含狀態信息數據的Intent,作為了一個選項,你可以在Intent中添加一個操作或者數據URI。

         下一步,通過調用LocalBroadcastManager.sendBroadcast()方法來發送Intent,在你應用中發送到其它組件的Intent是注冊過的。通過LocalBroadcastManager的getInstance()方法來實例化LocalBroadcastManager。

比如:

 
  1. <span style="font-size:18px">public final class Constants {  
  2.     ...  
  3.     // Defines a custom Intent action  
  4.     public static final String BROADCAST_ACTION =  
  5.         "com.example.android.threadsample.BROADCAST";  
  6.     ...  
  7.     // Defines the key for the status "extra" in an Intent  
  8.     public static final String EXTENDED_DATA_STATUS =  
  9.         "com.example.android.threadsample.STATUS";  
  10.     ...  
  11. }  
  12. public class RSSPullService extends IntentService {  
  13. ...  
  14.     /* 
  15.      * Creates a new Intent containing a Uri object 
  16.      * BROADCAST_ACTION is a custom Intent action 
  17.      */  
  18.     Intent localIntent =  
  19.             new Intent(Constants.BROADCAST_ACTION)  
  20.             // Puts the status into the Intent  
  21.             .putExtra(Constants.EXTENDED_DATA_STATUS, status);  
  22.     // Broadcasts the Intent to receivers in this app.  
  23.     LocalBroadcastManager.getInstance(this).sendBroadcast(localIntent);  
  24. ...  
  25. }  
  26. </span>  

 

從IntentService接收廣播狀態

         為了能夠接收Intent對象,需要定義一個BroadcastReciver的子類。在該類中,實現BroadcastReceiver的onReceive()回調方法,在接收到一個Intent時LocalBroadcastManager會引用它。LocalBroadcastManager將接收到的Intent傳遞到BroadcastReceiver的onRecive()方法中。

比如:

  1. <span style="font-size:18px">// Broadcast receiver for receiving status updates from the IntentService  
  2. private class ResponseReceiver extends BroadcastReceiver  
  3. {  
  4.     // Prevents instantiation  
  5.     private DownloadStateReceiver() {  
  6.     }  
  7.     // Called when the BroadcastReceiver gets an Intent it's registered to receive  
  8.     @  
  9.     public void onReceive(Context context, Intent intent) {  
  10. ...  
  11.         /* 
  12.          * Handle Intents here. 
  13.          */  
  14. ...  
  15.     }  
  16. }  
  17. </span>  

         一旦你定義了BroadcastReceiver,你就可以通過指定動作、類別和數據等過濾信息來匹配它了。為了達到這種效果,你需要創建一個IntentFilter。下面的代碼向你展示了如何定義filter:

  1. <span style="font-size:18px">// Class that displays photos  
  2. public class DisplayActivity extends FragmentActivity {  
  3.     ...  
  4.     public void onCreate(Bundle stateBundle) {  
  5.         ...  
  6.         super.onCreate(stateBundle);  
  7.         ...  
  8.         // The filter's action is BROADCAST_ACTION  
  9.         IntentFilter mStatusIntentFilter = new IntentFilter(  
  10.                 Constants.BROADCAST_ACTION);  
  11.       
  12.         // Adds a data filter for the HTTP scheme  
  13.         mStatusIntentFilter.addDataScheme("http");  
  14.         ...  
  15. </span>  

         為了在系統中注冊BroadcastReceiver和IntentFilter,你需要實例化LocalBroadcastManager並調用其registerReceiver()方法。下例展示的是如何注冊BroadcastReceiver和其過濾器的過程:

 

  1. <span style="font-size:18px">// Instantiates a new DownloadStateReceiver  
  2.         DownloadStateReceiver mDownloadStateReceiver =  
  3.                 new DownloadStateReceiver();  
  4.         // Registers the DownloadStateReceiver and its intent filters  
  5.         LocalBroadcastManager.getInstance(this).registerReceiver(  
  6.                 mDownloadStateReceiver,  
  7.                 mStatusIntentFilter);  
  8.         ...  
  9. </span>  

 

         一個BroadcastReceiver可以操作多於一種類型的廣播Intent對象,每個類型都有自己的操作。這種特征允許你在不同的action中運行代碼,不需要為每個action都定義一個BroadcastReceiver。為了為同一個BroadcastReceiver定義其它的IntentFilter,創建IntentFilter並重復調用registerReceiver()。比如:

 
  1. <span style="font-size:18px">/* 
  2.          * Instantiates a new action filter. 
  3.          * No data filter is needed. 
  4.          */  
  5.         statusIntentFilter = new IntentFilter(Constants.ACTION_ZOOM_IMAGE);  
  6.         ...  
  7.         // Registers the receiver with the new filter  
  8.         LocalBroadcastManager.getInstance(getActivity()).registerReceiver(  
  9.                 mDownloadStateReceiver,  
  10.                 mIntentFilter);  
  11. </span>  

         發送一個廣播Intent不會start或者resume一個窗口。即使你的窗口在后台,窗口中的BroadcastReceiver都是可以接收並處理Intent對象的,但並不會強制讓你的應用處於前台。當你的窗口處於后台時如果你想向用戶通知這個事件,你可以使用Notification。在接收一個廣播Intent時是絕不會啟動一個窗口的。

 

在后台加載數據

         對於你需要顯示的數據,但有需要花時間去通過ContentProvider查詢時,如果你直接在窗口層面去執行查詢操作,可能會嚴重影響界面的響應速度,比如ANR。就算不會ANR,用戶也會明顯地感覺到卡頓的現象。為了避免這種問題,你應該在非UI線程里面來初始化查詢操作,直到等待它結束后再窗口顯示結果。

         你可以通過一個對象在后台執行查詢同步,待查詢結束后更新UI。這個對象就是CursorLoader。除了初始化后台查詢外,當查詢有變動時CursorLoader會自動地重新查詢數據。

         本課將向你描述如何通過CursorLoader執行后台查詢操作。在課程中用到了V1-SupportLibrary版本的類,它支持V1.6及以上的版本執行此操作。

 

通過CursorLoader執行查詢操作

         通過CursorLoader在后台執行同步查詢有別於ContentProvider,它會返回結果到調用它的Activity或者FragmentActivity。這樣就允許Activity或者FragmentActivity在后台查詢數據時可以和用戶交互。

 

定義一個使用CursorLoader的窗口

         為了能夠在Activity或者FragmentActivity中使用CursorLoader,需要使用LoaderCallbacks<Cursor>接口,CursorLoader引用接口定義的回調和類進行交互;本課和下一課將詳細描述每一個回調。

         比如,下面的實例向你展示如何使用依賴庫中的CursorLoader定義FragmentActivity。通過擴展FragmentActivity,可以達到通過Fragment使用CursorLoader一樣的效果。

 
  1. <span style="font-size:18px">public class PhotoThumbnailFragment extends FragmentActivity implements  
  2.         LoaderManager.LoaderCallbacks<Cursor> {  
  3. ...  
  4. }  
  5. </span>  

初始化查詢操作

         為了初始化查詢操作,你需要調用LoadManager的initLoader()方法,它初始化后台框架,你可以在用戶進入查詢的數據后執行該操作,或者,如果你不需要任何數據,你可以在onCreate()或者onCreateView()中執行該操作,比如:

 
  1. <span style="font-size:18px">// Identifies a particular Loader being used in this component  
  2.     private static final int URL_LOADER = 0;  
  3.     ...  
  4.     /* When the system is ready for the Fragment to appear, this displays 
  5.      * the Fragment's View 
  6.      */  
  7.     public View onCreateView(  
  8.             LayoutInflater inflater,  
  9.             ViewGroup viewGroup,  
  10.             Bundle bundle) {  
  11.         ...  
  12.         /* 
  13.          * Initializes the CursorLoader. The URL_LOADER value is eventually passed 
  14.          * to onCreateLoader(). 
  15.          */  
  16.         getLoaderManager().initLoader(URL_LOADER, nullthis);  
  17.         ...  
  18.     }</span>  

注意:getLoaderManager()方法僅僅適用於Fragment類,為了能夠在FragmentActivity中獲取LoaderManager,需通過調用getSupportLoaderManager()。

 

開始查詢

         為了能夠盡快地初始化后台框架,系統會調用你類中的onCreateLoader()方法,為了能夠開始查詢,需要從該方法中反饋一個CursorLoader對象。你可以初始化一個空的CursorLoader對象然后通過它來定義查詢操作,或者你可以在初始化對象的同時定義查詢操作。

 
  1. <span style="font-size:18px">/* 
  2. * Callback that's invoked when the system has initialized the Loader and 
  3. * is ready to start the query. This usually happens when initLoader() is 
  4. * called. The loaderID argument contains the ID value passed to the 
  5. * initLoader() call. 
  6. */  
  7. @Override  
  8. public Loader<Cursor> onCreateLoader(int loaderID, Bundle bundle)  
  9. {  
  10.     /* 
  11.      * Takes action based on the ID of the Loader that's being created 
  12.      */  
  13.     switch (loaderID) {  
  14.         case URL_LOADER:  
  15.             // Returns a new CursorLoader  
  16.             return new CursorLoader(  
  17.                         getActivity(),   // Parent activity context  
  18.                         mDataUrl,        // Table to query  
  19.                         mProjection,     // Projection to return  
  20.                         null,            // No selection clause  
  21.                         null,            // No selection arguments  
  22.                         null             // Default sort order  
  23.         );  
  24.         default:  
  25.             // An invalid id was passed in  
  26.             return null;  
  27.     }  
  28. }  
  29. </span>  

         一旦生成了后台框架的對象,系統就會開始在后台執行查詢操作,當查詢操作執行完成后,后台框架會調用onLoadFinished()方法,這將在下一個作詳細討論。

 

處理查詢結果

         正如前面課程所述,你應該在你所實現類的onCreateLoader()方法中通過CursorLoader加載你的數據,加載器會在你Acitivity或者FragmentActivity的LoaderCallbacks.onLoadFinished()方法中返回查詢結果。該方法的其中一個入參為包含查詢結果的游標。你可以使用這個對象來更新你的數據或者做其它操作。

         除了onCreateLoader()和onLoadFinished()方法,你還需要實現onLoaderReset()方法,這個方法會在數據更新更新時被調用,當數據變化時,框架會重新執行當前的查詢操作。

 

處理查詢結果

         為了顯示從CursorLoader返回的游標數據,你需要自定義一個繼承於AdapterView的視圖,並為這個視圖定義一個繼承於CursorAdapter的適配器。然后系統會自動地將數據從游標移到視圖。

         在你顯示任何數據之前你可以為視圖和適配器建立連接,然后再onLoadFinished()方法中將游標移到適配器。當你將游標移到適配器后,系統會自動地更新視圖。在游標的數據有改動時同樣會更新視圖。

         比如:

 
  1. <span style="font-size:18px">public String[] mFromColumns = {  
  2.     DataProviderContract.IMAGE_PICTURENAME_COLUMN  
  3. };  
  4. public int[] mToFields = {  
  5.     R.id.PictureName  
  6. };  
  7. // Gets a handle to a List View  
  8. ListView mListView = (ListView) findViewById(R.id.dataList);  
  9. /* 
  10.  * Defines a SimpleCursorAdapter for the ListView 
  11.  * 
  12.  */  
  13. SimpleCursorAdapter mAdapter =  
  14.     new SimpleCursorAdapter(  
  15.             this,                // Current context  
  16.             R.layout.list_item,  // Layout for a single row  
  17.             null,                // No Cursor yet  
  18.             mFromColumns,        // Cursor columns to use  
  19.             mToFields,           // Layout fields to use  
  20.             0                    // No flags  
  21.     );  
  22. // Sets the adapter for the view  
  23. mListView.setAdapter(mAdapter);  
  24. ...  
  25. /* 
  26.  * Defines the callback that CursorLoader calls 
  27.  * when it's finished its query 
  28.  */  
  29. @Override  
  30. public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {  
  31.     ...  
  32.     /* 
  33.      * Moves the query results into the adapter, causing the 
  34.      * ListView fronting this adapter to re-display 
  35.      */  
  36.     mAdapter.changeCursor(cursor);  
  37. }</span>  

 

刪除舊的游標信息

         當游標非法時CursorLoader會被重置,這多數情況發生在游標的數據有改動時,在重新執行查詢操作前,框架會調用你所實現的onLoaderReset()方法。在這個回調中,你應該刪除當前游標的所有信息來避免內存泄露。一旦結束回調onLoaderReset()方法后,CursorLoader會重新執行查詢操作。

         比如:

 
  1. <span style="font-size:18px">/* 
  2.  * Invoked when the CursorLoader is being reset. For example, this is 
  3.  * called if the data in the provider changes and the Cursor becomes stale. 
  4.  */  
  5. @Override  
  6. public void onLoaderReset(Loader<Cursor> loader) {  
  7.       
  8.     /* 
  9.      * Clears out the adapter's reference to the Cursor. 
  10.      * This prevents memory leaks. 
  11.      */  
  12.     mAdapter.changeCursor(null);  
  13. }  
  14. </span>  

管理設備的激活狀態

         當一個android設備處於空閑狀態時,它首先會變暗,然后會關屏,最終會讓CPU停止工作。這樣處理是為了避免設備的電池被快速地耗盡,然而有些時候你的應用需要一些不同的表現:

(1)游戲或者電影應用可能需要保持屏幕常亮;

(2)有些應用雖然不需要屏幕常亮,但在CPU執行完核心操作之前同樣需要保持程序運行。

本課的目的是告訴你在避免電池被快速耗盡的情況下如何保持設備處於激活狀態。

保持設備處於激活狀態

         為了避免電池被耗盡,android設備會在處於空閑狀態時立即切換到休眠狀態。然而,有些時候一個應用需要保持屏幕常亮或者CPU直到某些事情被處理完成。

         你該采取什么操作取決於你應用的需求。然而,通用的規則是你應該使用最輕量級的操作來處理你的應用程序,使你的應用減少對系統資源的占用。下面將向你描述通過怎樣地操作來使得你對應用的處理和系統默認的休眠行為相容。

 

保持屏幕常亮

         某些應用需要保持屏幕常亮,比如游戲或者電影應用。最好的方式是在你的窗口中使用FLAG_KEEP_SCREEN_ON屬性(只在一個窗口,絕不是在一個服務或者其它應用組件中),比如:

 
  1. <span style="font-size:18px">public class MainActivity extends Activity {  
  2.   @Override  
  3.   protected void onCreate(Bundle savedInstanceState) {  
  4.     super.onCreate(savedInstanceState);  
  5.     setContentView(R.layout.activity_main);  
  6.     getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);  
  7.   }  
  8. </span>  

         這種先進的處理方式有別於喚醒鎖,它不需要特殊的權限,平台會正確地管理應用之間的切換,你不需要擔心自己的應用沒有釋放沒有使用的資源。

         實現該功能的另一種思路是在你應用xml文件中使用android:keepScreenOn屬性:

 
  1. <span style="font-size:18px"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="match_parent"  
  3.     android:layout_height="match_parent"  
  4.     android:keepScreenOn="true">  
  5.     ...  
  6. </RelativeLayout>  
  7. </span>  

         使用andorid:keepScreenOn=”true”和使用FLAG_KEEP_SCREEN_ON是一樣的效果。你可以使用適合於你應用的任何一種方式。通過在程序中設置你窗口常亮狀態的優勢是:它可以清楚這個標志,從而可以關閉屏幕。

 

保持CPU持續工作

         如果你希望在設備休眠之前CPU能夠完成需要處理的工作,你可以使用一個叫喚醒鎖的PowerManager系統服務。喚醒鎖允許你的應用可以控制主機設備電源的狀態。

         創建和保持喚醒鎖會在一定程度上對電池的壽命有所影響,因此你應該只在非常有必要的情況下使用它,並盡量控制使用時間。比如,你絕不應該在一個窗口中使用喚醒鎖,正如上面所描述的那樣,如果你想保持當前窗口的屏幕常亮,你可以使用FLAG_KEEP_SCREEN_ON。

         應該使用喚醒鎖的情況可能就是后台服務在屏幕關閉時需要通過喚醒鎖保持CPU持續工作。再次聲明,盡量限制它的使用時間,因為它會影響到電池的壽命。

         為了使用喚醒鎖,首先需要在清單文件中添加WAKE_LOCK權限:

 
  1. <span style="font-size:18px"><uses-permission android:name="android.permission.WAKE_LOCK" /></span>  

         如果你的應用包含一個使用服務處理某些事情的廣播接收器,你可以通過WakefulBroadcastReceiver來管理你的喚醒鎖,正如使用WakefulBroadcastReceiver一課中所描述的那樣,這是一個比較好的處理方式。如果你的應用沒有遵循這種方式,通過下面的代碼你可以直接設置喚醒鎖:

 
  1. <span style="font-size:18px">PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);  
  2. Wakelock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,  
  3.         "MyWakelockTag");  
  4. wakeLock.acquire();  
  5. </span>  

         為了釋放喚醒鎖,你需要調用wakelock的release()方法。它將釋放你對CPU的聲明,在你的應用結束工作后盡快關閉喚醒鎖避免電池被耗盡。

 

使用WakefulBroadcastReceiver

         使用廣播接收器和服務可以讓你很好地管理后台任務的生命周期。

         一個WakefulBroadcastReceiver是廣播接收器的一個特殊類型,它可以創建和管理你應用的PARITAL_WAKE_LOCK。一個WakeBroadcastReceiver接收到廣播后將工作傳遞給Service(一個典型的IntentService),直到確保設備沒有休眠。如果你在交接工作給服務的時候沒有保持喚醒鎖,在工作還沒完成之前就允許設備休眠的話,將會出現一些你不願意看到的情況。

         要使用WakefulBroadcastReceiver的第一步是在清單文件中添加它,和其它廣播接收器是一樣的:

 
  1. <span style="font-size:18px"><receiver android:name=".MyWakefulReceiver"></receiver></span>  

         接下來是在代碼中通過startWakefulService()來啟動MyIntentService。和starService()方法相比,除了在服務啟動時可以保持喚醒鎖外,通過startWakefulService()方法傳遞的Intent可以保持一個額外的喚醒鎖:

 
  1. <span style="font-size:18px">public class MyWakefulReceiver extends WakefulBroadcastReceiver {  
  2.   
  3.     @Override  
  4.     public void onReceive(Context context, Intent intent) {  
  5.   
  6.         // Start the service, keeping the device awake while the service is  
  7.         // launching. This is the Intent to deliver to the service.  
  8.         Intent service = new Intent(context, MyIntentService.class);  
  9.         startWakefulService(context, service);  
  10.     }  
  11. }</span>  

         當服務執行完成后,系統會調用MyWakefulReceiver的completeWakefulIntent()方法來釋放喚醒鎖,completeWakefulIntent()方法攜帶的參數是從WakefulBroadcastReceiver傳遞過來的intent:

 

  1. <span style="font-size:18px">public class MyIntentService extends IntentService {  
  2.     public static final int NOTIFICATION_ID = 1;  
  3.     private NotificationManager mNotificationManager;  
  4.     NotificationCompat.Builder builder;  
  5.     public MyIntentService() {  
  6.         super("MyIntentService");  
  7.     }  
  8.     @Override  
  9.     protected void onHandleIntent(Intent intent) {  
  10.         Bundle extras = intent.getExtras();  
  11.         // Do the work that requires your app to keep the CPU running.  
  12.         // ...  
  13.         // Release the wake lock provided by the WakefulBroadcastReceiver.  
  14.         MyWakefulReceiver.completeWakefulIntent(intent);  
  15.     }  
  16. }</span>  

 

原文:http://developer.android.com/training/best-background.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM