一、服务端
第一步:创建aidl文件
package com.example.servicetest; interface IScan { void startScan(); void stopScan(); }
第二步:创建service 以及实现IScan.Stub抽象类
public class MyService extends Service { @Override public IBinder onBind(Intent intent) { Log.e(TAG, "onBind!"); return iScan; }
IScan.Stub iScan = new IScan.Stub() { @Override public void startScan() throws RemoteException { Log.e(TAG, "开始扫描!"); } @Override public void stopScan() throws RemoteException { Log.e(TAG, "停止扫描!"); } }; ........ }
第三步:配置清单文件,因为需要提供给第三方app调用服务,所以需要增加一个action
<service android:name=".MyService" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="com.servicetest.scan"/> </intent-filter> </service>
服务端完整代码
public class MyService extends Service { String TAG = "MyService"; @Override public IBinder onBind(Intent intent) { Log.e(TAG, "onBind!"); return iScan; } @Override public boolean onUnbind(Intent intent) { Log.e(TAG, "onUnbind!"); return super.onUnbind(intent); } @Override public void onCreate() { super.onCreate(); Log.e(TAG, "onCreate!"); } @Override public void onDestroy() { super.onDestroy(); Log.e(TAG, "onDestroy!"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.e(TAG, "onStartCommand!"); return super.onStartCommand(intent, flags, startId); } IScan.Stub iScan = new IScan.Stub() { @Override public void startScan() throws RemoteException { Log.e(TAG, "开始扫描!"); } @Override public void stopScan() throws RemoteException { Log.e(TAG, "停止扫描!"); } }; }
二、客户端
第一步,创建一个和服务端一模一样的aidl文件 (包名,文件名,接口都必须一样)
package com.example.servicetest; interface IScan { void startScan(); void stopScan(); }
第二步,绑定服务获取服务接口
Intent intent=new Intent(); intent.setAction("com.servicetest.scan"); intent.setPackage("com.example.servicetest");//必须指定当前app的包名 bindService(intent,new ServiceConnect(),BIND_AUTO_CREATE); private class ServiceConnect implements ServiceConnection { @Override public void onServiceConnected(ComponentName name, IBinder service) { iScan = IScan.Stub.asInterface(service);//将service转换成 服务接口
} @Override public void onServiceDisconnected(ComponentName name) { Log.e("MyService","onServiceDisconnected!"); } }
备注:1. android 5.0之后anction 必须指定包名。
2.必须通过 asInterface将转换服务接口。
客户端完整代码
public class MainActivity extends Activity { IScan iScan; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Intent intent=new Intent(); intent.setAction("com.servicetest.scan"); intent.setPackage("com.example.servicetest"); bindService(intent,new ServiceConnect(),BIND_AUTO_CREATE); } public void startScan(View view){ try { iScan.startScan(); } catch (RemoteException e) { Log.e("MyService","e="+e.toString()); } } private class ServiceConnect implements ServiceConnection { @Override public void onServiceConnected(ComponentName name, IBinder service) { iScan = IScan.Stub.asInterface(service); } @Override public void onServiceDisconnected(ComponentName name) { Log.e("MyService","onServiceDisconnected!"); } } }
asInterface