国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
android筆記

[coolxing按: 轉(zhuǎn)載請(qǐng)注明作者和出處, 如有謬誤, 歡迎在評(píng)論中指正.]

 

Service是android中的服務(wù)組件, 經(jīng)常用來執(zhí)行一些運(yùn)行在后臺(tái)的耗時(shí)操作. 使用一個(gè)Service需要繼承Service類, 并根據(jù)需要重寫生命周期方法. Service的生命周期如下:

|-- public abstract IBinder onBind (Intent intent): 該方法是一個(gè)抽象方法, 因此Service子類必須實(shí)現(xiàn)這個(gè)方法. 它返回一個(gè)IBinder對(duì)象, 應(yīng)用程序可以通過這個(gè)對(duì)象與Service組件通信(關(guān)于這一點(diǎn), 其后會(huì)有詳細(xì)的講解), 以bindService()方式啟動(dòng)Service時(shí)回調(diào)該方法.

|-- public void onCreate (): 當(dāng)Service第一次被創(chuàng)建后回調(diào)的方法.

|-- public void onDestroy (): Service關(guān)閉之前回調(diào)的方法.

|-- public int onStartCommand (Intent intent, int flags, int startId): 以startService(Intent intent)的方式啟動(dòng)Service時(shí), 系統(tǒng)都會(huì)回調(diào)該方法.

|-- public boolean onUnbind (Intent intent): 當(dāng)所有綁定該Service的組件都斷開連接時(shí)回調(diào)該方法.

 

從圖中可以看出, Service可以有兩種啟動(dòng)方式:

1. 以startService(Intent intent)的方式啟動(dòng). 此時(shí)啟動(dòng)的Service與調(diào)用者之間沒有關(guān)聯(lián), 即使調(diào)用者已經(jīng)退出, Service仍然可以繼續(xù)運(yùn)行, 而且調(diào)用者和Service之間無法進(jìn)行數(shù)據(jù)交換和通信. 如果需要停止Service的運(yùn)行, 只能調(diào)用Context類的stopService(intent)方法, 或者由Service本身調(diào)用其stopSelf()等方法.

2. 以bindService(Intent service, ServiceConnection conn, int flags)的方式啟動(dòng).

此時(shí)調(diào)用者與Service綁定在一起, 如果調(diào)用者退出, 則Service也隨之退出, 而且調(diào)用者和Service之間可以進(jìn)行數(shù)據(jù)交換或通信.

根據(jù)調(diào)用者和Service是否在一個(gè)應(yīng)用程序內(nèi), 可以將調(diào)用者和Service之間的通信分為進(jìn)程內(nèi)通信和進(jìn)程間通信.

 

 

a. 進(jìn)程內(nèi)通信. bindService(Intent service, ServiceConnection conn, int flags)方法的第二個(gè)參數(shù)為ServiceConnection對(duì)象, 最后一個(gè)參數(shù)通??梢允荢ervice.BIND_AUTO_CREATE. ServiceConnection是一個(gè)接口, 該接口包含2個(gè)方法:

|-- onServiceConnected(ComponentName name, IBinder service): 該方法在調(diào)用者和Service成功綁定之后由系統(tǒng)回調(diào).

方法中的第一個(gè)參數(shù)ComponentName是所綁定的Service的組件名稱, 而IBinder對(duì)象就是Service中onBinder()方法的返回值. 要實(shí)現(xiàn)調(diào)用者和Service之間的通信, 只需要調(diào)用IBinder對(duì)象中定義的方法即可.

|-- onServiceDisconnected(ComponentName name): 該方法在調(diào)用者解除和Service的綁定之后由系統(tǒng)回調(diào).

以下是利用Service實(shí)現(xiàn)進(jìn)程內(nèi)通信的一個(gè)例子.

首先自定義Service類:

 

Java代碼
 
  1. public class MyService extends Service {   
  2.   
  3.     public class MyBinder extends Binder {   
  4.         /**  
  5.          * 獲取Service的運(yùn)行時(shí)間  
  6.          * @return  
  7.          */  
  8.         public long getServiceRunTime() {   
  9.             return System.currentTimeMillis() - startTime;   
  10.         }   
  11.     }   
  12.        
  13.     private long startTime;   
  14.        
  15.     /**  
  16.      * MyBinder是Binder的子類, 而Binder實(shí)現(xiàn)了IBinder接口.  
  17.      */  
  18.     @Override  
  19.     public IBinder onBind(Intent intent) {   
  20.         return new MyBinder();   
  21.     }   
  22.        
  23.     @Override  
  24.     public void onCreate() {   
  25.         super.onCreate();   
  26.         startTime = System.currentTimeMillis();   
  27.     }   
  28. }  
 

