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
相关文章
|
8月前
|
消息中间件 Linux C++
c++ linux通过实现独立进程之间的通信和传递字符串 demo
的进程间通信机制,适用于父子进程之间的数据传输。希望本文能帮助您更好地理解和应用Linux管道,提升开发效率。 在实际开发中,除了管道,还可以根据具体需求选择消息队列、共享内存、套接字等其他进程间通信方
167 16
|
11月前
|
消息中间件 存储 供应链
进程间通信方式-----消息队列通信
【10月更文挑战第29天】消息队列通信是一种强大而灵活的进程间通信机制,它通过异步通信、解耦和缓冲等特性,为分布式系统和多进程应用提供了高效的通信方式。在实际应用中,需要根据具体的需求和场景,合理地选择和使用消息队列,以充分发挥其优势,同时注意其可能带来的复杂性和性能开销等问题。
|
11月前
|
存储 Unix Linux
进程间通信方式-----管道通信
【10月更文挑战第29天】管道通信是一种重要的进程间通信机制,它为进程间的数据传输和同步提供了一种简单有效的方法。通过合理地使用管道通信,可以实现不同进程之间的协作,提高系统的整体性能和效率。
【进程通信】信号的捕捉原理&&用户态与内核态的区别
【进程通信】信号的捕捉原理&&用户态与内核态的区别
|
Java Android开发 数据安全/隐私保护
Android中多进程通信有几种方式?需要注意哪些问题?
本文介绍了Android中的多进程通信(IPC),探讨了IPC的重要性及其实现方式,如Intent、Binder、AIDL等,并通过一个使用Binder机制的示例详细说明了其实现过程。
798 4
|
12月前
|
存储 Python
Python中的多进程通信实践指南
Python中的多进程通信实践指南
169 0
Linux源码阅读笔记13-进程通信组件中
Linux源码阅读笔记13-进程通信组件中
|
消息中间件 安全 Java
Linux源码阅读笔记13-进程通信组件上
Linux源码阅读笔记13-进程通信组件上
|
消息中间件 存储 安全
python多进程并发编程之互斥锁与进程间的通信
python多进程并发编程之互斥锁与进程间的通信
|
消息中间件 分布式计算 网络协议
从管道路由到共享内存:进程间通信的演变(内附通信方式经典面试题及详解)
进程间通信(Inter-Process Communication, IPC)是计算机科学中的一个重要概念,指的是运行在同一系统或不同系统上的多个进程之间互相发送和接收信息的能力。IPC机制允许进程间共享数据、协调执行流程,是实现分布式系统、多任务操作系统和并发编程的基础。
176 0
从管道路由到共享内存:进程间通信的演变(内附通信方式经典面试题及详解)