Android IPC机制(六)在Android Studio中使用AIDL实现跨进程方法调用(补充)
一般来说AIDL 比较多的是机顶盒里面各个厂家apk 的相互调用,那么我们就来实现一个apk之间跨进程通讯的
在前面第三章 我们模拟了在同一个apk中不同进程中实现 。
服务端:在清单文件中把services加上action: <intent-filter> <action android:name="com.example.MyService"/> </intent-filter>
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.ywl5320.jnithread"> <application android:name=".MyApplication" android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:process=":secondprocess" android:name=".MyService" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="com.example.MyService"/> </intent-filter> </service> </application> </manifest>
其余的aidl文件和class不变
客户的:
copyaidl文件夹到main目录下面
以及对应传输的class对象到Java包下面
此时build项目
然后新建一个aidl绑定的类
package com.amt.bsdiffpatch.utils; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.IBinder; import com.ywl5320.jnithread.IGameManager; /** */ public class AidlUtil { public IGameManager serviceCfg = null; Context mContext; public AidlUtil(Context mContext){ this.mContext =mContext; } private ServiceConnection serviceConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { serviceCfg = IGameManager.Stub.asInterface(iBinder); } @Override public void onServiceDisconnected(ComponentName componentName) { mContext.unbindService(serviceConnection); } }; public boolean bindIptvAidl() { try { Intent intent = new Intent(); intent.setAction("com.example.MyService"); boolean isSucess = mContext.bindService(intent,serviceConnection,mContext.BIND_AUTO_CREATE); System.out.println("chenzhu bindIptvAidl isSucess :"+isSucess); return isSucess; }catch(Exception e){ System.out.println("chenzhu bindIptvAidl Exception :"+e.toString()); return false; } } }
在MainActivity中调用
public class MainActivity extends AppCompatActivity { Button mBtnPatch; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final AidlUtil aidlUtil = new AidlUtil(this); System.out.println("------------>is success"+aidlUtil.bindIptvAidl()); mBtnPatch = findViewById(R.id.id_btn_patch); mBtnPatch.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Game game =null; try { game=aidlUtil.serviceCfg.getGameList().get(0); System.out.println("========>"+game.gameDescribe); }catch(Exception e){ } Toast.makeText(MainActivity.this,"=======>"+game.gameDescribe,Toast.LENGTH_LONG).show(); } }); } } 注意aidl 绑定是耗时操作!!!