zoukankan      html  css  js  c++  java
  • 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
                
            }
            
        } 
    
    
    
    }
  • 相关阅读:
    js 获取和设置css3 属性值的实现方法
    API的自动化测试
    删除html标签或标签属性以及样式
    JS+CSS实现数字滚动
    video元素和audio元素相关事件
    SDT v0.0.1 上线
    safari浏览器fixed后,被软键盘遮盖的问题—【未解决】
    js中DOM事件探究
    使用Web存储API存取本地数据
    剑指offer(Java版)第七题:用两个栈实现一个队列。队列的声明如下,请实现它的两个函数appendTail和deleteHead, 分别完成在队列尾部插入结点和在队列头部删除结点的功能。
  • 原文地址:https://www.cnblogs.com/84126858jmz/p/4971349.html
Copyright © 2011-2022 走看看