Service組件

   Service組件能夠看做是沒有界面的Activity組件,兩者地位相同。它是運行在系統後臺的一種服務,通常處理耗時較長的操做,不與用戶進行交互。和其餘組件同樣,Service組件一樣須要在AndroidManifest.xml中聲明,在<service>中能夠添加過濾器指定如何如何訪問該Service。android

 

      
      
               
      
      
  1. <service android:name="MyService">  
  2.             <intent-filter>  
  3.      <action android:name="com.amaker.ch07.app.action.MY_SERVICE"/>  
  4.             </intent-filter>  
  5.         </service>  

  Service和整個應用程序在同一個進程和同一個線程中,並不是單獨的進程和線程。它的運行會阻塞其餘操做。
  Service的用處有兩點,其一是在後臺運行耗時較長且不與用戶進行交互的操做;其二是經過綁定,與其餘進程進行通訊。

1. 建立Service
  首先須要定義一個繼承於Service的類,而後覆蓋其中的的方法便可。代碼以下:
app

 

      
      
               
      
      
  1. public class MyService extends Service{  
  2.  
  3. // 能夠返回null,一般返回一個有aidl定義的接口,必須實現  
  4. public IBinder onBind(Intent intent) {  
  5. Log.i("SERVICE""onBind..............");  
  6. Toast.makeText(MyService.this"onBind..............", Toast.LENGTH_LONG).show();  
  7. return null;  
  8. }  
  9. // Service建立時調用  
  10. public void onCreate() {  
  11. Log.i("SERVICE""onCreate..............");  
  12. Toast.makeText(MyService.this"onCreate..............", Toast.LENGTH_LONG).show();  
  13. }  
  14. // 當客戶端調用startService()方法啓動Service時,該方法被調用  
  15. public void onStartCommond(Intent intent, int flag, int startId) {  
  16. Log.i("SERVICE""onStart..............");  
  17. Toast.makeText(MyService.this"onStart..............", Toast.LENGTH_LONG).show();  
  18. }  
  19. // 當Service再也不使用時調用  
  20. public void onDestroy() {  
  21. Log.i("SERVICE""onDestroy..............");  
  22. Toast.makeText(MyService.this"onDestroy..............", Toast.LENGTH_LONG).show();  
  23. }  
  24. }  

2. 啓動和中止Service
  一旦定義好一個Service,就能夠在其餘組件中啓動並中止該Service了。代碼以下:
ide

 

      
      
               
      
      
  1. //啓動Service
  2. // 建立Intent  
  3. Intent intent = new Intent();  
  4. // 設置Action屬性  
  5. intent.setAction("com.amaker.ch07.app.action.MY_SERVICE");  
  6. // 啓動該Service  
  7. startService(intent);  
  8.  
  9.  // 中止Service
  10. // 建立Intent  
  11. Intent intent = new Intent();  
  12. // 設置Action屬性  
  13. intent.setAction("com.amaker.ch07.app.action.MY_SERVICE");  
  14. // 啓動該Service  
  15. stopService(intent);  
  16.  

3. 綁定一個已經存在的Service
  綁定Service和啓動它的區別在於,綁定服務通常用於遠程調用。若是服務沒有被建立,首先會調用onCreate方法,可是不會調用onStrart方法,而是調用onBind返回客戶端一個IBind接口。
  綁定服務的代碼以下:
this

 

      
      
               
      
      
  1. // 鏈接對象  
  2. private ServiceConnection conn = new ServiceConnection() {  
  3. @Override 
  4. public void onServiceConnected(ComponentName name, IBinder service) {  
  5. Log.i("SERVICE""鏈接成功!");  
  6. Toast.makeText(MainActivity.this"鏈接成功!", Toast.LENGTH_LONG).show();  
  7. }  
  8. @Override 
  9. public void onServiceDisconnected(ComponentName name) {  
  10. Log.i("SERVICE""斷開鏈接!");  
  11. Toast.makeText(MainActivity.this"斷開鏈接!", Toast.LENGTH_LONG).show();  
  12. }  
  13. };  
  14.  
  15. // 綁定Service監聽器  
  16. private OnClickListener bindListener = new OnClickListener() {  
  17. @Override 
  18. public void onClick(View v) {  
  19. // 建立Intent  
  20. Intent intent = new Intent();  
  21. // 設置Action屬性  
  22. intent.setAction("com.amaker.ch07.app.action.MY_SERVICE");  
  23.  
  24. // 綁定Service  
  25. bindService(intent, conn, Service.BIND_AUTO_CREATE);  
  26. }  
  27. };  
  28.  
  29.  
  30. // 解除綁定Service監聽器  
  31. private OnClickListener unBindListener = new OnClickListener() {  
  32. @Override 
  33. public void onClick(View v) {  
  34. // 建立Intent  
  35. Intent intent = new Intent();  
  36. // 設置Action屬性  
  37. intent.setAction("com.amaker.ch07.app.action.MY_SERVICE");  
  38. // 解除綁定Service  
  39. unbindService(conn);  
  40. }  
  41. };  
  42.  
  43. }  
 
相關文章
相關標籤/搜索