Android studio 查看aidl定义的文件:当你进入你的AIDL文件并编写好了之后,点击AS上方菜单栏中的Build->Make Project,之后便可以在当前工程的app/build/generated/source/aidl/debug中找到系统为我们生成的.java文件了。
Service端
<service android:name="com.atguigu.l07_service.remote.MyRemoteService"> <intent-filter> <action android:name="com.atguigu.l07_service.remote.MyRemoteService.Action"/> </intent-filter> </service>
public class MyRemoteService extends Service { @Override public IBinder onBind(Intent intent) { Log.e("TAG", "onBind()"); return new StudentService(); } @Override public boolean onUnbind(Intent intent) { Log.e("TAG", "onUnbind()"); return super.onUnbind(intent); } //处理Student相关的业务逻辑类 class StudentService extends IStudentService.Stub { @Override public Student getStudentById(int id) throws RemoteException { Log.e("TAG", "Service getStudentById() "+id); return new Student(id, "Tom", 10000); } } }
《------------------------------start定义aidl接口--------------------------------------------》
定义自定义类型Student //必须实现Parcelable接口 public class Student implements Parcelable { private int id; private String name; get set。。。。 public int describeContents() { return 0;} //将当前对象的属性数据写到Parcel包对象中(也就是打包) 打包解包在服务器端或client端都有可能,根据功能需求分类,如果服务器传出数据,则打包就在服务器端完成,如果客户端传输数据,则打包就在客户端完成 public void writeToParcel(Parcel dest, int flags) { dest.writeInt(this.id); dest.writeString(this.name); } // 添加一个静态成员,名为CREATOR,该对象实现了Parcelable.Creator接口 public static final Parcelable.Creator<Student> CREATOR = new Parcelable.Creator<Student>() { public Student createFromParcel(Parcel source) { return new Student(source.readInt(), source.readString()); } public Student[] newArray(int size) { return new Student[size]; } }; }
创建文件:Student.aidl package com.atguigu.service.test.remote; parcelable Student;
创建文件:IStudentService.aidl package com.atguigu.service.test.remote; import com.atguigu.service.test.remote.Student; interface IStudentService { Student getStudentById(int id); }
eclipse自动生成一个通信接口类
package com.atguigu.service.test.remote; public interface IStudentService extends android.os.IInterface{ ....... }
《------------------------------end定义aidl接口--------------------------------------------》
client
public class MainActivity extends Activity { private EditText et_aidl_id; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_aidl_id = (EditText) findViewById(R.id.et_aidl_id); } private ServiceConnection conn; private IStudentService studentService; public void bindRemoteService(View v) { Intent intent = new Intent( "com.atguigu.l07_service.remote.MyRemoteService.Action"); if (conn == null) { conn = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { } @Override public void onServiceConnected(ComponentName name, IBinder service) { Log.e("TAG", "onServiceConnected()"); studentService = IStudentService.Stub.asInterface(service); } }; bindService(intent, conn, Context.BIND_AUTO_CREATE); Toast.makeText(this, "绑定Service", 0).show(); } else { Toast.makeText(this, "已经绑定Service", 0).show(); } } public void invokeRemote(View v) throws RemoteException { if(studentService!=null) { int id = Integer.parseInt(et_aidl_id.getText().toString()); Student student = studentService.getStudentById(id); Toast.makeText(this, student.toString(), 0).show(); } } public void unbindRemoteService(View v) { if (conn != null) { unbindService(conn); conn = null; studentService = null; Toast.makeText(this, "解绑Service", 0).show(); } else { Toast.makeText(this, "还未绑定Service", 0).show(); } } }