android四大组件之Service 模仿支付功能(AIDL使用)

AIDL:Android Interface Definition Language,即Android接口定义语言。

aidl就是为了让不同的进程能够进行通信,

如果进程调用自己的服务就可以直接用自己的接口对象,但是在另外一个程序没办法用要调用服务的接口对象。

aidl的应用场景:多个客户端,多线程。

aidl的参数包括除了short的基本数据类型,还有String,list和map。但是注意的是如果是大数据,例如list和map要加上他的是传入,还是传入,还是既是传入又是传出。in out

如果传递的是一个对象的话要实现

Parcelable序列化接口,

然后要添加aidl文件,声明这个类,比如一个person类

parcelable person,

这样aidl文件才能识别这个类,然后也要加上是 in or out

支付软件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.zhifubao"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.zhifubao.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service
            android:name="com.example.zhifubao.ZhiFuBaoService" >
            <intent-filter >
                <action android:name="com.alibaba.zhifubao"/>
            </intent-filter>
        </service>
    </application>

</manifest>
package com.example.zhifubao;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;

public class MainActivity extends Activity {

    private Intent intent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        intent = new Intent(MainActivity.this,ZhiFuBaoService.class); 
        startService(intent);
    }


}
package com.example.zhifubao;

import com.example.zhifubao.PayHandle.Stub;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.widget.Toast;

public class ZhiFuBaoService extends Service {


    @Override
    public IBinder onBind(Intent intent) {
        return  new MyBinder();
    }
    //不用继承Binder
    class MyBinder extends Stub{

        public void pay() {
            ZhiFuBaoService.this.pay();
            
        }
        
    }
    public void pay(){
        System.out.println("已经支付100元");
    }
    
}

aidl

package com.example.zhifubao;

interface PayHandle {
    void pay();
}

要调用的支付软件
首先要吧aidl导入到自己的工程里

package com.example.game;

import com.example.zhifubao.PayHandle;
import com.example.zhifubao.PayHandle.Stub;

import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity {
    private PayHandle handle;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent intent = new Intent();
        //绑定服务
        intent.setAction("com.alibaba.zhifubao");
        bindService(intent, new Conn(),BIND_AUTO_CREATE);
        findViewById(R.id.pay).setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                try {
                    handle.pay();
                } catch (RemoteException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
    }
    class Conn implements ServiceConnection{

        @Override
        public void onServiceConnected(ComponentName arg0, IBinder service) {
            // TODO Auto-generated method stub
            //转换成一个接口对象
            handle = Stub.asInterface(service);
        }

        @Override
        public void onServiceDisconnected(ComponentName arg0) {
            // TODO Auto-generated method stub
            
        }
        
    } 



}
原文地址:https://www.cnblogs.com/84126858jmz/p/4971349.html