创建使用远程服务的基本流程

1.创建aidl接口文件,定义其抽象方法和常量字段,由ADT生成Java文件

创建使用远程服务的基本流程:
* 1. 创建AIDL接口文件,定义其抽象方法和常量字段,由ADT生成Java文件
* 2. 创建远程服务Service类RemoteService
* 3. 在该RemoteService类中实现OnBind方法
* 4. 在onBind方法中返回该服务的IBinder的代理对象DataService.Stub
* 5. 在代理对象中实现AIDL接口中的抽象方法,以便于向远程客户端提供相应数据
* 6. 安装该服务(注册该服务:

<service android:name="android.inputmethodservice.InputMethodService">
<intent-filter>
<action android:name="android.intent.action.remoteservice" />

</intent-filter>
</service>

)
* 7. 定义远程客户端,将服务端的包含AIDL文件的包原封不动的拷贝到客户端的src下,让其生成Java文件
* 8. 创建ServiceConnection连接
* 9. 在其onServiceConnected方法中接收代理对象
* dataService = DataService.Stub.asInterface(service);
* 10. 通过bindService绑定服务
* 11. 通过dataService这个代理对象访问远程服务提供的数据
* 12. 通过unBindService解除绑定

 

2. 创建远程服务Service类RemoteService

 1 package com.bwf.a18_serviceremote;
 2 
 3 import com.bwf.a18_serviceremote.aidl.DataService;
 4 
 5 import android.app.Service;
 6 import android.content.Intent;
 7 import android.os.Binder;
 8 import android.os.IBinder;
 9 import android.os.RemoteException;
10 /**
11  * 创建使用远程服务的基本流程:
12  *         1. 创建AIDL接口文件,定义其抽象方法和常量字段,由ADT生成Java文件
13  *         2. 创建远程服务Service类RemoteService
14  *         3. 在该RemoteService类中实现OnBind方法
15  *         4. 在onBind方法中返回该服务的IBinder的代理对象DataService.Stub
16  *         5. 在代理对象中实现AIDL接口中的抽象方法,以便于向远程客户端提供相应数据
17  *         6. 安装该服务
18  *         7. 定义远程客户端,将服务端的包含AIDL文件的包原封不动的拷贝到客户端的src下,让其生成Java文件
19  *         8. 创建ServiceConnection连接
20  *         9. 在其onServiceConnected方法中接收代理对象
21  *             dataService = DataService.Stub.asInterface(service);
22  *         10. 通过bindService绑定服务
23  *         11. 通过dataService这个代理对象访问远程服务提供的数据
24  *         12. 通过unBindService解除绑定
25  *         
26  * */
27 public class RemoteService extends Service{
28     String[] arr;
29     
30     @Override
31     public IBinder onBind(Intent intent) {
32         arr = getResources().getStringArray(R.array.phoneName);
33         return binder;
34     }
35 
36     Binder binder = new DataService.Stub() {
37         
38         @Override
39         public String getStr(int flag) throws RemoteException {
40             if(flag > -1 && flag < arr.length){
41                 return arr[flag];
42             } else{
43                 return "无名氏";
44             }
45         }
46     };
47 }

3.在访问服务的客户端需要将aidl文件复制该目录下

4.在客户端取出数据

 1 package com.bwf.a18_service_cleint_01;
 2 
 3 import java.util.InputMismatchException;
 4 
 5 import com.bwf.remote_011.Data;
 6 
 7 import android.os.Bundle;
 8 import android.os.IBinder;
 9 import android.os.RemoteException;
10 import android.app.Activity;
11 import android.content.ComponentName;
12 import android.content.Context;
13 import android.content.Intent;
14 import android.content.ServiceConnection;
15 import android.view.Menu;
16 import android.view.MenuItem;
17 import android.widget.EditText;
18 import android.widget.TextView;
19 import android.widget.Toast;
20 
21 public class MainActivity extends Activity {
22     TextView tv;
23     EditText edt;
24     
25     Intent intent;
26     Data data;
27     
28     ServiceConnection conn = new ServiceConnection() {
29         
30         @Override
31         public void onServiceDisconnected(ComponentName name) {
32             // TODO Auto-generated method stub
33             
34         }
35         
36         @Override
37         public void onServiceConnected(ComponentName name, IBinder service) {
38             data = Data.Stub.asInterface(service);
39         }
40     };
41     
42     @Override
43     protected void onCreate(Bundle savedInstanceState) {
44         super.onCreate(savedInstanceState);
45         setContentView(R.layout.activity_main);
46         
47         tv = (TextView) findViewById(R.id.tv);
48         edt = (EditText) findViewById(R.id.edt);
49         
50         intent = new Intent("com.fanhy.remo");
51     }
52 
53     @Override
54     public boolean onCreateOptionsMenu(Menu menu) {
55         // Inflate the menu; this adds items to the action bar if it is present.
56         getMenuInflater().inflate(R.menu.main, menu);
57         return true;
58     }
59 
60     @Override
61     public boolean onOptionsItemSelected(MenuItem item) {
62         switch (item.getItemId()) {
63         case R.id.action_bind:
64             bindService(intent, conn, Context.BIND_AUTO_CREATE);
65             break;
66         case R.id.action_unbind:
67             unbindService(conn);
68             break;
69         case R.id.action_get:
70             String str = edt.getText()+"";
71             try {
72                 int index = Integer.parseInt(str);
73                 String name = data.getData(index);
74                 tv.setText(name);
75             } catch (RemoteException e) {
76                 // TODO Auto-generated catch block
77                 e.printStackTrace();
78             } catch (InputMismatchException e) {
79                 Toast.makeText(getApplicationContext(), "输入数据格式异常", Toast.LENGTH_LONG).show();
80             }
81             break;
82 
83         default:
84             break;
85         }
86         return super.onOptionsItemSelected(item);
87     }
88 }

 

posted on 2015-10-12 22:25  敬的男人  阅读(249)  评论(0)    收藏  举报