让我们一起学习如何使用AIDL,它其实并不难(Android)

摘要:
并配置信息AndroidManifest.xml<在清单文件中;调用服务方法-->AliPayServiceimportandroid。应用程序。服务;}//模拟支付方式:publicbooleanaliPay(intmoney){Log.e(“AIDL”;returntrue;returnfalse;

前言
该篇文件讲述的是AIDL最基本的使用(创建、调用),关于对于AIDL更深的认识,在后续的随笔中,会持续与大家分享并探讨。

正文

  • AIDL的定义(什么是AIDL?)
  • AIDL的应用场景(AIDL可以做什么?)
  • 如何写一个AIDL的应用?(代码)

AIDL概述(定义)

  • AIDL:Android Interface Definition Language,即Android接口定义语言。也就是说:AIDL也是一种语言。
  • 设计AIDL语言的目的:为了实现进程间通信。
    本篇文章主要介绍的是AIDL的代码实现。关于进程间通信的分析,小伙伴们可以参考:浅谈应用进程间的通信(AIDL和IPC)

AIDL应用场景

  • 如:某个应用调用支付宝的支付功能、微信的支付功能

写一个AIDL的应用 AIDL模板代码
*与你一步步掌握AIDL的应用*

需求

  • 应用A:模拟一个商城应用(如:拼XX)
  • 应用B:模拟一个支付应用(如:支付宝),应用中有一个支付服务在运行,服务中定义了一个带返回值的支付方法。
  • 要求:在应用A中,调用应用B支付服务中的支付方法,传一个参数并获取返回值。

代码实现

应用B:被调用方

    1. 创建一个service:AliPayService,并在清单文件中配置信息
  • AndroidManifest.xml

        <!--调用远程服务,需要通过bind方式启动服务,调用服务方法-->
        <service android:name=".service.pay.AliPayService">
            <intent-filter>
                <action android:name="com.zero.notes.service.pay.xxx"/>
            </intent-filter>
        </service>
  • AliPayService
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;
/**
 * 模拟:阿里支付服务
 */
public class AliPayService extends Service {
    @Override
    public IBinder onBind(Intent intent) {
        return new MyBinder();
    }
    //模拟支付方法
    public boolean aliPay(int money) {
        Log.e("AIDL", "服务线程:" + Thread.currentThread().getName());
        if (money > 100) {
            handler.sendEmptyMessage(1);
            return true;
        } else {
            handler.sendEmptyMessage(0);
            return false;
        }
    }

//    class MyBinder extends Binder implements IPayservice {
//        @Override
//        public boolean callAliPay(int money) throws RemoteException {
//            return aliPay(money);
//        }
//        @Override
//        public IBinder asBinder() {
//            return null;
//        }
//    }
    /**
     * 创建中间人对象(中间帮助类)
     */
    class MyBinder extends IPayservice.Stub {
        @Override
        public boolean callAliPay(int money) {
            return aliPay(money);
        }
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                Toast.makeText(getApplicationContext(), "土豪,购买成功...", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(getApplicationContext(), "钱太少啦,购买失败...", Toast.LENGTH_SHORT).show();
            }
        }
    };
}
  • IPayservice.aidl
    此处需要注意:创建的 IPayservice.aidl 文件是一个接口文件。Android Studio上可以直接创建一个AIDL文件。创建完成后,切记:把整个项目clean一下,让代码编辑器生成一些必要的文件信息。(Android Studio项目clean方式:Build -> Clean Project)
// IPayservice.aidl
package com.zero.notes.service.pay;
// Declare any non-default types here with import statements
interface IPayservice {
  boolean callAliPay(int money);
}
  • MainActivity
    应用B启动该服务(代码书写:kotlin语言)
class MainActivity : BaseActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initData()
    }
    private fun initData() {
        val intent = Intent(this,AliPayService::class.java)
        startService(intent)
    }
}

应用A:调用方(书写代码:Kotlin语言)

  • 拷贝:把应用B 中的 IPayservice.aidl 文件拷贝到应用A内
    切记::拷贝的时候,IPayservice.aidl 的路径位置必须与应用B中保持完全一致,包名、路径名要完全一致!
    如图所示:图1️⃣是以Android方式查看;图2️⃣是以Project方式查看。

让我们一起学习如何使用AIDL,它其实并不难(Android)第1张

让我们一起学习如何使用AIDL,它其实并不难(Android)第2张

  • 创建服务连接对象
//创建一个服务连接对象
class MyServiceConnection : ServiceConnection {

