2. 客户端实现:
(1)目录结构,如下图:

(2)将服务器端的IAIDLService.aidl,Person.aidl和Person.java文件拷贝到本工程中,如上图所示:
(3)res/layout/main.xml实现:

Java代码:

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

  2. <LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
  3. android:orientation = "vertical"
  4. android:layout_width = "fill_parent"
  5. android:layout_height = "fill_parent"
  6. >

  7. <TextView
  8. android:id = "@+id/name"
  9. android:layout_width = "wrap_content"
  10. android:layout_height = "wrap_content"
  11. />

  12. <Button
  13. android:id = "@+id/connection"
  14. android:layout_width = "wrap_content"
  15. android:layout_height = "wrap_content"
  16. android:text = "连接"
  17. />

  18. <Button
  19. android:id = "@+id/message"
  20. android:layout_width = "wrap_content"
  21. android:layout_height = "wrap_content"
  22. android:enabled = "false"
  23. android:text = "信息"
  24. />

  25. <Button
  26. android:id = "@+id/person"
  27. android:layout_width = "wrap_content"
  28. android:layout_height = "wrap_content"
  29. android:enabled = "false"
  30. android:text = "人"
  31. />

  32. </LinearLayout>
复制代码


(4)主Activity实现,从服务器端获取数据在客户端显示:

Java代码:

  1. package eoe.demo;

  2. import android.app.Activity;
  3. import android.content.ComponentName;
  4. import android.content.Intent;
  5. import android.content.ServiceConnection;
  6. import android.os.Bundle;
  7. import android.os.IBinder;
  8. import android.os.RemoteException;
  9. import android.view.View;
  10. import android.view.View.OnClickListener;
  11. import android.widget.Button;
  12. import android.widget.TextView;

  13. import com.focus.aidl.IAIDLService;
  14. import com.focus.aidl.Person;

  15. public class AIDLClientAcitivty extends Activity {

  16. private IAIDLService mAIDLService;
  17. private TextView mName;
  18. private Button mMessage;
  19. private Button mPerson;

  20. /**
  21. * 第一步,创建ServiceConnection对象,在onServiceConnected()方法中获取IAIDLService实现。
  22. */
  23. private ServiceConnection mServiceConnection = new ServiceConnection() {

  24. public void onServiceConnected(ComponentName name, IBinder service) {
  25. mAIDLService = IAIDLService.Stub.asInterface(service);

  26. mMessage.setEnabled(true);
  27. mPerson.setEnabled(true);
  28. }

  29. public void onServiceDisconnected(ComponentName name) {
  30. mAIDLService = null;

  31. mMessage.setEnabled(false);
  32. mPerson.setEnabled(false);
  33. }

  34. };

  35. @Override
  36. public void onCreate(Bundle savedInstanceState) {
  37. super.onCreate(savedInstanceState);

  38. setContentView(R.layout.main);
  39. mName = (TextView) findViewById(R.id.name);

  40. findViewById(R.id.connection).setOnClickListener(new OnClickListener() {
  41. public void onClick(View view) {

  42. /**
  43. * 第二步,单击"连接"按钮后用mServiceConnection去bind服务器端创建的Service。
  44. */
  45. Intent service = new Intent("com.focus.aidl.IAIDLService");
  46. bindService(service, mServiceConnection, BIND_AUTO_CREATE);

  47. }
  48. });

  49. mMessage = (Button) findViewById(R.id.message);
  50. mMessage.setOnClickListener(new OnClickListener() {
  51. public void onClick(View view) {


  52. /**
  53. * 第三步,从服务器端获取字符串。
  54. */
  55. try {
  56. mName.setText(mAIDLService.getName());
  57. } catch (RemoteException e) {
  58. e.printStackTrace();
  59. }
  60. }
  61. });

  62. mPerson = (Button) findViewById(R.id.person);
  63. mPerson.setOnClickListener(new OnClickListener() {
  64. public void onClick(View view) {
  65. /**
  66. * 第四步,从服务器端获取Person对象。
  67. */
  68. try {
  69. Person mPerson = mAIDLService.getPerson();
  70. mName.setText("姓名:" + mPerson.getName() + ", 年龄:" + mPerson.getAge());
  71. } catch (RemoteException e) {
  72. e.printStackTrace();
  73. }
  74. }
  75. });
  76. }

  77. }

更多相关文章

  1. android webview js
  2. Android系统默认Home应用程序(Launcher)的启动过程源代码分析(2)
  3. android如何取得本地通讯录的头像的原图的实现代码
  4. Android(安卓)获取手机信息实例详解
  5. Android即时通讯——融云——基本环境搭建(坑很多)
  6. 获取Android(安卓)手机设备信息:包括机型、操作系统版本号、手机
  7. Android(安卓)EditTextView 设置输入英文字母全部大写
  8. Android(安卓)获取View的高度和宽度
  9. Android(安卓)Source Code

随机推荐

  1. 在Android中创建和使用数据库
  2. Android(安卓)release版本apk添加数字签
  3. Android(安卓)JNI 机制
  4. Android时区问题
  5. Android调用系统自带的文件管理器进行文
  6. 移动互联网的新宠:Android之缤纷世界
  7. Android仿腾讯视频悬浮窗的实现
  8. 最牛逼android上的图表库MpChart(二) 折线
  9. Android(安卓)自定义URL Scheme
  10. 从0到1 Android安全学习之路 -- 环境篇