android 中activity调用远程service中的方法之 aidl的使用

服务端:只有服务,没有界面

1.编写interface文件,复制到 .aidl 文件中,并去掉其中的public 等修饰符。系统会自动在gen目录下生成对应的java文件  (对应本地调用中的接口文件)

  

2.编写service,其中内部类的自定义bind 只需要继承Stub即可。(本地调用则需要继承Bind 并实现 interface接口)

   

 1 public class PayService extends Service {
 2 
 3     @Override
 4     public IBinder onBind(Intent intent) {
 5         return new Pay();
 6     }
 7     public class Pay extends  Stub {
 8         @Override
 9         public void pay(String userName, String password, String sendToAccount, float money) throws RemoteException {
10             toPay(userName, password, sendToAccount, money);
11         }
12     }
13     public void toPay(String userName, String password, String sendToAccount, float money){
14         System.out.println(userName + "@" + password + "  to " + sendToAccount + "$" + money);
15     }
16     
17 }

 

3.清单文件中注册,添加intent-filter 项,以便于被调用

  <service android:name="cn.pay.service.PayService" >
            <intent-filter>
                <action android:name="cn.pay.service.PayService" />
            </intent-filter>
        </service>

 

 

 

========================================================分隔线========================================================

 

客户端:

  在activity中自定义ServiceConnection的内部类。转化接口实例的方法用 接口类名.Stub.asInterface(service)  (本地调用是直接将service强转为接口实例)

 

 1 package com.example.aidlpayclient;
 2 
 3 import cn.pay.aidl.IPayService;
 4 import android.os.Bundle;
 5 import android.os.IBinder;
 6 import android.os.RemoteException;
 7 import android.app.Activity;
 8 import android.content.ComponentName;
 9 import android.content.Context;
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     @Override
18     protected void onCreate(Bundle savedInstanceState) {
19         super.onCreate(savedInstanceState);
20         setContentView(R.layout.activity_main);
21         
22         //绑定服务
23         conn=new MyServiceConnection();
24         bindService(new Intent("cn.pay.service.PayService"), conn, Context.BIND_AUTO_CREATE);
25     }
26 
27     private MyServiceConnection conn = null;
28     private IPayService iPayService;
29 
30     private class MyServiceConnection implements ServiceConnection {
31 
32         @Override
33         public void onServiceConnected(ComponentName name, IBinder service) {
34             iPayService = IPayService.Stub.asInterface(service);
35             System.out.println("绑定成功");
36         }
37 
38         @Override
39         public void onServiceDisconnected(ComponentName name) {
40             iPayService = null;
41         }
42 
43     }
44 
45     public void pay(View v) {
46         System.out.println("begin pay");
47         try {
48             //调用方法.
49             iPayService.pay("a", "b", "c", 100);
50         } catch (RemoteException e) {
51             e.printStackTrace();
52         }
53         System.out.println("end pay");
54     }
55 
56 }

 

posted @ 2015-04-12 19:25  MarcoReus  阅读(616)  评论(0编辑  收藏  举报