本文操作基于Android Studio
服务端
- 创建一个Android工程
- 选中项目右键新建AIDL文件(菜单:New > AIDL)
定义了一个getSum方法调用
AidlServer\app\src\main\aidl\efun\uiai\aidlserver\IAidlServerInterface.aidl
package efun.uiai.aidlserver; interface IAidlServerInterface { int getSum(int a,int b); }
- 继承AIDL实现功能
代码extends时可能会报红,Make一下就好了(菜单:Build > Make Projiect)
AidlServer\app\src\main\java\efun\uiai\aidlserver\AidlServerImpl.java
package efun.uiai.aidlserver; import android.util.Log; public class AidlServerImpl extends IAidlServerInterface.Stub { public int getSum(int a,int b){ Log.e("AidlServer","服务连接成功:"+a+"+"+b+"="+(a+b)); return a+b; } }
- 实现Android服务
这是客户端需要连接的服务
AidlServer\app\src\main\java\efun\uiai\aidlserver\AidlService.java
package efun.uiai.aidlserver; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.util.Log; import androidx.annotation.Nullable; public class AidlService extends Service { private AidlServerImpl mAidlServer; @Override public void onCreate() { super.onCreate(); Log.e("AidlService","服务创建成功!"); mAidlServer=new AidlServerImpl(); } @Nullable @Override public IBinder onBind(Intent intent) { Log.e("AidlService","onBind被调用!"); return mAidlServer; } }
- AndroidManifest.xml注册服务
必须加上exported="true",否则其它应用没法调用
- 服务端启用服务
我这里是直接在Activity中启动服务了,正常使用时视情况定
package efun.uiai.aidlserver; import android.content.Intent; import android.os.Bundle; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //启动服务 Intent intent=new Intent(this,AidlService.class); startService(intent); } }
客户端
- 创建一个Android工程
- 选中项目右键新建AIDL文件(菜单:New > AIDL)
修改AIDL文件名与服务端一样,内容一样。创建的文件路径文件夹名字和包名回合服务端的不一样,修改为一样的
AidlClient\app\src\main\aidl\efun\uiai\aidlserver\IAidlServerInterface.aidl
package efun.uiai.aidlserver; interface IAidlServerInterface { int getSum(int a,int b); }
- 连接并调用
我这里全部在Activity里面实现的
package efun.uiai.aidlclient; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.Handler; import android.os.IBinder; import android.os.RemoteException; import android.widget.TextView; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import efun.uiai.aidlserver.IAidlServerInterface; public class MainActivity extends AppCompatActivity { //定义AIDL的全局变量 private IAidlServerInterface mAidlServer; //创建链接的全局变量 private ServiceConnection mConn=new ServiceConnection() { //和服务产生连接后,给AIDL的全局变量赋值 @Override public void onServiceConnected(ComponentName name, IBinder service) { mAidlServer = IAidlServerInterface.Stub.asInterface(service); } //和服务断开连接后,将AIDL变量清空 @Override public void onServiceDisconnected(ComponentName name) { mAidlServer=null; } }; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //配置Intent,和服务端的AidlService创建连接 Intent intent=new Intent(); intent.setComponent(new ComponentName("efun.uiai.aidlserver","efun.uiai.aidlserver.AidlService")); //创建连接,连接失败会报错,正常使用时建议做异常不会处理,不然会闪退 bindService(intent,mConn, Context.BIND_AUTO_CREATE); //和界面做绑定,现实效果 TextView show=findViewById(R.id.show); new Handler().postDelayed(() -> { if (mAidlServer==null){ show.setText("连接失败!"); return; } try { //调用远程方法 int res = mAidlServer.getSum(20,7); show.setText("20+7="+res); } catch (RemoteException e) { e.printStackTrace(); } },5000); } }
效果展示与讲解
- 服务端的log输出说明
- 客户端显示效果
评论区