安卓笔记之Service服务——远程服务

//自定义服务继承系统服务Service
package com.example.day0329_alipayseverice;

import com.example.day0329_alipayseverice.PublicBusiness.Stub;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
//创建一个AlipayService继承Service(要在清单文件中注册)
//<application
//android:allowBackup="true"
//android:icon="@drawable/ic_launcher"
//android:label="@string/app_name"
//android:theme="@style/AppTheme" >
//<activity
//    android:name="com.example.day0329_alipayseverice.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.day0329_alipayseverice.AlipayService">
//    <intent-filter >
//        <action android:name="com.qianfeng.severice"/>
//    </intent-filter>
//</service>
//</application>
public class AlipayService extends Service {
    //
    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return new ZhiFuBao();
    }
    //定义一个中间人,来是其他应用可以访问pay()方法
    //这是要定义一个接口PublicBusiness,要把接口java类后缀名改为adil,
    //这是涉及到了adil,后面附笔记说明
    class ZhiFuBao extends Stub{

        @Override
        public void pay() throws RemoteException {
            // TODO Auto-generated method stub
            //调用外部类的pay()方法
            AlipayService.this.pay();
        }
        
    }
    public void pay(){
        Log.i("xxxxx","购买成功");
        
    }
}
//adil
package com.example.day0329_alipayseverice;


interface PublicBusiness {
     void pay();
}
//第三方应用
package com.example.day0329_alipay;

import com.example.day0329_alipayseverice.PublicBusiness;
import com.example.day0329_alipayseverice.PublicBusiness.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;

public class MainActivity extends Activity {
    PublicBusiness pb;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent intent = new Intent();
        intent.setAction("com.qianfeng.severice");
        bindService(intent, new ServiceConnection() {
            
        @Override
              public void  onServiceDisconnected(ComponentName name) {
                
            }
            
         @Override
          public void onServiceConnected(ComponentName name, IBinder service) {
                // TODO Auto-generated method stub
                pb =Stub.asInterface(service);
            }
        }, BIND_AUTO_CREATE);
    }
    public void click(View v){
        try {
            pb.pay();
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
//步骤
AIDL
* Android interface definition language
* 安卓接口定义语言
* 作用:跨进程通信
* 应用场景:远程服务中的中间人对象,其他应用是拿不到的,那么在通过绑定服务获取中间人对象时,就无法强制转换,使用aidl,就可以在其他应用中拿到中间人类所实现的接口

##支付宝远程服务
1. 定义支付宝的服务,在服务中定义pay方法
2. 定义中间人对象,把pay方法抽取成接口
3. 把抽取出来的接口后缀名改成aidl
4. 中间人对象直接继承Stub对象
5. 注册这个支付宝服务,定义它的intent-Filter

##需要支付的应用
1. 把刚才定义好的aidl文件拷贝过来,注意aidl文件所在的包名必须跟原包名一致
2. 远程绑定支付宝的服务,通过onServiceConnected方法我们可以拿到中间人对象
3. 把中间人对象通过Stub.asInterface方法强转成定义了pay方法的接口
4. 调用中间人的pay方法                   

 

posted @ 2016-03-29 20:50  丨缘来是你丨  阅读(278)  评论(0编辑  收藏  举报