国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

詳解Android跨進程通信之AIDL

瀏覽:4日期:2022-09-19 17:10:44

需求描述

進程A調起第三方進程B進行第三方登錄 ? 實現雙向通信

代碼(進程A)1.目錄結構

詳解Android跨進程通信之AIDL

2.LoginActivity.java

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() {// 綁定進程B中的服務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) { // 獲取到進程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, '未安裝第三方應用啊~', Toast.LENGTH_SHORT).show(); }} catch (RemoteException e) { e.printStackTrace();} } @Override protected void onDestroy() {super.onDestroy();if (iLogin != null) { unbindService(conn);} }}

對應界面

詳解Android跨進程通信之AIDL

3. ILoginInterface.aidl

// 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 用于進程B登錄回調的Service

public class LoginService extends Service { @Nullable @Override public IBinder onBind(Intent intent) {// 該Binder對象返回給進程B來回調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' // 是否可以被系統實例化 android:exported='true' // 是否可以被其他進程隱式調用 android:process=':remote_a'> <intent-filter><action android:name='ACTION_A'/> </intent-filter></service> </application></manifest>代碼(進程B)1.目錄結構

詳解Android跨進程通信之AIDL

2.LoginActivity.java

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() {// 綁定進程A中的服務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) { // 登錄成功后,通知客戶端進程 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);} }}

對應界面

詳解Android跨進程通信之AIDL

3. ILoginInterface.aidl (與進程A相同)4. LoginService.java 用于進程A調用的Service

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() {// 調起登錄頁面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跨進程通信之AIDL的詳細內容,更多關于Android跨進程通信AIDL的資料請關注好吧啦網其它相關文章!

標簽: Android
相關文章:
主站蜘蛛池模板: 泰和县| 临夏市| 怀集县| 丹阳市| 通化县| 自贡市| 玉溪市| 婺源县| 贵州省| 萝北县| 宣汉县| 潼南县| 安岳县| 澎湖县| 依兰县| 白山市| 邵东县| 桃园市| 温泉县| 新蔡县| 抚松县| 苏尼特右旗| 祁连县| 洪洞县| 炉霍县| 陇川县| 宁夏| 凭祥市| 乌拉特后旗| 乐都县| 夏河县| 合水县| 惠水县| 渑池县| 崇义县| 尖扎县| 怀安县| 隆化县| 柳河县| 当阳市| 北海市|