然后在activity中綁定上述的Service:

 

Java代碼
 
  1. public class MainActivity extends Activity {   
  2.     private MyBinder binder = null;   
  3.   
  4.     /** Called when the activity is first created. */  
  5.     @Override  
  6.     public void onCreate(Bundle savedInstanceState) {   
  7.         super.onCreate(savedInstanceState);   
  8.         setContentView(R.layout.main);   
  9.   
  10.         // 創(chuàng)建一個(gè)指向MyService的intent   
  11.         Intent intent = new Intent("cn.xing.action.my_service");   
  12.         this.bindService(intent, new MyServiceConnection(),   
  13.                 Service.BIND_AUTO_CREATE);   
  14.   
  15.         Button button = (Button) this.findViewById(R.id.button);   
  16.         button.setOnClickListener(new View.OnClickListener() {   
  17.             @Override  
  18.             public void onClick(View v) {   
  19.                 if (binder != null) {   
  20.                     Toast.makeText(getApplicationContext(), "MyService已經(jīng)運(yùn)行了" + binder.getServiceRunTime()   
  21.                                     + "毫秒", Toast.LENGTH_LONG).show();   
  22.                 }    
  23.             }   
  24.         });   
  25.     }   
  26.   
  27.     /**  
  28.      * 實(shí)現(xiàn)ServiceConnection接口  
  29.      *   
  30.      * @author xing  
  31.      *   
  32.      */  
  33.     private final class MyServiceConnection implements ServiceConnection {   
  34.         /**  
  35.          * 和MyService綁定時(shí)系統(tǒng)回調(diào)這個(gè)方法  
  36.          */  
  37.         @Override  
  38.         public void onServiceConnected(ComponentName name, IBinder service) {   
  39.             // MyService中的onBinder()方法的返回值實(shí)際上是一個(gè)MyBinder對(duì)象, 因此可以使用強(qiáng)制轉(zhuǎn)換.   
  40.             binder = (MyBinder) service;   
  41.         }   
  42.   
  43.         /**  
  44.          * 解除和MyService的綁定時(shí)系統(tǒng)回調(diào)這個(gè)方法  
  45.          */  
  46.         @Override  
  47.         public void onServiceDisconnected(ComponentName name) {   
  48.             // 解除和MyService的綁定后, 將binder設(shè)置為null.   
  49.             binder = null;   
  50.         }   
  51.   
  52.     }   
  53. }  
 

b.進(jìn)程間通信. 調(diào)用者和Service如果不在一個(gè)進(jìn)程內(nèi), 就需要使用android中的遠(yuǎn)程Service調(diào)用機(jī)制.

android使用AIDL定義進(jìn)程間的通信接口. AIDL的語法與java接口類似, 需要注意以下幾點(diǎn):

1. AIDL文件必須以.aidl作為后綴名.

2. AIDL接口中用到的數(shù)據(jù)類型, 除了基本類型, String, List, Map, CharSequence之外, 其他類型都需要導(dǎo)包, 即使兩種在同一個(gè)包內(nèi). List和Map中的元素類型必須是AIDL支持的類型.

3. 接口名需要和文件名相同.

4. 方法的參數(shù)或返回值是自定義類型時(shí), 該自定義的類型必須實(shí)現(xiàn)了Parcelable接口.

5. 所有非java基本類型參數(shù)都需要加上in, out, inout標(biāo)記, 以表明參數(shù)是輸入?yún)?shù), 輸出參數(shù), 還是輸入輸出參數(shù).

6. 接口和方法前不能使用訪問修飾符和static, final等修飾.

 

下面通過一個(gè)例子來演示android遠(yuǎn)程Service調(diào)用機(jī)制的各個(gè)步驟:

1. 創(chuàng)建remoteService項(xiàng)目.

2. 在cn.xing.remoteservice包下定義aidl文件--IRemoteService.aidl:

 

Java代碼
 
  1. package cn.xing.remoteservice;   
  2. interface IRemoteService{   
  3.     int getServiceRunTime();   
  4. }  

Eclipse的ADT插件會(huì)在gen目錄的cn.xing.remoteservice包下自動(dòng)根據(jù)aidl文件生成IRemoteService接口. 

接口中有一個(gè)static的抽象內(nèi)部類Stub, Stub類繼承了Binder類并實(shí)現(xiàn)了IRemoteService接口. Stub類有如下的靜態(tài)方法:

public static cn.xing.remoteservice.IRemoteService asInterface(android.os.IBinder obj)

該方法接受一個(gè)IBinder對(duì)象, 返回值是IRemoteService的instance, 通過這個(gè)instance我們就可以調(diào)用IRemoteService中定義的方法了.

