详解Android跨进程通信之AIDL

 更新时间:2021年04月09日 14:17:21   作者:CJ_Geek  
这篇文章主要介绍了详解Android跨进程通信之AIDL,想了解跨进程的同学可以参考下

需求描述

进程A调起第三方进程B进行第三方登录 – 实现双向通信

代码(进程A)

1.目录结构

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() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            // 获取到进程B中的binder对象
            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, "未安装第三方应用啊~", Toast.LENGTH_SHORT).show();
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (iLogin != null) {
            unbindService(conn);
        }
    }
}

对应界面

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 用于进程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">

    <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" // 是否可以被系统实例化
            android:exported="true" // 是否可以被其他进程隐式调用
            android:process=":remote_a">
            <intent-filter>
                <action android:name="ACTION_A"/>
            </intent-filter>
        </service>

    </application>

</manifest>

代码(进程B)

1.目录结构

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() {
        @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) {
                    // 登录成功后,通知客户端进程
                    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);
        }
    }
}

对应界面

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

<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跨进程通信之AIDL的详细内容,更多关于Android跨进程通信AIDL的资料请关注脚本之家其它相关文章!

相关文章

  • Android开发之android_gps定位服务简单实现

    Android开发之android_gps定位服务简单实现

    这篇文章主要介绍了Android开发之android_gps定位服务简单实现 ,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-04-04
  • Android仿微信录制语音功能

    Android仿微信录制语音功能

    这篇文章主要介绍了Android仿微信录制语音功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-11-11
  • Android自定义实现循环滚轮控件WheelView

    Android自定义实现循环滚轮控件WheelView

    滚轮布局WheelView大家经常使用,比如在选择生日的时候,风格类似系统提供的DatePickerDialog,这篇文章主要为大家详细介绍了Android自定义实现循环滚轮控件WheelView,感兴趣的小伙伴们可以参考一下
    2016-07-07
  • Android学习笔记之ActionBar Item用法分析

    Android学习笔记之ActionBar Item用法分析

    这篇文章主要介绍了Android学习笔记之ActionBar Item用法,结合实例形式分析了ActionBar Item的具体功能与相关使用技巧,需要的朋友可以参考下
    2017-05-05
  • Android自定义带动画效果的圆形ProgressBar

    Android自定义带动画效果的圆形ProgressBar

    这篇文章主要为大家详细介绍了Android自定义带动画效果的圆形ProgressBar,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-05-05
  • Android 线程死锁场景与优化解决

    Android 线程死锁场景与优化解决

    线程死锁是老生常谈的问题,线程池死锁本质上属于线程死锁的一部分,线程池造成的死锁问题往往和业务场景相关,本文主要介绍了Android 线程死锁场景与优化,感兴趣的可以了解一下
    2023-12-12
  • Android小程序实现访问联系人

    Android小程序实现访问联系人

    这篇文章主要为大家详细介绍了Android小程序实现访问联系人,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一
    2020-05-05
  • Android 列表倒计时的实现的示例代码(CountDownTimer)

    Android 列表倒计时的实现的示例代码(CountDownTimer)

    本篇文章主要介绍了Android 列表倒计时的实现的示例代码(CountDownTimer),具有一定的参考价值,有兴趣的可以了解一下
    2017-09-09
  • Android getReadableDatabase() 和 getWritableDatabase()分析对比

    Android getReadableDatabase() 和 getWritableDatabase()分析对比

    这篇文章主要介绍了Android getReadableDatabase() 和 getWritableDatabase()分析对比的相关资料,需要的朋友可以参考下
    2017-06-06
  • RecyclerView实现拖拽排序效果

    RecyclerView实现拖拽排序效果

    这篇文章主要为大家详细介绍了RecyclerView实现拖拽排序效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-06-06

最新评论