使用aidl绑定远程服务

一、服务端

1、清单文件,因为要远程调用,所以要配个action

1        <service android:name="com.example.alipayservice.AliPayService">
2             <intent-filter>
3                 <action android:name="com.example.alipay"></action>
4             </intent-filter>
5         </service>

2、实现服务类

 1 package com.example.alipayservice;
 2 
 3 import android.app.Service;
 4 import android.content.Intent;
 5 import android.os.Binder;
 6 import android.os.IBinder;
 7 
 8 public class AliPayService extends Service {
 9 
10     @Override
11     public IBinder onBind(Intent intent) {
12         // TODO Auto-generated method stub
13         System.out.println("远程服务被绑定onBind");
14         return new Mybinder();
15     }
16 
17     private void methodInService(){
18         System.out.println("我是远程支付宝的服务,用来支付的");
19     }
20     
21     private class Mybinder extends IService.Stub{
22 
23         @Override
24         public void callMethodInService() {
25             // TODO Auto-generated method stub
26             methodInService();
27         }
28         
29     }
30     @Override
31     public void onCreate() {
32         // TODO Auto-generated method stub
33         System.out.println("远程服务被创建onCreate");
34         super.onCreate();
35     }
36 
37     @Override
38     public void onDestroy() {
39         // TODO Auto-generated method stub
40         System.out.println("远程服务被销毁onDestroy");
41         super.onDestroy();
42     }
43 
44     @Override
45     public boolean onUnbind(Intent intent) {
46         // TODO Auto-generated method stub
47         System.out.println("远程服务被解除onUnbind");
48         return super.onUnbind(intent);
49     }
50 
51 }

3、aidl文件(安卓接口定义语言),注意:它是没有public修饰的

1 package com.example.alipayservice;
2 interface IService {
3     void callMethodInService();
4 }

 

 

二、客户端调用

1、引入aidl文件,这个文件要和服务端的一样,并且连包名也要一样,否则会报错

1 package com.example.alipayservice;
2 
3 interface IService {
4     void callMethodInService();
5 }

2、客户端调用代码

 1 package com.example.games;
 2 
 3 import com.example.alipayservice.IService;
 4 
 5 import android.os.Bundle;
 6 import android.os.IBinder;
 7 import android.os.RemoteException;
 8 import android.app.Activity;
 9 import android.content.ComponentName;
10 import android.content.Intent;
11 import android.content.ServiceConnection;
12 import android.view.Menu;
13 import android.view.View;
14 
15 public class MainActivity extends Activity {
16 
17     private Intent intent;
18     private IService iService;
19     @Override
20     protected void onCreate(Bundle savedInstanceState) {
21         super.onCreate(savedInstanceState);
22         setContentView(R.layout.activity_main);
23         intent = new Intent();
24         intent.setAction("com.example.alipay");
25     }
26 
27     public void bind(View view){
28         bindService(intent, new MyConn(), BIND_AUTO_CREATE);
29     }
30     
31     private class MyConn implements ServiceConnection{
32 
33         @Override
34         public void onServiceConnected(ComponentName name, IBinder service) {
35             // 注意和本地使用时的区别,这里是使用了方法获取的,而不是强制类型转换
36             iService = IService.Stub.asInterface(service);
37         }
38 
39         @Override
40         public void onServiceDisconnected(ComponentName name) {
41             // TODO Auto-generated method stub
42             
43         }
44         
45         
46     }
47     public void call(View view){
48         try {
49             iService.callMethodInService();
50         } catch (RemoteException e) {
51             // TODO Auto-generated catch block
52             e.printStackTrace();
53         }
54     }
55 
56 }

注意:

1、获取接口对象的方法是:IService.Stub.asInterface(service),而不是本地使用时的强制类型转换

2、不解除绑定,直接退出activity,程序的logcat还是会显示错误信息的,应当遵循本地绑定服务时的注意事项

posted @ 2016-03-29 17:14  zhongyinghe  阅读(513)  评论(0编辑  收藏  举报