    private lateinit var iPayService: IPayservice

    override fun onServiceDisconnected(name: ComponentName?) {
    }
    override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
        //2. 获取中间人对象(服务绑定成功后会返回一个中间人对象)
        iPayService = IPayservice.Stub.asInterface(service)
    }
    //获取中间人对象
    fun getIPayService():IPayservice{
        return iPayService
    }
}
  • 在应用A 的 Activity 中调用服务方法
class MainActivity : AppCompatActivity() {

    private var connection: MyServiceConnection? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val intent = Intent()
        intent.action = "com.zero.notes.service.pay.xxx"
        intent.setPackage("com.zero.notes")
        connection = MyServiceConnection()
        bindService(intent, connection, Context.BIND_AUTO_CREATE)

        tvJump.setOnClickListener {
            val iPayService = connection?.getIPayService()
            val pay = iPayService?.callAliPay(1000)!!
            if (pay) {
                //购买成功
            } else {
                //购买失败
            }
        }
    }
    override fun onDestroy() {
        super.onDestroy()
        if (connection != null) {
            unbindService(connection)
            connection = null
        }
    }
}
  • 应用A:Activity的xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:gravity="center_horizontal"
        android:layout_height="match_parent"
        android:orientation="vertical">
    <TextView
            android: 
            android:layout_width="wrap_content"
            android:layout_marginTop="30dp"
            android:text="AIDL调用方法"
            android:textColor="#FF212121"
            android:textSize="20sp"
            android:padding="16dp"
            android:background="#66000000"
            android:textStyle="bold"
            android:layout_height="wrap_content"/>
</LinearLayout>

免责声明:文章转载自《让我们一起学习如何使用AIDL,它其实并不难(Android)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇墨卡托投影、地理坐标系、地面分辨率、地图比例尺解决镜像无法删除的问题multiple repositories下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

SonarQube系列一、Linux安装与部署

【前言】 随着项目团队规模日益壮大,项目代码量也越来越多。且不说团队成员编码水平层次不齐,即便是老手,也难免因为代码量的增加和任务的繁重而忽略代码的质量,最终的问题便是bug的增多和代码债务的堆积。因此,代码review便日益提上了日程。当然人工review的效率还是相当低下的,于是我们采用了自动化代码review的工具,便是今天的主角:SonarQube...

安卓智能聊天机器人开发(二)

接上一篇文章《安卓智能聊天机器人开发(一)》,晚上继续写。 在上一篇文章中,已经实现了对网络数据的获取和处理封装,这篇文章来讲下如何嵌入到安卓应用中。 先看下效果图: 从上面两张图我们可以发现,这个聊天布局其实就是一个ListView,只不过它和传统的ListView有些区别,因为它使用了多Item样式布局 首先,先来分析下基础布局: 这个界面是由3个布...

GIT使用log命令显示中文乱码

背静: 公司项目使用GIT进行代码同步。 问题: 之前代码提交后,有中文备注,但是在使用git log查看代码历史记录的时候发现显示乱码,如下: 后查询相关资料,现将解决办法总结如下: 1、运行Git Bash窗口,在该窗口导航条(即最上面)右键,选择Options−>Text,找到下面两处  Locale:选择 zh_CN   Charector...

VS2013、VS2019快捷键、代码块

 vs2013 返回上一光标位置: Ctrl + - 前进到下一光标位置: Ctrl + Shift + - 光标所在行的上面插入一行: Ctrl + Enter 光标所在行的下面插入一行: Ctrl + Shift + Enter 调用关键字智能提示: Ctrl + j (或者 Alt + →) 调用参数提示: Ctrl + Shift +...

一个服务器部署多个项目

一:预置条件 1:PHP环境部署完成 2:以Xampp集成环境为例 二:步骤 1:找到\apache\conf目录下的httpd.conf文件     去掉LoadModule vhost_alias_module modules/mod_vhost_alias.so之前的分号(或者#);   把以下两个地方修改为代码对应的路径   DocumentRoo...

开发人员必须知道的10个跨平台应用解决方案

移动跨平台开发已经成为现在开发很火的一个主流,比方说Tiggzi 号称最快速与最简单的创建 移动App 的基于云的生成器,它可以用来创建 HTML5 , jQuery Mobile 与 PhoneGap 应用。能够轻松的连接到 REST API ,能够导出 Android,iOS 或者移动 web 应用。该应用为收费应用,可以用Free版本进行体验。今天...