zoukankan      html  css  js  c++  java
  • 让我们一起学习如何使用AIDL,它其实并不难(Android)

    前言
    该篇文件讲述的是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方式查看。

    • 创建服务连接对象
    //创建一个服务连接对象
    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:id="@+id/tvJump"
                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>
    
  • 相关阅读:
    HTTP/1.1 Status Code Definitions
    宽带到底“宽不宽”
    tmux的简介及使用
    Simple Gesture – Fling
    使用postfix搭建匿名smtp服务器
    dos2unix和unix2dos命令使用 [〓 脚本功略 〓]
    Android Coding: Gestures Builder: create your gestures library
    Android Gesture 手势识别使用实例 Android mobile ITeye论坛
    使用socat进行端口转发
    notepad++在编辑python文件时以4个空格代替TAB
  • 原文地址:https://www.cnblogs.com/io1024/p/11573484.html
Copyright © 2011-2022 走看看