詳解Android跨進(jìn)程通信之AIDL
需求描述
進(jìn)程A調(diào)起第三方進(jìn)程B進(jìn)行第三方登錄 ? 實現(xiàn)雙向通信
代碼(進(jìn)程A)1.目錄結(jié)構(gòu)public class LoginActivity extends AppCompatActivity { private ILoginInterface iLogin; @Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initService(); } private void initService() {// 綁定進(jìn)程B中的服務(wù)Intent intent = new Intent();intent.setAction('ACTION_B');intent.setPackage('com.example.processs');bindService(intent, conn, BIND_AUTO_CREATE); } private ServiceConnection conn = new ServiceConnection() {@Overridepublic void onServiceConnected(ComponentName name, IBinder service) { // 獲取到進(jìn)程B中的binder對象 iLogin = ILoginInterface.Stub.asInterface(service);}@Overridepublic void onServiceDisconnected(ComponentName name) {} }; /** * 去登錄 * * @param view */ public void goLogin(View view) {try { if (iLogin != null) {iLogin.login(); } else {Toast.makeText(this, '未安裝第三方應(yīng)用啊~', Toast.LENGTH_SHORT).show(); }} catch (RemoteException e) { e.printStackTrace();} } @Override protected void onDestroy() {super.onDestroy();if (iLogin != null) { unbindService(conn);} }}
對應(yīng)界面
// ILoginInterface.aidlpackage com.example.process;// Declare any non-default types here with import statementsinterface ILoginInterface { void login(); void loginCallback(int loginStatus, String loginUser);}4.LoginService.java 用于進(jìn)程B登錄回調(diào)的Service
public class LoginService extends Service { @Nullable @Override public IBinder onBind(Intent intent) {// 該Binder對象返回給進(jìn)程B來回調(diào)return new ILoginInterface.Stub() { @Override public void login() throws RemoteException { } @Override public void loginCallback(int loginStatus, String loginUser) throws RemoteException {Log.d('lichaojun123>>>', 'loginCallback: ' + loginStatus + ' : ' + loginUser); }}; }}5.AndroidManifest.xml
<?xml version='1.0' encoding='utf-8'?><manifest xmlns:android='http://schemas.android.com/apk/res/android' xmlns:tools='http://schemas.android.com/tools' package='com.example.processc'> <applicationandroid: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='com.example.process.LoginActivity'> <intent-filter><action android:name='android.intent.action.MAIN' /><category android:name='android.intent.category.LAUNCHER' /> </intent-filter></activity><service android:name='com.example.process.LoginService' android:enabled='true' // 是否可以被系統(tǒng)實例化 android:exported='true' // 是否可以被其他進(jìn)程隱式調(diào)用 android:process=':remote_a'> <intent-filter><action android:name='ACTION_A'/> </intent-filter></service> </application></manifest>代碼(進(jìn)程B)1.目錄結(jié)構(gòu)
public class LoginActivity extends AppCompatActivity { private EditText etName; private EditText etPwd; private ILoginInterface iLogin; @Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);etName = findViewById(R.id.et_name);etPwd = findViewById(R.id.et_pwd);initService(); } private void initService() {// 綁定進(jìn)程A中的服務(wù)Intent intent = new Intent();intent.setAction('ACTION_A');intent.setPackage('com.example.processc');bindService(intent, conn, BIND_AUTO_CREATE); } private ServiceConnection conn = new ServiceConnection() {@Overridepublic void onServiceConnected(ComponentName name, IBinder service) { iLogin = ILoginInterface.Stub.asInterface(service);}@Overridepublic void onServiceDisconnected(ComponentName name) {} }; // 去登錄 public void go_login(View view) {if (etName.getText().toString().trim().equals('lcj')&& etPwd.getText().toString().trim().equals('123')) { try {if (iLogin != null) { // 登錄成功后,通知客戶端進(jìn)程 iLogin.loginCallback(1, '登錄成功'); finish();} } catch (RemoteException e) {e.printStackTrace(); }} else { Toast.makeText(this, '登錄失敗', Toast.LENGTH_SHORT).show();} } @Override protected void onDestroy() {super.onDestroy();if (iLogin != null) { unbindService(conn);} }}
對應(yīng)界面
public class LoginService extends Service { @Nullable @Override public IBinder onBind(Intent intent) {return new ILoginInterface.Stub() { @Override public void login() throws RemoteException {execLogin(); } @Override public void loginCallback(int loginStatus, String loginUser) throws RemoteException { }}; } private void execLogin() {// 調(diào)起登錄頁面Intent intent = new Intent(this, LoginActivity.class);intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);startActivity(intent); }}5.AndroidManifest.xml
<applicationandroid: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='.LoginActivity'> <intent-filter><action android:name='android.intent.action.MAIN' /><category android:name='android.intent.category.LAUNCHER' /> </intent-filter></activity><service android:name='.LoginService' android:enabled='true' android:exported='true' android:process=':remote_b'> <intent-filter><action android:name='ACTION_B'/> </intent-filter></service> </application>
以上就是詳解Android跨進(jìn)程通信之AIDL的詳細(xì)內(nèi)容,更多關(guān)于Android跨進(jìn)程通信AIDL的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. AspNetCore&MassTransit Courier實現(xiàn)分布式事務(wù)的詳細(xì)過程2. 利用FastReport傳遞圖片參數(shù)在報表上展示簽名信息的實現(xiàn)方法3. react axios 跨域訪問一個或多個域名問題4. XHTML 1.0:標(biāo)記新的開端5. CSS3使用過度動畫和緩動效果案例講解6. ASP.NET MVC通過勾選checkbox更改select的內(nèi)容7. 詳解php如何合并身份證正反面圖片為一張圖片8. AJAX實現(xiàn)省市縣三級聯(lián)動效果9. H5頁面使用audio標(biāo)簽播放音頻10. ASP.NET MVC視圖頁使用jQuery傳遞異步數(shù)據(jù)的幾種方式詳解
