一:数据提供端代码(数据提供APP)

1.java代码:Service

package com.example.remoteaidi;import com.example.remoteaidi.aidl.StudentAidl;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.os.RemoteException;public class StudentService extends Service {private String[] studentNo = {"王1","2","王3","王4","王月4","宋6"};private IBinder binder = new StudentQueryBinder();@Overridepublic IBinder onBind(Intent intent) {return binder;}class StudentQueryBinder extends StudentAidl.Stub {@Overridepublic String getStudent(int no) throws RemoteException {int l = studentNo.length;if(l<0) {no = 0;}if(no>=l) {no = l - 1;}return studentNo[no];}}}
aidl文件

package com.example.remoteaidi.aidl;interface StudentAidl {String getStudent(int no);}

清单文件

<?xml version="1.0" encoding="utf-8"?>                                                                              

二:请求端代码

package com.example.aidlclient;import com.example.remoteaidi.aidl.StudentAidl;import android.app.Activity;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.os.RemoteException;import android.view.View;import android.widget.EditText;import android.widget.TextView;public class MainActivity extends Activity {private ServiceConnectionImpl sci ;private StudentAidl student;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Intent intent = new Intent();intent.setAction("student.query");sci = new ServiceConnectionImpl();bindService(intent, sci, BIND_AUTO_CREATE);}public void queryOnClick(View v) {EditText et_no_text = (EditText) findViewById(R.id.et_no_text);String no = et_no_text.getText().toString();String sname;try {sname = student.getStudent(new Integer(no));TextView tv_no = (TextView) findViewById(R.id.tv_no);tv_no.setText(sname);} catch (NumberFormatException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (RemoteException e) {// TODO Auto-generated catch blocke.printStackTrace();}}class ServiceConnectionImpl implements ServiceConnection {@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {student = StudentAidl.Stub.asInterface(service);}@Overridepublic void onServiceDisconnected(ComponentName name) {student = null;}}@Overrideprotected void onDestroy() {// TODO Auto-generated method stubsuper.onDestroy();if(sci != null) {unbindService(sci);}}}

aidl文件与上一个aidl文件相同

package com.example.remoteaidi.aidl;interface StudentAidl {String getStudent(int no);}


布局文件

        


清单文件
<?xml version="1.0" encoding="utf-8"?>                                                                                    





更多相关文章

  1. Android学习——在Android中使用OpenCV的第一个程序
  2. android操作xml
  3. cordova入门教程(五)给android apk签名
  4. Android组件的样式
  5. 2011.07.05(2)——— android PopupWindow
  6. Android内核的根文件系统
  7. Android文件存储
  8. android操作xml
  9. Android多媒体开发(5)————利用Android(安卓)AudioTrack播放mp3

随机推荐

  1. Android的16进制颜色值
  2. Android跑起Cocos2d-x的SimpleGame
  3. Android应用开发之(让你的应用向后兼容)
  4. 2012-04-12 21:24 Android(安卓)开发中发
  5. Android(安卓)Toast自定义
  6. 利用Fiddler对Android模拟器网络请求进行
  7. AsyncTask的用法总结
  8. 从实际问题中分析 Android中@id与@+id区
  9. Android帧缓冲区(Frame Buffer)硬件抽象层(H
  10. [Android]如何做一个崩溃率少于千分之三