AIDL的简单使用

第一、定义AIDL文件
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// IRemoteService.aidl
package com.example.android;
 
// Declare any non-default types here with import statements
 
/** Example service interface */
interface IRemoteService {
     /** Request the process ID of this service, to do evil things with it. */
     int getPid();
 
     /** Demonstrates some basic types that you can use as parameters
      * and return values in AIDL.
      */
     void basicTypes( int anInt, long aLong, boolean aBoolean, float aFloat,
             double aDouble, String aString);
}
这段代码也是官方文档的。命名为IRemoteService.aidl,放在com.example.android包下(这个可以随意),保存后Android编译器会在gen目录下自动生成IRemoteService.java文件 第二、定义我们的服务,DDService.java,并且需要在AndroidManifest.xml中注册,并添加“duanqing.test.aidl” 的ACTION
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.example.service;
 
import com.example.android.IRemoteService;
 
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.Process;
 
public class DDService extends Service {
     @Override
     public void onCreate() {
         super .onCreate();
         System.out.println( "DDService onCreate........" + "Thread: " + Thread.currentThread().getName());
     }
     @Override
     public IBinder onBind(Intent arg0) {
         System.out.println( "DDService onBind" );
         return mBinder;
     }
 
     private final IRemoteService.Stub mBinder = new IRemoteService.Stub() {
         public int getPid(){
             System.out.println( "Thread: " + Thread.currentThread().getName());
             System.out.println( "DDService getPid " );
             return Process.myPid();
         }
         public void basicTypes( int anInt, long aLong, boolean aBoolean,
             float aFloat, double aDouble, String aString) {
             System.out.println( "Thread: " + Thread.currentThread().getName());
             System.out.println( "basicTypes aDouble: " + aDouble + " anInt: " + anInt+ " aBoolean " + aBoolean+ " aString " + aString);
         }
     };
 
}

这样我们的服务端就完成了,把服务端运行到模拟器(或者手机上),等一会可以看一下打印信息,重点看“线程名” 
第三、实现客户端测试代码 新建另一个工程,同样需要添加AIDL协议文件(这是一个标准的协议文件,定义对外服务),这里我列出来我的测试代码:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package com.example.aidlclient;
 
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Process;
import android.os.RemoteException;
import android.view.View;
 
import com.example.android.IRemoteService;
 
public class MainActivity extends Activity {
     private IRemoteService remoteService;
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
     }
     
     ServiceConnection conn = new ServiceConnection() {
         
         @Override
         public void onServiceDisconnected(ComponentName name) {
         }
         
         @Override
         public void onServiceConnected(ComponentName name, IBinder service) {
             remoteService = IRemoteService.Stub.asInterface(service);
             try {
                 int pid = remoteService.getPid();
                 int currentPid = Process.myPid();
                 System.out.println( "currentPID: " + currentPid + "  remotePID: " + pid);
                 remoteService.basicTypes( 12 , 1223 , true , 12 .2f, 12.3 , "我们的爱,我明白" );
             } catch (RemoteException e) {
                 e.printStackTrace();
             }
             System.out.println( "bind success! " + remoteService.toString());
         }
     };
         
     /**
      * 监听按钮点击
      * @param view
      */
     public void buttonClick(View view) {
         System.out.println( "begin bindService" );
         Intent intent = new Intent( "duanqing.test.aidl" );
         bindService(intent, conn, Context.BIND_AUTO_CREATE);
     }
 
     @Override
     protected void onDestroy() {
         super .onDestroy();
         unbindService(conn);
     }
}
4、执行 点击客户端按钮,执行,看打印信息:
看服务端打印,DDService onCreate..........Thread: main,主线程,当客户端调用服务端getPid方法时,服务端是在Thread: Binder2中执行,当客户端调用服务端basicType方法时,服务端是在Thread:Binder1中执行
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值