欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解Android跨進(jìn)程通信之AIDL

 更新時(shí)間:2021年04月09日 14:17:21   作者:CJ_Geek  
這篇文章主要介紹了詳解Android跨進(jìn)程通信之AIDL,想了解跨進(jìn)程的同學(xué)可以參考下

需求描述

進(jìn)程A調(diào)起第三方進(jìn)程B進(jìn)行第三方登錄 – 實(shí)現(xiàn)雙向通信

代碼(進(jìn)程A)

1.目錄結(jié)構(gòu)

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() {
        // 綁定進(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() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            // 獲取到進(jìn)程B中的binder對(duì)象
            iLogin = ILoginInterface.Stub.asInterface(service);
        }

        @Override
        public 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);
        }
    }
}

對(duì)應(yīng)界面

3. ILoginInterface.aidl

// ILoginInterface.aidl
package com.example.process;

// Declare any non-default types here with import statements

interface 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對(duì)象返回給進(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">

    <application
        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="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)實(shí)例化
            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)

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() {
        // 綁定進(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() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            iLogin = ILoginInterface.Stub.asInterface(service);
        }

        @Override
        public 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);
        }
    }
}

對(duì)應(yīng)界面

3. ILoginInterface.aidl (與進(jìn)程A相同)

4. LoginService.java 用于進(jìn)程A調(diào)用的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() {
        // 調(diào)起登錄頁面
        Intent intent = new Intent(this, LoginActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

}

5.AndroidManifest.xml

<application
        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=".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的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Android中一種巧妙的drawable.xml替代方案分享

    Android中一種巧妙的drawable.xml替代方案分享

    這篇文章主要給大家介紹了關(guān)于Android中一種巧妙的drawable.xml替代方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-09-09
  • android 版本檢測(cè) Android程序的版本檢測(cè)與更新實(shí)現(xiàn)介紹

    android 版本檢測(cè) Android程序的版本檢測(cè)與更新實(shí)現(xiàn)介紹

    做個(gè)網(wǎng)站的安卓客戶端,用戶安裝到自己手機(jī)上,如果我出了新版本怎么辦呢?要有版本更新功能,感興趣的朋友可以了解下
    2013-01-01
  • Android多線程下載示例詳解

    Android多線程下載示例詳解

    這篇文章主要為大家詳細(xì)介紹了Android多線程下載示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • Android全屏設(shè)置的方法總結(jié)

    Android全屏設(shè)置的方法總結(jié)

    這篇文章主要介紹了Android全屏設(shè)置的方法總結(jié)的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • Android筆記之:App列表之下拉刷新的使用

    Android筆記之:App列表之下拉刷新的使用

    本篇文章介紹了,在Android中App列表之下拉刷新的使用。需要的朋友參考下
    2013-04-04
  • Android自定義控件仿iOS滑塊SwitchButton

    Android自定義控件仿iOS滑塊SwitchButton

    這篇文章主要為大家詳細(xì)介紹了Android自定義控件模仿iOS滑塊SwitchButton,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-12-12
  • Android AS創(chuàng)建自定義布局案例詳解

    Android AS創(chuàng)建自定義布局案例詳解

    這篇文章主要介紹了Android AS創(chuàng)建自定義布局案例詳解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-09-09
  • Android使用RecycleView實(shí)現(xiàn)拖拽交換item位置

    Android使用RecycleView實(shí)現(xiàn)拖拽交換item位置

    這篇文章主要為大家詳細(xì)介紹了Android使用RecycleView實(shí)現(xiàn)拖拽交換item位置,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • Android 實(shí)現(xiàn)沉浸式狀態(tài)欄的方法

    Android 實(shí)現(xiàn)沉浸式狀態(tài)欄的方法

    沉浸式狀態(tài)欄的來源就是很多手機(jī)用的是實(shí)體按鍵,沒有虛擬鍵,于是開了沉浸模式就只有狀態(tài)欄消失了。下面腳本之家小編給大家介紹Android 實(shí)現(xiàn)沉浸式狀態(tài)欄,需要的朋友可以參考下
    2015-09-09
  • Android Studio注釋模板介紹

    Android Studio注釋模板介紹

    這篇文章主要介紹了Android Studio注釋模板介紹,之前Eclipse敲/**加回車,模板就出來了,而Android Studio卻不可以自定義,現(xiàn)在我給大家介紹下用live templates替代,需要的朋友可以參考下
    2015-07-07

最新評(píng)論