10个AIDL与10个Service->10个AIDL+1个Service
工作机制:每个业务模块创建自己的AIDL接口,并实现接口,向服务端提供自己的唯一标识和其对应的Binder对象
对于服务端:提供一个queryBinder接口,根据业务模块特征来返回相应的Binder对象
BinderPool练习
AIDL接口
1.ICompute接口(提供计算加法)
interface ICompute { /** * Demonstrates some basic types that you can use as parameters * and return values in AIDL. */ int add(int a,int b); }2.IBinderPool接口(binder连接池接口)
interface IBinderPool { /** * Demonstrates some basic types that you can use as parameters * and return values in AIDL. */ IBinder queryBinder(int binderCode); }
3.加解密接口
interface ISecurityCenter { /** * Demonstrates some basic types that you can use as parameters * and return values in AIDL. */ String encrypt(String content); String decrypt(String password); }
AIDL接口的实现
1.ISecurityImp
import android.os.RemoteException; /** * Created by YRC on 2017/11/30. */ public class ISecurityImp extends ISecurityCenter.Stub { @Override public String encrypt(String content) throws RemoteException { char SECRET_CODE='^'; char[] encryptContent=content.toCharArray(); for (int i=0;i<encryptContent.length;i++){ encryptContent[i] ^=SECRET_CODE; } return (new String(encryptContent)); } @Override public String decrypt(String password) throws RemoteException { return encrypt(password); } }
2.IComputeImp
import android.os.RemoteException; /** * Created by YRC on 2017/11/30. */ public class IComputeImp extends ICompute.Stub{ @Override public int add(int a, int b) throws RemoteException { return a + b; } }
3.IBinderPool实现+BinderPool具体实现
import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.Binder; import android.os.IBinder; import android.os.RemoteException; import android.util.Log; import java.util.concurrent.CountDownLatch; /** * Created by YRC on 2017/11/30. */ public class BinderPool { private static final String TAG ="BinderPool" ; private static Context mContext; private CountDownLatch countDownLatch; private IBinderPool mBinderPool; private static volatile BinderPool sInstance; public static final int BINDER_COMPUTE = 0; public static final int BINDER_SECURITY_CENTER = 1; private BinderPool(Context context) { mContext = context.getApplicationContext(); connectBinderPoolService(); } public static BinderPool getInstance(Context context) { if (sInstance == null) { synchronized (BinderPool.class) { if (sInstance == null) { sInstance = new BinderPool(context); } } } return sInstance; } //连接绑定service private synchronized void connectBinderPoolService() { countDownLatch=new CountDownLatch(1); Intent service=new Intent(mContext,BindPoolService.class); mContext.bindService(service,serviceConnection,Context.BIND_AUTO_CREATE); Log.d(TAG,"connectSuccess"); try { countDownLatch.await(); } catch (InterruptedException e) { e.printStackTrace(); } } //将IBinderPool中的方法转到BinderPool类中来 public IBinder queryBinder(int binderCode){ IBinder iBinder=null; try { if (mBinderPool!=null) { iBinder = mBinderPool.queryBinder(binderCode); } } catch (RemoteException e) { e.printStackTrace(); } return iBinder; } //创建serviceConnection private ServiceConnection serviceConnection=new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { mBinderPool=BinderPoolImp.Stub.asInterface(service); try { mBinderPool.asBinder().linkToDeath(deathRecipient,0); } catch (RemoteException e) { e.printStackTrace(); } countDownLatch.countDown(); } @Override public void onServiceDisconnected(ComponentName name) { } }; //binder死亡时重启 private IBinder.DeathRecipient deathRecipient=new IBinder.DeathRecipient() { @Override public void binderDied() { mBinderPool.asBinder().unlinkToDeath(deathRecipient,0); Log.d(TAG,"binder died"); mBinderPool=null; connectBinderPoolService(); } }; //实现BinderPoolImp类 public static class BinderPoolImp extends IBinderPool.Stub{ //binder是实现了IBinder的接口类 public BinderPoolImp(){ super(); } private IBinder binder=null; @Override public IBinder queryBinder(int binderCode) throws RemoteException { switch (binderCode){ case BINDER_SECURITY_CENTER: binder=new ISecurityImp(); break; case BINDER_COMPUTE: binder=new IComputeImp(); break; default: break; } return binder; } } }
BinderPool远程Service
import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; import android.util.Log; public class BindPoolService extends Service { private static final String TAG = "Service"; private Binder mBinderPool=new BinderPool.BinderPoolImp(); public BindPoolService() { } @Override public IBinder onBind(Intent intent) { return mBinderPool; } @Override public void onCreate() { super.onCreate(); Log.d(TAG,"create success"); } }
MainActivity
import android.os.IBinder; import android.os.RemoteException; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; public class MainActivity extends AppCompatActivity { private static final String TAG = "MainAc"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); new Thread(new Runnable() { @Override public void run() { doWork(); } }).start(); } private void doWork() { BinderPool binderPool=BinderPool.getInstance(MainActivity.this); IBinder securityIBinder=binderPool.queryBinder(BinderPool.BINDER_SECURITY_CENTER); ISecurityCenter securityCenter = ISecurityCenter.Stub.asInterface(securityIBinder); String msg="helloworld-安卓"; try { String password= securityCenter.encrypt(msg); Log.d(TAG,"encrypt:"+password); Log.d(TAG,"decrypt:"+ securityCenter.decrypt(password)); } catch (RemoteException e) { e.printStackTrace(); } IBinder computeIBinder=binderPool.queryBinder(BinderPool.BINDER_COMPUTE); ICompute iCompute=ICompute.Stub.asInterface(computeIBinder); try { Log.d(TAG,"result is:"+iCompute.add(3,5)); } catch (RemoteException e) { e.printStackTrace(); } } }