1.概述
AIDL全程Android Interface Definition language ,接口定义语言,主要用于进程间的通信
使用aidL实现服务与activity之间的通信
创建AIDL文件:右键点击->创建新的AIDL文件
IMyAidlInterface.aidl
// IMyAidlInterface.aidl package com.hejun.sevicedemo; // Declare any non-default types here with import statements interface IMyAidlInterface { /** * Demonstrates some basic types that you can use as parameters * and return values in AIDL. */ //在接口中自定义所需要的方法 //自定义显示进程方法 void showProgress(); }
在进行rebuild项目,将会自动生成AIDL的Java文件,如下:
/* * This file is auto-generated. DO NOT MODIFY. * Original file: D:\AndroidProject\MyStudy2\sevicedemo\src\main\aidl\com\hejun\sevicedemo\IMyAidlInterface.aidl */ package com.hejun.sevicedemo; // Declare any non-default types here with import statements public interface IMyAidlInterface extends android.os.IInterface { /** Local-side IPC implementation stub class. */ public static abstract class Stub extends android.os.Binder implements com.hejun.sevicedemo.IMyAidlInterface { private static final java.lang.String DESCRIPTOR = "com.hejun.sevicedemo.IMyAidlInterface"; /** Construct the stub at attach it to the interface. */ public Stub() { this.attachInterface(this, DESCRIPTOR); } /** * Cast an IBinder object into an com.hejun.sevicedemo.IMyAidlInterface interface, * generating a proxy if needed. */ public static com.hejun.sevicedemo.IMyAidlInterface asInterface(android.os.IBinder obj) { if ((obj==null)) { return null; } android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR); if (((iin!=null)&&(iin instanceof com.hejun.sevicedemo.IMyAidlInterface))) { return ((com.hejun.sevicedemo.IMyAidlInterface)iin); } return new com.hejun.sevicedemo.IMyAidlInterface.Stub.Proxy(obj); } @Override public android.os.IBinder asBinder() { return this; } @Override public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException { switch (code) { case INTERFACE_TRANSACTION: { reply.writeString(DESCRIPTOR); return true; } case TRANSACTION_showProgress: { data.enforceInterface(DESCRIPTOR); this.showProgress(); reply.writeNoException(); return true; } } return super.onTransact(code, data, reply, flags); } private static class Proxy implements com.hejun.sevicedemo.IMyAidlInterface { private android.os.IBinder mRemote; Proxy(android.os.IBinder remote) { mRemote = remote; } @Override public android.os.IBinder asBinder() { return mRemote; } public java.lang.String getInterfaceDescriptor() { return DESCRIPTOR; } /** * Demonstrates some basic types that you can use as parameters * and return values in AIDL. */ @Override public void showProgress() throws android.os.RemoteException { android.os.Parcel _data = android.os.Parcel.obtain(); android.os.Parcel _reply = android.os.Parcel.obtain(); try { _data.writeInterfaceToken(DESCRIPTOR); mRemote.transact(Stub.TRANSACTION_showProgress, _data, _reply, 0); _reply.readException(); } finally { _reply.recycle(); _data.recycle(); } } } static final int TRANSACTION_showProgress = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0); } /** * Demonstrates some basic types that you can use as parameters * and return values in AIDL. */ public void showProgress() throws android.os.RemoteException; }
其中类 Stub extends android.os.Binder implements com.hejun.sevicedemo.IMyAidlInterface
所以可以在服务类中的onBind方法中 返回Sub类
@Override public IBinder onBind(Intent intent) { // TODO: Return the communication channel to the service. Log.d("TAG", "绑定"); return new IMyAidlInterface.Stub() { @Override public void showProgress() throws RemoteException { Log.e(TAG, "showProgress: " + "当前进度为:" + i ); } }; }
然后在ServiceConnection的onServiceConnection的方法中,通过asInterface方法获取MyAidlInterface对象
从而调用showProgress()方法
ServiceConnection serviceConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { IMyAidlInterface iMyAidlInterface = IMyAidlInterface.Stub.asInterface(iBinder); try { iMyAidlInterface.showProgress(); } catch (RemoteException e) { e.printStackTrace(); } } @Override public void onServiceDisconnected(ComponentName componentName) { } };
将AIDL文件复制到另一个应用中,注意包名一致
package com.hejun.aldl; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.os.Build; import android.os.IBinder; import android.os.RemoteException; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import com.hejun.sevicedemo.IMyAidlInterface; public class MainActivity extends AppCompatActivity implements View.OnClickListener { ServiceConnection serviceConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { IMyAidlInterface iMyAidlInterface = IMyAidlInterface.Stub.asInterface(iBinder); try { iMyAidlInterface.showProgress(); } catch (RemoteException e) { e.printStackTrace(); } } @Override public void onServiceDisconnected(ComponentName componentName) { } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button start = findViewById(R.id.main_start); Button stop = findViewById(R.id.main_stop); Button bind = findViewById(R.id.main_bind); Button unbind = findViewById(R.id.main_unbind); start.setOnClickListener(this); stop.setOnClickListener(this); bind.setOnClickListener(this); unbind.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.main_start: Intent intent = new Intent(); intent.setAction("com.service"); intent.setPackage("com.hejun.sevicedemo"); startService(intent); break; case R.id.main_stop: Intent intent1 = new Intent(); intent1.setAction("com.service"); intent1.setPackage("com.hejun.sevicedemo"); stopService(intent1); break; case R.id.main_bind: Intent intent2 = new Intent(); intent2.setAction("com.service"); intent2.setPackage("com.hejun.sevicedemo"); bindService(intent2,serviceConnection,BIND_AUTO_CREATE); break; case R.id.main_unbind: Intent intent3= new Intent(); intent3.setAction("com.service"); intent3.setPackage("com.hejun.sevicedemo"); unbindService(serviceConnection); break; } } }
运行演示: