现在的位置: 首页 > 移动开发 > 正文

Android中AIDL实现(跨进程通信)

2019年10月10日 移动开发 ⁄ 共 7109字 ⁄ 字号 评论关闭

一. 概述:

 

     跨进程通信(AIDL),主要实现进程(应用)间数据共享功能。

 

二. 实现流程:

 

     1. 服务器端实现:

 

        (1)目录结构,如下图:

                AIDL服务器端工程

        (2)实现*.aidl文件:

 

                A. IAIDLService.aidl实现:

  1. package com.focus.aidl;  
  2.   
  3. import com.focus.aidl.Person;  
  4.   
  5. interface IAIDLService {  
  6.   
  7.     String getName();  
  8.   
  9.     Person getPerson();  
  10.   
  11. }  

 

                B. Person.aidl实现:

  1. parcelable Person;  

 

        (3)进程间传递对象必需实现Parcelable或Serializable接口,下面是被传递的Person对象实现:

  1. package com.focus.aidl;  
  2.   
  3. import android.os.Parcel;  
  4. import android.os.Parcelable;  
  5.   
  6. public class Person implements Parcelable {  
  7.   
  8.     private String name;  
  9.       
  10.     private int age;  
  11.   
  12.     public Person() {  
  13.           
  14.     }  
  15.       
  16.     public Person(Parcel source) {  
  17.         name = source.readString();  
  18.         age = source.readInt();  
  19.     }  
  20.       
  21.     public String getName() {  
  22.         return name;  
  23.     }  
  24.   
  25.     public void setName(String name) {  
  26.         this.name = name;  
  27.     }  
  28.   
  29.     public int getAge() {  
  30.         return age;  
  31.     }  
  32.   
  33.     public void setAge(int age) {  
  34.         this.age = age;  
  35.     }  
  36.   
  37.     public int describeContents() {  
  38.         return 0;  
  39.     }  
  40.   
  41.     public void writeToParcel(Parcel dest, int flags) {  
  42.         dest.writeString(name);  
  43.         dest.writeInt(age);  
  44.     }  
  45.       
  46.     public static final Parcelable.Creator<Person> CREATOR = new Creator<Person>() {  
  47.         public Person[] newArray(int size) {  
  48.             return new Person[size];  
  49.         }  
  50.           
  51.         public Person createFromParcel(Parcel source) {  
  52.             return new Person(source);  
  53.         }  
  54.     };  
  55.       
  56. }  

 

        (4)实现IAIDLService.aidl文件中定义的接口,并定义Service,在Service被bind时返回此实现类:

  1. package com.focus.aidl;  
  2.   
  3. import com.focus.aidl.IAIDLService.Stub;  
  4.   
  5. import android.app.Service;  
  6. import android.content.Intent;  
  7. import android.os.IBinder;  
  8. import android.os.RemoteException;  
  9.   
  10. public class AIDLServiceImpl extends Service {  
  11.   
  12.     @Override  
  13.     public IBinder onBind(Intent intent) {  
  14.         return mBinder;  
  15.     }  
  16.   
  17.     /** 
  18.      * 在AIDL文件中定义的接口实现。 
  19.      */  
  20.     private IAIDLService.Stub mBinder = new Stub() {  
  21.   
  22.         public String getName() throws RemoteException {  
  23.             return "mayingcai";  
  24.         }  
  25.   
  26.         public Person getPerson() throws RemoteException {  
  27.             Person mPerson = new Person();  
  28.               
  29.             mPerson.setName("mayingcai");  
  30.             mPerson.setAge(24);  
  31.               
  32.             return mPerson;  
  33.         }  
  34.           
  35.     };  
  36.       
  37. }  

 

        (5)在AndroidManifest.xml文件中注册Service:

  1. <service android:name = ".AIDLServiceImpl" android:process = ":remote">  
  2.   
  3.     <intent-filter>  
  4.         <action android:name = "com.focus.aidl.IAIDLService" />  
  5.     </intent-filter>  
  6.       
  7. </service>  

 

     2. 客户端实现:

 

        (1)目录结构,如下图:

                AIDL客户端工程

        (2)将服务器端的IAIDLService.aidl,Person.aidl和Person.java文件拷贝到本工程中,如上图所示:

 

        (3)res/layout/main.xml实现:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"  
  4.     android:orientation = "vertical"  
  5.     android:layout_width = "fill_parent"  
  6.     android:layout_height = "fill_parent"  
  7.     >  
  8.       
  9.     <TextView  
  10.         android:id = "@+id/name"  
  11.         android:layout_width = "wrap_content"  
  12.         android:layout_height = "wrap_content"  
  13.         />  
  14.       
  15.     <Button  
  16.         android:id = "@+id/connection"  
  17.         android:layout_width = "wrap_content"  
  18.         android:layout_height = "wrap_content"  
  19.         android:text = "连接"  
  20.         />  
  21.           
  22.     <Button  
  23.         android:id = "@+id/message"  
  24.         android:layout_width = "wrap_content"  
  25.         android:layout_height = "wrap_content"  
  26.         android:enabled = "false"  
  27.         android:text = "信息"  
  28.         />  
  29.       
  30.     <Button  
  31.         android:id = "@+id/person"  
  32.         android:layout_width = "wrap_content"  
  33.         android:layout_height = "wrap_content"  
  34.         android:enabled = "false"  
  35.         android:text = "人"  
  36.         />  
  37.       
  38. </LinearLayout>  

 

        (4)主Activity实现,从服务器端获取数据在客户端显示:

  1. package com.focus.aidl.client;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.ComponentName;  
  5. import android.content.Intent;  
  6. import android.content.ServiceConnection;  
  7. import android.os.Bundle;  
  8. import android.os.IBinder;  
  9. import android.os.RemoteException;  
  10. import android.view.View;  
  11. import android.view.View.OnClickListener;  
  12. import android.widget.Button;  
  13. import android.widget.TextView;  
  14.   
  15. import com.focus.aidl.IAIDLService;  
  16. import com.focus.aidl.Person;  
  17.   
  18. public class AIDLClientAcitivty extends Activity {  
  19.       
  20.     private IAIDLService mAIDLService;  
  21.       
  22.     private TextView mName;  
  23.       
  24.     private Button mMessage;  
  25.       
  26.     private Button mPerson;  
  27.       
  28.     /** 
  29.      * 第一步,创建ServiceConnection对象,在onServiceConnected()方法中获取IAIDLService实现。 
  30.      */  
  31.     private ServiceConnection mServiceConnection = new ServiceConnection() {  
  32.   
  33.         public void onServiceConnected(ComponentName name, IBinder service) {  
  34.             mAIDLService = IAIDLService.Stub.asInterface(service);  
  35.               
  36.             mMessage.setEnabled(true);  
  37.             mPerson.setEnabled(true);  
  38.         }  
  39.           
  40.         public void onServiceDisconnected(ComponentName name) {  
  41.             mAIDLService = null;  
  42.   
  43.             mMessage.setEnabled(false);  
  44.             mPerson.setEnabled(false);  
  45.         }  
  46.           
  47.     };  
  48.       
  49.     @Override  
  50.     public void onCreate(Bundle savedInstanceState) {  
  51.         super.onCreate(savedInstanceState);  
  52.           
  53.         setContentView(R.layout.main);  
  54.           
  55.         mName = (TextView) findViewById(R.id.name);  
  56.           
  57.         findViewById(R.id.connection).setOnClickListener(new OnClickListener() {  
  58.             public void onClick(View view) {  
  59.                   
  60.                 /** 
  61.                  * 第二步,单击"连接"按钮后用mServiceConnection去bind服务器端创建的Service。 
  62.                  */  
  63.                 Intent service = new Intent("com.focus.aidl.IAIDLService");  
  64.                 bindService(service, mServiceConnection, BIND_AUTO_CREATE);  
  65.                   
  66.             }  
  67.         });  
  68.           
  69.         mMessage = (Button) findViewById(R.id.message);  
  70.         mMessage.setOnClickListener(new OnClickListener() {  
  71.             public void onClick(View view) {  
  72.                 /** 
  73.                  * 第三步,从服务器端获取字符串。 
  74.                  */  
  75.                 try {  
  76.                     mName.setText(mAIDLService.getName());  
  77.                 } catch (RemoteException e) {  
  78.                     e.printStackTrace();  
  79.                 }  
  80.             }  
  81.         });  
  82.           
  83.         mPerson = (Button) findViewById(R.id.person);  
  84.         mPerson.setOnClickListener(new OnClickListener() {  
  85.             public void onClick(View view) {  
  86.                 /** 
  87.                  * 第四步,从服务器端获取Person对象。 
  88.                  */  
  89.                 try {  
  90.                     Person mPerson = mAIDLService.getPerson();  
  91.                     mName.setText("姓名:" + mPerson.getName() + ", 年龄:" + mPerson.getAge());  
  92.                 } catch (RemoteException e) {  
  93.                     e.printStackTrace();  
  94.                 }  
  95.             }  
  96.         });  
  97.     }  
  98.       
  99. }  

抱歉!评论已关闭.