Android 跨进程通信导论,极致干货
</intent-filter> </service> 3.Client 端进行关联调用 Server 端 public?class?MainActivity?extends?AppCompatActivity?{ @Override protected?void?onCreate(Bundle?savedInstanceState)?{ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Intent?intent?=?new?Intent("com.sinitek.aidl.service"); intent.setPackage("com.sinitek.transactionserver");//minSdkVersion?21 bindService(intent,?mConn,?Context.BIND_AUTO_CREATE); } private?ServiceConnection?mConn?=?new?ServiceConnection()?{ @Override public?void?onServiceDisconnected(ComponentName?name)?{ } @Override public?void?onServiceConnected(ComponentName?name,?IBinder?service)?{ IMathAidl?mathAidl?=?IMathAidl.Stub.asInterface(service); try?{ double?result?=?mathAidl.add(1,?1); Toast.makeText(MainActivity.this,?"计算结果为:"?+?result,?Toast.LENGTH_SHORT).show(); mathAidl.play("请观看服务端播放:'西京一村夫'SINITEK 冲刺之路.mp4"); }?catch?(RemoteException?e)?{ e.printStackTrace(); } } }; } 以上代码放在: > https://github.com/BuilderPattern/AidlTransactionServer.git > https://github.com/BuilderPattern/AidlTransactionClient.git (2)如果你是一个比较有求知欲的键盘侠,你通过一顿操作在 IMathAidl.java 中就会发现,该 AIDL 会生成 java 类,实际的交互都这里完成。问题来了,是不是可以不用定义 aidl 接口,直接采用 IMathAidl.java 类中的方式去实现呢?答案是:肯定可以!因为,本质上就是 aidl 通过构建生成对应的 IMathAidl.java 文件来实现具体操作,如下:1.Server 端创建一个 Service 并注册: public?class?NoAidlService?extends?Service?{ public?IBinder?onBind(Intent?t)?{ return?mBinder; } private?NormalBinder?mBinder?=?new?NormalBinder(); private?class?NormalBinder?extends?Binder?{ @Override protected?boolean?onTransact(int?code,?Parcel?data,?Parcel?reply,?int?flags)?throws?RemoteException?{//0 加,1 乘 switch?(code)?{ case?0:?{ data.enforceInterface("NoAidlService");//检测标识 int?_arg0?=?data.readInt(); int?_arg1?=?data.readInt(); int?_result?=?_arg0?+?_arg1; reply.writeNoException(); reply.writeInt(_result); return?true; } case?1:?{ data.enforceInterface("NoAidlService"); int?_arg0?=?data.readInt(); int?_arg1?=?data.readInt(); int?_result?=?_arg0?*?_arg1; reply.writeNoException(); reply.writeInt(_result); return?true; } } return?super.onTransact(code,?data,?reply,?flags); } } } <service?android:name=".NoAidlService"> <intent-filter> <action?android:name="com.sinitek.noaidl.myservice"?/> <category?android:name="android.intent.category.DEFAULT"?/> </intent-filter> </service> 2. ``` 《Android 学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》 浏览器打开:qq.cn.hn/FTe 免费领取 ``` Client 端连接 Server 端,发送收据/等待回复: public?class?MainActivity?extends?AppCompatActivity?{ IBinder?mBinder; ServiceConnection?mServiceConnection?=?new?ServiceConnection()?{ @Override public?void?onServiceConnected(ComponentName?name,?IBinder?service)?{ mBinder?=?service; } @Override public?void?onServiceDisconnected(ComponentName?name)?{ } }; TextView?mTextView; @Override protected?void?onCreate(Bundle?savedInstanceState)?{ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTextView?=?findViewById(R.id.activity_main_operate_tv); Intent?intent?=?new?Intent("com.sinitek.noaidl.myservice"); intent.setPackage("com.sinitek.transactionservernoaidl"); bindService(intent,?mServiceConnection,?Context.BIND_AUTO_CREATE); initEvent(); } private?void?initEvent()?{ mTextView.setOnClickListener(new?View.OnClickListener()?{ @Override public?void?onClick(View?v)?{ mRcvSnd(); } }); } public?void?mRcvSnd()?{ if?(mBinder?==?null)?{ return; } Parcel?_data?=?Parcel.obtain(); Parcel?_reply?=?Parcel.obtain(); int?_code?=?(int)?(Math.random()?*?6)?%?2; int?_result; try?{ _data.writeInterfaceToken("NoAidlService");//客户端标识 _data.writeInt(6); _data.writeInt(6); mBinder.transact(_code,?_data,?_reply,?0); _reply.readException(); _result?=?_reply.readInt(); Toast.makeText(MainActivity.this,?"收到回复:"?+?_result,?Toast.LENGTH_SHORT).show();
评论