android 中IntentService的做用及使用

 

  IntentService是繼承於Service並處理異步請求的一個類,在IntentService內有一個工做線程來處理耗時操做,啓動IntentService的方式和啓動傳統Service同樣,同時,當任務執行完後,IntentService會自動中止,而不須要咱們去手動控制。另外,能夠啓動IntentService屢次,而每個耗時操做會以工做隊列的方式在IntentService的onHandleIntent回調方法中執行,而且,每次只會執行一個工做線程,執行完第一個再執行第二個,以此類推。java

IntentService與Service的不一樣:android

(1)直接 建立一個默認的工做線程,該線程執行全部的intent傳遞給onStartCommand()區別於應用程序的主線程。多線程

(2)直接建立一個工做隊列,將一個意圖傳遞給你onHandleIntent()的實現,因此咱們就永遠沒必要擔憂多線程。異步

(3)當請求完成後本身會調用stopSelf(),因此你就不用調用該方法了。ide

(4)提供的默認實現onBind()返回null,因此也不須要重寫這個方法。so easy啊函數

(5)提供了一個默認實現onStartCommand(),將意圖工做隊列,而後發送到你onHandleIntent()實現。真是太方便了spa

咱們須要作的就是實現onHandlerIntent()方法,還有一點就是常常被遺忘的,構造函數是必需的。線程

簡單說呢?第一,咱們省去了在Service中手動開線程的麻煩,第二,當操做完成時,咱們不用手動中止Service,第三,it's so easy to use!code

 

接下來讓咱們來看看如何使用,我寫了一個Demo來模擬兩個耗時操做,Operation1與Operation2,先執行1,2必須等1執行完才能執行:blog

新建工程,新建一個繼承IntentService的類,我這裏是IntentServiceDemo.java

 1 public class IntentServiceDemo extends IntentService {
 2 
 3     public IntentServiceDemo() {
 4         //必須實現父類的構造方法
 5         super("IntentServiceDemo");
 6     }
 7     
 8     @Override
 9     public IBinder onBind(Intent intent) {
10         System.out.println("onBind");
11         return super.onBind(intent);
12     }
13 
14 
15     @Override
16     public void onCreate() {
17         System.out.println("onCreate");
18         super.onCreate();
19     }
20 
21     @Override
22     public void onStart(Intent intent, int startId) {
23         System.out.println("onStart");
24         super.onStart(intent, startId);
25     }
26 
27 
28     @Override
29     public int onStartCommand(Intent intent, int flags, int startId) {
30         System.out.println("onStartCommand");
31         return super.onStartCommand(intent, flags, startId);
32     }
33 
34 
35     @Override
36     public void setIntentRedelivery(boolean enabled) {
37         super.setIntentRedelivery(enabled);
38         System.out.println("setIntentRedelivery");
39     }
40 
41     @Override
42     protected void onHandleIntent(Intent intent) {
43         //Intent是從Activity發過來的,攜帶識別參數,根據參數不一樣執行不一樣的任務
44         String action = intent.getExtras().getString("param");
45         if (action.equals("oper1")) {
46             System.out.println("Operation1");
47         }else if (action.equals("oper2")) {
48             System.out.println("Operation2");
49         }
50         
51         try {
52             Thread.sleep(2000);
53         } catch (InterruptedException e) {
54             e.printStackTrace();
55         }
56     }
57 
58     @Override
59     public void onDestroy() {
60         System.out.println("onDestroy");
61         super.onDestroy();
62     }
63 
64 }

我把生命週期方法全打印出來了,待會咱們來看看它執行的過程是怎樣的。接下來是Activity,在Activity中來啓動IntentService:

 

 1 public class TestActivity extends Activity {
 2     /** Called when the activity is first created. */
 3     @Override
 4     public void onCreate(Bundle savedInstanceState) {
 5         super.onCreate(savedInstanceState);
 6         setContentView(R.layout.main);
 7         
 8         //能夠啓動屢次,每啓動一次,就會新建一個work thread,但IntentService的實例始終只有一個
 9         //Operation 1
10         Intent startServiceIntent = new Intent("com.test.intentservice");
11         Bundle bundle = new Bundle();
12         bundle.putString("param", "oper1");
13         startServiceIntent.putExtras(bundle);
14         startService(startServiceIntent);
15         
16         //Operation 2
17         Intent startServiceIntent2 = new Intent("com.test.intentservice");
18         Bundle bundle2 = new Bundle();
19         bundle2.putString("param", "oper2");
20         startServiceIntent2.putExtras(bundle2);
21         startService(startServiceIntent2);
22     }
23 }

 

最後記得在Android中進行註冊,

1 <service android:name=".IntentServiceDemo">
2             <intent-filter >
3                 <action android:name="com.test.intentservice"/>
4             </intent-filter>
5         </service>

 

咱們來看看結果:

從結果能夠看到,onCreate方法只執行了一次,而onStartCommand和onStart方法執行了兩次,開啓了兩個Work Thread,這就證明了以前所說的,啓動屢次,但IntentService的實例只有一個,這跟傳統的Service是同樣的。Operation1也是先於Operation2打印,而且我讓兩個操做間停頓了2s,最後是onDestroy銷燬了IntentService。

 

這就是IntentService,一個方便咱們處理業務流程的類,它是一個Service,可是比Service更智能。

相關文章
相關標籤/搜索