4-AIII--Service跨进程通信:aidl

简介: 零、前言[1]. aidl:Android Interface definition language(安卓接口定义语言),目的:提供进程间的通信接口[2].

零、前言

[1]. aidl:Android Interface definition language(安卓接口定义语言),目的:提供进程间的通信接口
[2]. 一个应用提供服务:称为服务应用,一个使用服务:称为客户应用
[3]. 解决客户应用如何调用服务应用的服务方法时,便是aidl用武之地
[4]. 服务端开启验证服务,客户端输入用户名和命名及数值,验证用户名:abc,密码:123,数值<5000

aidl.gif
aidl.png

一、服务端代码实现:

1.com/toly1994/aiii_service/IPayAidlInterface.aidl
// IPayAidlInterface.aidl
package com.toly1994.aiii_service;

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

interface IPayAidlInterface {
    //暴露方法
   boolean pay(String name, String pwd, int money);
}

2.PayService服务
public class PayService extends Service {

    @Override
    public IBinder onBind(Intent arg0) {
        return new MyBinder();
    }

    public class MyBinder extends IPayAidlInterface.Stub {

        @Override
        public boolean pay(String name, String pwd, int money) {
            return "abc".equals(name) && "123".equals(pwd) && money < 5000;
        }
    }
}
3.注册:app/src/main/AndroidManifest.xml
<service android:name=".aidl.PayService"
         android:enabled="true"
         android:process=":push"
         android:exported="true">
    <intent-filter>
        <action android:name="www.toly1994.com.pay"></action>
    </intent-filter>
</service>
4.AidlActivity:启动服务
public class AidlActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        startService(new Intent(this, PayService.class));
    }
}

二、客户端

1. MainActivity
public class MainActivity extends AppCompatActivity {

    @BindView(R.id.et_acc)
    EditText mEtAcc;
    @BindView(R.id.et_pass)
    EditText mEtPass;
    @BindView(R.id.et_num)
    EditText mEtNum;
    @BindView(R.id.btn_buy)
    Button mBtnBuy;
    private ServiceConnection mConn;
    private IPayAidlInterface mService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        bindService();
    }

    /**
     * 绑定远程服务
     */
    private void bindService() {
        Intent intent = new Intent();
        //坑点:5.0以后要加 服务包名,不然报错
        intent.setPackage("com.toly1994.aiii_service");
        intent.setAction("www.toly1994.com.pay");
        mConn = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {

                mService = IPayAidlInterface.Stub.asInterface(service);
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
            }
        };
        bindService(intent, mConn, BIND_AUTO_CREATE);
    }


    @OnClick(R.id.btn_buy)
    public void onViewClicked() {
        try {
            boolean isPay = mService.pay(mEtAcc.getText().toString(),
                    mEtPass.getText().toString(),
                    Integer.parseInt(mEtNum.getText().toString()));
            
            Toast.makeText(this, isPay ? "购买成功" : "购买失败", Toast.LENGTH_SHORT).show();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onDestroy() {
        unbindService(mConn);
        super.onDestroy();
    }
}
2.com/toly1994/aiii_service/IPayAidlInterface.aidl
// IPayAidlInterface.aidl
package com.toly1994.aiii_service;

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

interface IPayAidlInterface {
    //暴露方法
   boolean pay(String name, String pwd, int money);
}

附录、客户端布局文件:activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical" >

    <EditText
        android:id="@+id/et_acc"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入账号" />
    <EditText
        android:id="@+id/et_pass"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入密码" />
    <EditText
        android:id="@+id/et_num"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="1"
        android:hint="请输入数额" />
    <Button
        android:id="@+id/btn_buy"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="确定" />

</LinearLayout>

注意坑点:5.0以后用action启动服务要加服务所在应用的包名,不然报错


后记、

1.声明:

[1]本文由张风捷特烈原创,转载请注明
[2]欢迎广大编程爱好者共同交流
[3]个人能力有限,如有不正之处欢迎大家批评指证,必定虚心改正
[4]你的喜欢与支持将是我最大的动力

2.连接传送门:

更多安卓技术欢迎访问:安卓技术栈
我的github地址:欢迎star
简书首发,腾讯云+社区同步更新
张风捷特烈个人网站,编程笔记请访问:http://www.toly1994.com

3.联系我

QQ:1981462002
邮箱:1981462002@qq.com
微信:zdl1994328

4.欢迎关注我的微信公众号,最新精彩文章,及时送达:
公众号.jpg
相关文章
|
1月前
|
消息中间件 Unix Linux
Linux进程间通信(IPC)介绍:详细解析IPC的执行流程、状态和通信机制
Linux进程间通信(IPC)介绍:详细解析IPC的执行流程、状态和通信机制
55 1
|
4月前
|
消息中间件 存储 Unix
进程间通信和线程间通信总结
写在前面 面试的时候一定不要疲劳战,比如上午面了一个,然后中午不休息直接赶到另外一个相距比较远的公司,影响状态。 面试的时候一定不要紧张,不管对方有几个人,总之面试的时候做好充分准备,休息好,放松心态。 好了,言归正传,开始总结。
39 0
|
4月前
|
消息中间件 存储 程序员
进程间的通信
进程间的通信
33 1
|
5月前
|
Linux
百度搜索:蓝易云【【Linux】进程间的通信之共享内存】
总结而言,Linux中的共享内存是一种用于进程间通信的高效机制,它允许多个进程共享同一块物理内存区域,实现数据的快速交换。通过适当的创建、连接、分离和删除操作,可以实现进程对共享内存的使用和管理。
223 7
|
3月前
|
编解码 监控 API
操作系统:进程的控制和通信(Windows2000)
操作系统:进程的控制和通信(Windows2000)
45 0
|
1月前
|
消息中间件 Linux API
跨进程通信设计:Qt 进程间通讯类全面解析
跨进程通信设计:Qt 进程间通讯类全面解析
80 0
|
1月前
|
消息中间件 并行计算 网络协议
探秘高效Linux C/C++项目架构:让进程、线程和通信方式助力你的代码飞跃
探秘高效Linux C/C++项目架构:让进程、线程和通信方式助力你的代码飞跃
34 0
|
2月前
|
Python
Python多进程间通信的最佳实践
Python多进程间通信的最佳实践
|
2月前
|
消息中间件 Linux
Linux下的系统编程——进程间的通信(九)
Linux下的系统编程——进程间的通信(九)
37 1
Linux下的系统编程——进程间的通信(九)
|
3月前
|
存储 缓存 Unix
C语言第四章(进程间的通信,管道通信,pipe()函数)
C语言第四章(进程间的通信,管道通信,pipe()函数)
55 0

相关实验场景

更多