3. 在remoteService項(xiàng)目的cn.xing.remoteservice包下創(chuàng)建遠(yuǎn)程服務(wù)類RemoteService:

 

Java代碼
 
  1. public class RemoteService extends Service {   
  2.     private long startTime;   
  3.        
  4.     /**  
  5.      * IRemoteService.Stub類實(shí)現(xiàn)了IBinder和IRemoteService接口  
  6.      * 因此Stub的子類對(duì)象可以作為onBinder()方法的返回值.  
  7.      * @author xing  
  8.      *  
  9.      */  
  10.     public class MyBinder extends IRemoteService.Stub {   
  11.         @Override  
  12.         public long getServiceRunTime() throws RemoteException {   
  13.             return System.currentTimeMillis() - startTime;   
  14.         }   
  15.     };   
  16.        
  17.     @Override  
  18.     public IBinder onBind(Intent intent) {   
  19.         return new MyBinder();   
  20.     }   
  21.        
  22.     @Override  
  23.     public void onCreate() {   
  24.         super.onCreate();   
  25.         startTime = System.currentTimeMillis();   
  26.     }   
  27. }  

4. 創(chuàng)建一個(gè)新的android項(xiàng)目invokeRemoteService, 并復(fù)制remoteService項(xiàng)目中的aidl文件(連同包結(jié)構(gòu)一起復(fù)制)到invokeRemoteService項(xiàng)目中.

5. 在invokeRemoteService項(xiàng)目中綁定遠(yuǎn)程服務(wù), 并調(diào)用遠(yuǎn)程方法.

 

Java代碼
 
  1. public class MainActivity extends Activity {   
  2.     private IRemoteService iRemoteService = null;   
  3.   
  4.     /** Called when the activity is first created. */  
  5.     @Override  
  6.     public void onCreate(Bundle savedInstanceState) {   
  7.         super.onCreate(savedInstanceState);   
  8.         setContentView(R.layout.main);   
  9.   
  10.         // 創(chuàng)建一個(gè)指向RemoteService的intent   
  11.         Intent intent = new Intent("cn.xing.action.remote_service");   
  12.         this.bindService(intent, new MyServiceConnection(),   
  13.                 Service.BIND_AUTO_CREATE);   
  14.   
  15.         Button button = (Button) this.findViewById(R.id.button);   
  16.         button.setOnClickListener(new View.OnClickListener() {   
  17.             @Override  
  18.             public void onClick(View v) {   
  19.                 if (iRemoteService != null) {   
  20.                     try {   
  21.                         Toast.makeText(getApplicationContext(), "MyService已經(jīng)運(yùn)行了" + iRemoteService.getServiceRunTime()   
  22.                                         + "毫秒", Toast.LENGTH_LONG).show();   
  23.                     } catch (RemoteException e) {   
  24.                         e.printStackTrace();   
  25.                     }   
  26.                 }    
  27.             }   
  28.         });   
  29.     }   
  30.   
  31.     /**  
  32.      * 實(shí)現(xiàn)ServiceConnection接口  
  33.      *   
  34.      * @author xing  
  35.      *   
  36.      */  
  37.     private final class MyServiceConnection implements ServiceConnection {   
  38.         /**  
  39.          * 和RemoteService綁定時(shí)系統(tǒng)回調(diào)這個(gè)方法  
  40.          */  
  41.         @Override  
  42.         public void onServiceConnected(ComponentName name, IBinder service) {   
  43.             // 此處不能使用強(qiáng)制轉(zhuǎn)換, 應(yīng)該調(diào)用Stub類的靜態(tài)方法獲得IRemoteService接口的實(shí)例對(duì)象   
  44.             iRemoteService = IRemoteService.Stub.asInterface(service);   
  45.         }   
  46.   
  47.         /**  
  48.          * 解除和RemoteService的綁定時(shí)系統(tǒng)回調(diào)這個(gè)方法  
  49.          */  
  50.         @Override  
  51.         public void onServiceDisconnected(ComponentName name) {   
  52.             // 解除和RemoteService的綁定后, 將iRemoteService設(shè)置為null.   
  53.             iRemoteService = null;   
  54.         }   
  55.     }   
  56. }  

 

附件中包含進(jìn)程內(nèi)通信和進(jìn)程間通信例子的源碼, 需要的朋友可自行下載查看.

 

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Binder機(jī)制,從Java到C (1. IPC in Application Remote Service)
Android流氓代碼塊(親測可行)
android service入門
探討一種新型的雙進(jìn)程守護(hù)應(yīng)用保活方法
android自定義一個(gè)本地服務(wù)的方法
Android 綁定Service 實(shí)現(xiàn)android控制service的生命周期
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服