很早之前用过eclipse实现过进程通信,现在eclipse开发安卓基本上淘汰掉了。这篇文章主要记录Android Studio如何实现AIDL跨进程通信。
AIDL的使用步骤
aidl远程调用传递的参数和返回值支持Java的基本类型(int long booen char byte等)和String,List,Map等。当然也支持一个自定义对象的传递。
服务端
新建一个MyAidlDemoServer工程,然后在java目录下右键新建一个aidl File
然后在该目录下新建一个IMyAidlInterface.aidl文件,代码如下:
修改生成的.aidl文件中的内容 interface IMyAidlInterface { int add(int arg1, int arg2); } //aidl文件里面的代码不需要加任何修饰符
这里定义了一个IMyAidlInterface接口,里面定义的add方法用于求和计算。
然后Build当前工程(Build选项里的Make Project)。
会发现在app/build/generated/source/aidl/debug目录下会生成一个与IMyAidlInterface.aidl文件同样包名的一个文件,该文件下面自动生成IMyAidlInterface文件,该文件里面自动实现了一些方法用于远程调用。
编写远程服务
新建MyService类继承Service,并实现以下代码。
public class MyService extends Service { IMyAidlInterface.Stub mStub = new IMyAidlInterface.Stub() { @Override public int add(int arg1, int arg2) throws RemoteException { return arg1 + arg2; } }; @Override public IBinder onBind(Intent intent) { return mStub; } }
服务里的代码重写了IMyAidlInterface.Stub类中的 add方法,然后通过重写onBind()方法将重写的IMyAidlInterface.Stub类返回出去。
然后在AndroidManifest.xml对Service进行配置。
<service android:process=":remote" android:name=".MyService" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="co.example.leo.myService"/> </intent-filter> </service>
这里设置了android:process属性,并且设置为":remote"。
android:process=":remote",代表在应用程序里,当需要该service时,会自动创建新的进程。而如果是android:process="remote",没有“:”分号的,则创建全局进程,不同的应用程序共享该进程。
然后添加了一个意图过滤器。
客户端
新建MyAidlDemoCustomer工程,然后直接把服务端的aidl目录直接拷贝到客户端的main目录下。这么一来客户端的aidl就无需编写了,直接和服务端的一模一样。包括路径的包名等。 当然也可以在客户端这边重新写aidl文件。
编辑布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="30sp" /> </LinearLayout>
这里只用了一个TextView来显示最终的计算结果。
然后编辑客户端的调用代码:
public class MainActivity extends AppCompatActivity { TextView tv; IMyAidlInterface mStub; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = (TextView)findViewById(R.id.tv); Intent intent = new Intent(); //由于是隐式启动Service 所以要添加对应的action,A和之前服务端的一样。 intent.setAction("co.example.leo.myService"); //android 5.0以后直设置action不能启动相应的服务,需要设置packageName或者Component。 intent.setPackage("co.example.leo.myaidldemoserver"); //packageName 需要和服务端的一致. bindService(intent,serviceConnection,BIND_AUTO_CREATE); } private ServiceConnection serviceConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { //调用asInterface()方法获得IMyAidlInterface实例 mStub = IMyAidlInterface.Stub.asInterface(service); if (mStub == null) { Log.e("MainActivity", "the mStub is null"); } else { //当mStub不为空就调用其add方法进行计算,并显示到TextView上面。 try { int value = mStub.add(1, 8); tv.setText(value + ""); } catch (RemoteException e) { e.printStackTrace(); } } } @Override public void onServiceDisconnected(ComponentName name) { } }; @Override protected void onDestroy(){ //解绑服务 super.onDestroy(); unbindService(serviceConnection); } }
最后安装上客户端和服务端,打开客户端后会发现已经调用了服务端的方法并计算出了结果。
总结
这是一个在AS下最简单的一个AIDL编程:
1.服务端创建一个aidl目录,然后在该目录下新建一个.aidl为后缀的接口类,该类定义远程调用的接口方法。
2.build编译之后会在app/build/generated/source/aidl/debug目录下会生成aidl远程实现类,该类是AS自动生成的。
3.在AndroidManifest.xml下配置Service的action和process属性。
4.将服务端的aidl目录拷贝到客户端相应的目录下,然后编写客户端调用代码,AS下简单的aidl编程就ok了。
- 文章2299
- 用户1336
- 访客10618802
你留下,或者我跟你走。——《海角七号》