目录
前言
之前也记录过AIDL的使用,今天简单写AIDL添加回调代码。
记录一下,方便自己查阅。
正文
为了简单一点,放在同一个应用中,然后把服务设置到新的进程中。
AIDL文件
设置回调,这里会创建两个AIDL文件。
ICar.aidl
// ICar.aidl package com.biumall.aidlx; import com.biumall.aidlx.ICarListener; interface ICar { void addICarListener(ICarListener listener); void removeICarListener(ICarListener listener); int getStatus(); void setStatus(int status); }
ICarListener.aidl
// ICarListener.aidl package com.biumall.aidlx; interface ICarListener { void onCarStatus(int status); }
远程服务
SecondService.java
public class SecondService extends Service { private final String TAG = getClass().getSimpleName(); private int mStatus = -1; private final List<ICarListener> mICarListenerList = new ArrayList<>(); @Nullable @Override public IBinder onBind(Intent intent) { return new ICarBinder(); } //继承ICar.Stu private class ICarBinder extends ICar.Stub { @Override public void addICarListener(ICarListener listener) throws RemoteException { Log.d(TAG, "addICarListener listener : " + listener); if (null != listener) { mICarListenerList.add(listener); } } @Override public void removeICarListener(ICarListener listener) throws RemoteException { Log.d(TAG, "removeICarListener listener : " + listener); if (null != listener) { for (ICarListener iCarListener : mICarListenerList) { if (listener.asBinder() == iCarListener.asBinder()) { mICarListenerList.remove(listener); } } } } @Override public int getStatus() throws RemoteException { return mStatus; } @Override public void setStatus(int status) throws RemoteException { mStatus = status; for (ICarListener listener : mICarListenerList) { listener.onCarStatus(status); } } } }
这里需要把SecondService设置为不同进程。
AndroidManifest.xml
通过process属性,设置到另外进程中。
<service android:name="com.biumall.aidlx.second.SecondService" android:exported="true" android:process=":Second" tools:ignore="MissingClass"> </service>
绑定服务
MainActivity.java
public class MainActivity extends AppCompatActivity { private final String TAG = getClass().getSimpleName(); private ICar mICar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //绑定服务 Intent intent = new Intent(); intent.setComponent(new ComponentName(MainActivity.this, SecondService.class)); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE); } @Override protected void onDestroy() { super.onDestroy(); try { if (null != mICar) { mICar.removeICarListener(mICarListener); } } catch (RemoteException e) { e.printStackTrace(); } unbindService(mServiceConnection); Log.d(TAG, "onDestroy: "); } private final ServiceConnection mServiceConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { mICar = ICar.Stub.asInterface(iBinder); try { if (null != mICar) { mICar.addICarListener(mICarListener); mICar.setStatus(1111); } } catch (RemoteException e) { e.printStackTrace(); } } @Override public void onServiceDisconnected(ComponentName componentName) { Log.d(TAG, "onServiceDisconnected: "); if (null != mICar) { try { mICar.removeICarListener(mICarListener); } catch (RemoteException e) { e.printStackTrace(); } } mICar = null; } }; private final ICarListener mICarListener = new ICarListener.Stub() { @Override public void onCarStatus(int status) throws RemoteException { Log.d(TAG, "onCarStatus status : "+ status); } }; }
上面代码绑定成功后,设置了一个状态,然后onCarStatus()会回调状态的变化。
参考文章
《
© 版权声明