Android银联支付之AIDL的基本使用

近期忙着银联支付接口的对接,银联支付的接口采用的AIDL进行通讯。那么我就和大家一起分享AIDL是如何使用的?

AIDL的全称为Android Interface Definition Language,接口描述语言。主要用于进程之间的通讯

新建service项目,新建AIDL文件

我这里使用的工具是Android Studio,新建service项目,我这里的包名是com.github.service,以Packages为目录结构,如下图新建IRemoteService.aidl文件:

aidl

// IRemoteService.aidl
package com.github.service;

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

interface IRemoteService {
    /**
     * Demonstrates some basic types that you can use as parameters
     * and return values in AIDL.
     */
    void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
            double aDouble, String aString);
            //aidl支持的参数类型有int  long  boolean  float  double String
}

AIDL支持下列所述的数据类型:

  • 所有的基本类型(int、float等)
  • String
  • CharSequence
  • List
  • Map
  • 实现Pacelable接口

接着在接口中添加方法:

interface IRemoteService {
    /**
     * Demonstrates some basic types that you can use as parameters
     * and return values in AIDL.
     */
    void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
            double aDouble, String aString);

    //简单测试获取返回值        
    String baseAidl();
}

然后点击预编译按钮:

aidl

AIDL文件生成对应的Java文件, 可以在build/generated/source/aidl/debug目录下找到这些Java文件。可以看到其内部有一个静态抽象类Stub,这个Stub继承自Binder类,并抽象实现了其父接口,这里对应的是IRemoteService这个接口:

public static abstract class Stub extends android.os.Binder implements com.github.service.IRemoteService

新建RemoteService服务

Java目录下新建RemoteService类:

aidl

package com.github.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.Nullable;

/**
 * Created by Administrator on 9/19 0019.
 */
public class RemoteService extends Service {

    private IRemoteService.Stub mIRemoteService = new IRemoteService.Stub() {
        @Override
        public String baseAidl() throws RemoteException {
            return "你好啊,我是小智!";
        }

        @Override
        public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {

        }
    };

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return mIRemoteService.asBinder();
    }
}

RemoteService 继承Service,在onBind方法中返回IBinder 实例

一定不要忘记在AndroidManifest.xml清单文件中配置:

        <service
            android:name=".RemoteService"
            android:process=":remote">
            <intent-filter>
                <action android:name="com.github.service.RemoteService"></action>
            </intent-filter>

        </service>

绑定服务

        Intent intent = new Intent(this, RemoteService.class);
        bindService(intent, new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                IResultReceiver.Stub.asInterface(service);
                Toast.makeText(MainActivity.this, "发送成功!", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {

            }
        }, BIND_AUTO_CREATE);

调用bindService方法进行服务的一个绑定

新建Client项目进行通讯

复制Service项目下的aidl目录到Clientmain目录下:

aidl

当然你新建目录也是可以的,需要注意的是:aidl文件下的包名。

下面来看看aidl接口是怎么调用的:

package com.github.client;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.github.service.IRemoteService;


public class MainActivity extends AppCompatActivity {

    private IRemoteService mIRemoteService;
    private TextView tv;
    private Button bt;

    private static final  String  ACTION="com.github.service.RemoteService";
    private static final  String  PACKAGE="com.github.service";

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

        tv = (TextView) findViewById(R.id.tv);
        bt = (Button) findViewById(R.id.bt);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    if (mIRemoteService != null) {
                        String text = mIRemoteService.baseAidl();
                        if (text != null) {
                            tv.setText(text);
                        }
                    }else {
                        tv.setText("没有获取到服务器发送来的消息");
                    }
                } catch (RemoteException e) {
                    e.printStackTrace();
                }

            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        Intent service = new Intent();
        service.setAction(ACTION);
        service.setPackage(PACKAGE);
        bindService(service, new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
                mIRemoteService = IRemoteService.Stub.asInterface(iBinder);
            }

            @Override
            public void onServiceDisconnected(ComponentName componentName) {

            }
        }, BIND_AUTO_CREATE);

    }
}

注意setAction是与service清单文件的action相匹配的,5.0以上的系统需要加上:service.setPackage(PACKAGE);

运行起来,我们一起来看看:

aidl

项目已经上传到github

Client项目

Service项目

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值