Service n Service q q q Activity Service

  • Slides: 118
Download presentation

Service简介 n Service使用方法 q q q 启动方式 绑定方式 混合方式 Activity Service

Service简介 n Service使用方法 q q q 启动方式 绑定方式 混合方式 Activity Service

本地服务 n 服务的启动和停止 q 1. 2. 3. 实现Service的最小代码集 import android. app. Service; import android.

本地服务 n 服务的启动和停止 q 1. 2. 3. 实现Service的最小代码集 import android. app. Service; import android. content. Intent; import android. os. IBinder; 引入必要的包 4. 5. 6. 7. 8. 9. 10. public class Random. Service extends Service{ @Override public IBinder on. Bind(Intent intent) { return null; } } q 继承Service 重载On. Bind() on. Bind()函数是在Service被绑定后调用的函数,能够返 回Service的对象

本地服务 q 注册Service n n 1. 在Android. Manifest. xml文件中注册,否则,这个Service 根本无法启� Android. Manifest. xml文件中注册Service的代�如下 <service

本地服务 q 注册Service n n 1. 在Android. Manifest. xml文件中注册,否则,这个Service 根本无法启� Android. Manifest. xml文件中注册Service的代�如下 <service android: name=". Random. Service"/> n 使用<service>��声明服�,其中的 android: name表示 的是Service的�名称, 一定要与用户建立的Service� 名 称一致

本地服务 q 启动Service n 1. 2. q 显示启动 q 隐式启动 显示启动:在Intent中指明Service所在的�,并�用 start. Service(Intent)函数启� Service,示例代�如下

本地服务 q 启动Service n 1. 2. q 显示启动 q 隐式启动 显示启动:在Intent中指明Service所在的�,并�用 start. Service(Intent)函数启� Service,示例代�如下 final Intent service. Intent = new Intent(this, Random. Service. class); start. Service(service. Intent); q 在Intent中指明启动的Service在Random. Serevice. class中

本地服务 n 1. 2. 3. 4. 5. 1. 2. 隐式启动:在注册Service�,声明 Intent-filter的action属 性 <service android:

本地服务 n 1. 2. 3. 4. 5. 1. 2. 隐式启动:在注册Service�,声明 Intent-filter的action属 性 <service android: name=". Random. Service"> <intent-filter> <action android: name="edu. hrbeu. Random. Service" /> </intent-filter> 声明action属性 </service> q 设置Intent的action属性,可以在不声明Service所在类的情 况下启动服务 q 隐式启动的代码如下: final Intent service. Intent = new Intent(); service. Intent. set. Action("edu. hrbeu. Random. Service"); 隐式启动

本地服务 q 停止Service n 将启动Service的Intent��� stop. Service(Intent)函数即 可,示例代�如下 1. stop. Service(service. Intent); q 在调用start.

本地服务 q 停止Service n 将启动Service的Intent��� stop. Service(Intent)函数即 可,示例代�如下 1. stop. Service(service. Intent); q 在调用start. Service(Intent)函数首次启动Service后,系统会 先后调用on. Create()和on. Start() q 再次调用start. Service(Intent)函数,系统则仅调用on. Start(), 而不再调用on. Create() q 在调用stop. Service(Intent)函数停止Service时,系统会调用 on. Destroy() q 无论调用过多少次start. Service(Intent),在调用stop. Service (Intent)函数时,系统仅调用on. Destroy()一次

本地服务 q 示例 Simple. Random. Service. Demo以 显式启动服务 n n n 在 程中创建Random. Service服

本地服务 q 示例 Simple. Random. Service. Demo以 显式启动服务 n n n 在 程中创建Random. Service服 务,该服务启动后会产生一个随 机数,使用Toast显示在屏幕上 “启动Service”按钮调用 start. Service(Intent)函数,启动 Random. Service服务 “停止Service”按钮调用 stop. Service(Intent)函数,停止 Random. Service服务

本地服务 q 1. Random. Service. java文件的代�如下 package edu. hrbeu. Simple. Random. Service. Demo; 2.

本地服务 q 1. Random. Service. java文件的代�如下 package edu. hrbeu. Simple. Random. Service. Demo; 2. 3. 4. 5. 6. import android. app. Service; import android. content. Intent; import android. os. IBinder; import android. widget. Toast; 7. 8. public class Random. Service extends Service{ 9. 10. 11. 12. 13. 14. 15. 16. @Override public void on. Create() { super. on. Create(); Toast. make. Text(this, "(1) 调用on. Create()", Toast. LENGTH_LONG). show(); }

本地服务 17. 18. 19. 20. @Override public void on. Start(Intent intent, int start. Id)

本地服务 17. 18. 19. 20. @Override public void on. Start(Intent intent, int start. Id) { super. on. Start(intent, start. Id); Toast. make. Text(this, "(2) 调用on. Start()", Toast. LENGTH_SHORT). show(); 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. double random. Double = Math. random(); String msg = "随机数:"+ String. value. Of(random. Double); Toast. make. Text(this, msg, Toast. LENGTH_SHORT). show(); } @Override public void on. Destroy() { super. on. Destroy(); Toast. make. Text(this, "(3) 调用on. Destroy()", Toast. LENGTH_SHORT). show(); }. @Override public IBinder on. Bind(Intent intent) { return null; } }

本地服务 q 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12.

本地服务 q 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. Android. Manifest. xml文件的代�如下 <? xml version="1. 0" encoding="utf-8"? > <manifest xmlns: android="http: //schemas. android. com/apk/res/android" package="edu. hrbeu. Simple. Random. Service. Demo" android: version. Code="1" android: version. Name="1. 0"> <application android: icon="@drawable/icon" android: label="@string/app_name"> <activity android: name=". Simple. Random. Service. Demo" android: label="@string/app_name"> <intent-filter> <action android: name="android. intent. action. MAIN" /> <category android: name="android. intent. category. LAUNCHER" /> </intent-filter> </activity> 14. <service android: name=". Random. Service"/> 15. </application> 16. <uses-sdk android: min. Sdk. Version="3" /> 17. </manifest>

本地服务 q Simple. Random. Service. Demo. java文件的代�如下 1. package edu. hrbeu. Simple. Random. Service.

本地服务 q Simple. Random. Service. Demo. java文件的代�如下 1. package edu. hrbeu. Simple. Random. Service. Demo; 2. 3. 4. 5. 6. 7. import android. app. Activity; import android. content. Intent; import android. os. Bundle; import android. view. View; import android. widget. Button; 8. 9. 10. 11. 12. 13. 14. public class Simple. Random. Service. Demo extends Activity { @Override public void on. Create(Bundle saved. Instance. State) { super. on. Create(saved. Instance. State); set. Content. View(R. layout. main);

本地服务 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27.

本地服务 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. } n Button start. Button = (Button)find. View. By. Id(R. id. start); Button stop. Button = (Button)find. View. By. Id(R. id. stop); final Intent service. Intent = new Intent(this, Random. Service. class); start. Button. set. On. Click. Listener(new Button. On. Click. Listener(){ public void on. Click(View view){ start. Service(service. Intent); } }); stop. Button. set. On. Click. Listener(new Button. On. Click. Listener(){ public void on. Click(View view){ stop. Service(service. Intent); } }); } 第 20行和第 25行分�是启�和停止 Service的代�

本地服务 1. 2. public class Math. Service extends Service{ private final IBinder m. Binder

本地服务 1. 2. public class Math. Service extends Service{ private final IBinder m. Binder = new Local. Binder(); 3. public class Local. Binder extends Binder{ Math. Service get. Service() { return Math. Service. this; } } 4. 5. 6. 7. 8. 9. @Override public IBinder on. Bind(Intent intent) { return m. Binder; } 10. 11. 12. 13. 14. } n 当Service被�定�,系�会�用 on. Bind()函数,通� on. Bind()函数的返回�,将 Service�象返回��用者

本地服务 q 1. 2. 绑定服务:bind. Service()函数 final Intent service. Intent = new Intent(this, Math.

本地服务 q 1. 2. 绑定服务:bind. Service()函数 final Intent service. Intent = new Intent(this, Math. Service. class); bind. Service(service. Intent, m. Connection, Context. BIND_AUTO_CREATE); Activity bind. Service. Connection on. Service. Connected on. Service. Dis. Connected Android OS

本地服务 n 单向调用 Acitvity start. Activity() on. Create() on. Start() on. Resume() …… Intent

本地服务 n 单向调用 Acitvity start. Activity() on. Create() on. Start() on. Resume() …… Intent Acitvity Service start. Service() on. Create() on. Start() stop. Service() on. Destory() Intent Andriod操作系统

本地服务 n 双向调用 Acitvity start. Activity. For. Result() on. Activity. Result() get. Service() Acitvity

本地服务 n 双向调用 Acitvity start. Activity. For. Result() on. Activity. Result() get. Service() Acitvity on. Create() on. Start() on. Resume() …… bind. Service() un. Bind. Service() set. Result() finish() request. Code on. Destory() on. Bind() int Add() IBinder Local. Binder Intent Service Result. Code Intent BIND_AUTO_CREATE Andriod操作系统 IBinder Service. Connection Local. Binder on. Service. Connected on. Service. Dis. Connected

本地服务 n 第 1个参数中将Intent传递给bind. Service()函数,声明需要 启动的Service n 第 3个参数Context. BIND_AUTO_CREATE表明只要绑定 存在,就自动建立Service;同时也告知Android系统,这 个Service的重要程度与调用者相同,除非考虑终止调用 者,否则不要关闭这个Service n

本地服务 n 第 1个参数中将Intent传递给bind. Service()函数,声明需要 启动的Service n 第 3个参数Context. BIND_AUTO_CREATE表明只要绑定 存在,就自动建立Service;同时也告知Android系统,这 个Service的重要程度与调用者相同,除非考虑终止调用 者,否则不要关闭这个Service n 第 2个参数是Service. Connnection q 为了绑定方式使用Service,调用者需要声明一个 Service. Connnection,并重载内部的on. Service. Connected() 方法和on. Service. Disconnected方法 § 当绑定成功后,系统将调用Service. Connnection的 on. Service. Connected()方法 § 而当绑定意外断开后,系统将调用Service. Connnection中 的on. Service. Disconnected方法

本地服务 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. private Service. Connection

本地服务 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. private Service. Connection m. Connection = new Service. Connection() { @Override public void on. Service. Connected(Component. Name name, IBinder service) { math. Service = ((Math. Service. Local. Binder)service). get. Service(); } @Override public void on. Service. Disconnected(Component. Name name) { math. Service = null; } }; n 在第 4行代�中,�定成功后通� get. Service()�取 Service�象,��便可以�用 Service中的方法和属性 n 第 8行代�将 Service�象�置� null,表示�定意外失效, Service�例不再可用

本地服务 q 取消绑定:unbind. Service()方法,并将 Service. Connnection��� unbind. Service()方法 n 需注意的是,unbind. Service()方法成功后,系�并不会 �用 on. Service.

本地服务 q 取消绑定:unbind. Service()方法,并将 Service. Connnection��� unbind. Service()方法 n 需注意的是,unbind. Service()方法成功后,系�并不会 �用 on. Service. Disconnected(),因� on. Service. Disconnected()�在 意外断开绑定时才被调用 unbind. Service(m. Connection);

本地服务 q q 1. 在Simple. Math. Service. Demo示例中,Math. Service. java 文件是描述Service的文件 Math. Service. java文件的完整代�如下

本地服务 q q 1. 在Simple. Math. Service. Demo示例中,Math. Service. java 文件是描述Service的文件 Math. Service. java文件的完整代�如下 package edu. hrbeu. Simple. Math. Service. Demo; 2. 3. 4. 5. 6. 7. 8. import android. app. Service; import android. content. Intent; import android. os. Binder; import android. os. IBinder; import android. widget. Toast;

本地服务 9. public class Math. Service extends Service{ private final IBinder m. Binder =

本地服务 9. public class Math. Service extends Service{ private final IBinder m. Binder = new Local. Binder(); 10. 11. 13. 14. 15. 16. 17. public class Local. Binder extends Binder{ Math. Service get. Service() { return Math. Service. this; } } 18. 19. 20. 21. 22. 23. 24. 25. @Override public IBinder on. Bind(Intent intent) { Toast. make. Text(this, "本地绑定:Math. Service", Toast. LENGTH_SHORT). show(); return m. Binder; }

本地服务 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38.

本地服务 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. @Override public boolean on. Unbind(Intent intent){ Toast. make. Text(this, "取消本地绑定:Math. Service", Toast. LENGTH_SHORT). show(); return false; } public long Add(long a, long b){ return a+b; } }

本地服务 q Simple. Math. Service. Demo. java文件是界面的Activity文 件,�定和取消�定服�的代�在�个文件中 q Simple. Math. Service. Demo. java文件的完整代�如下

本地服务 q Simple. Math. Service. Demo. java文件是界面的Activity文 件,�定和取消�定服�的代�在�个文件中 q Simple. Math. Service. Demo. java文件的完整代�如下 1. package edu. hrbeu. Simple. Math. Service. Demo; 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. import android. app. Activity; import android. content. Component. Name; import android. content. Context; import android. content. Intent; import android. content. Service. Connection; import android. os. Bundle; import android. os. IBinder; import android. view. View; import android. widget. Button; import android. widget. Text. View;

本地服务 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26.

本地服务 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. public class Simple. Math. Service. Demo extends Activity { private Math. Service math. Service; private boolean is. Bound = false; Text. View label. View; @Override public void on. Create(Bundle saved. Instance. State) { super. on. Create(saved. Instance. State); set. Content. View(R. layout. main); label. View = (Text. View)find. View. By. Id(R. id. label); Button bind. Button = (Button)find. View. By. Id(R. id. bind); Button unbind. Button = (Button)find. View. By. Id(R. id. unbind); Button comput. Button = (Button)find. View. By. Id(R. id. compute); bind. Button. set. On. Click. Listener(new View. On. Click. Listener(){ @Override public void on. Click(View v) { if(!is. Bound){

本地服务 32. 33. 34. 35. 36. 37. final Intent service. Intent = new Intent(Simple.

本地服务 32. 33. 34. 35. 36. 37. final Intent service. Intent = new Intent(Simple. Math. Service. Demo. this, Math. Service. class); bind. Service(service. Intent, m. Connection, Context. BIND_AUTO_CREATE); is. Bound = true; } } }); 38. 39. . 40. 41. 42. 43. 44. 45. 46. 47. 48. unbind. Button. set. On. Click. Listener(new View. On. Click. Listener(){ @Override public void on. Click(View v) { if(is. Bound){ is. Bound = false; unbind. Service(m. Connection); math. Service = null; } } });

本地服务 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61.

本地服务 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. comput. Button. set. On. Click. Listener(new View. On. Click. Listener(){ @Override public void on. Click(View v) { if (math. Service == null){ label. View. set. Text("未绑定服务"); return; } long a = Math. round(Math. random()*100); long b = Math. round(Math. random()*100); long result = math. Service. Add(a, b); String msg = String. value. Of(a)+" + "+String. value. Of(b)+ " = "+String. value. Of(result); label. View. set. Text(msg); } }); }

本地服务 67. private Service. Connection m. Connection = new Service. Connection() { 68. @Override

本地服务 67. private Service. Connection m. Connection = new Service. Connection() { 68. @Override 69. public void on. Service. Connected(Component. Name name, IBinder service) { 70. math. Service = ((Math. Service. Local. Binder)service). get. Service(); 71. } 72. 73. @Override 74. public void on. Service. Disconnected(Component. Name name) { 75. math. Service = null; 76. } 77. }; 78. }

本地服务 n 1. 2. 3. 4. 5. 6. 实现Java的Runnable接口,并重载run()方法。在run()中 放置代码的主体部分 private Runnable backgroud. Work

本地服务 n 1. 2. 3. 4. 5. 6. 实现Java的Runnable接口,并重载run()方法。在run()中 放置代码的主体部分 private Runnable backgroud. Work = new Runnable(){ @Override public void run() { //过程代码 } }; 实现Runable接口 重载run()

本地服务 n 创建Thread�象 ,并将上面实现的Runnable对象作为参 数传递给Thread对象 Runnable参数传递 private Thread work. Thread; work. Thread = new

本地服务 n 创建Thread�象 ,并将上面实现的Runnable对象作为参 数传递给Thread对象 Runnable参数传递 private Thread work. Thread; work. Thread = new Thread(null, backgroud. Work, "Work. Thread"); 1. 2. 创建Thread对象 q q q n 1. Thread的构造函数中,第 1个参数用来表示线程组 第 2个参数是需要执行的Runnable对象 第 3个参数是线程的名称 �用 start()方法启��程 work. Thread. start();

本地服务 q Handler使用 监听器 主线程消息 队列 Handler 子线程 post Runnable Send. Message View 消息响应函数

本地服务 q Handler使用 监听器 主线程消息 队列 Handler 子线程 post Runnable Send. Message View 消息响应函数 界面事件响应 Handle. Message

本地服务 q 使用Handler将子线程数据更新到用户界面(主线程) n n Handler允�将 Runnable�象 发送到线程的消息队列中, 每个Handler�象 绑定到一个单独的线程和消息队列上 当用�建立一个新的 Handler�象,通� post()方法将 Runnable�象从

本地服务 q 使用Handler将子线程数据更新到用户界面(主线程) n n Handler允�将 Runnable�象 发送到线程的消息队列中, 每个Handler�象 绑定到一个单独的线程和消息队列上 当用�建立一个新的 Handler�象,通� post()方法将 Runnable�象从 后台线程发送到GUI�程 的消息队列中, 当Runnable�象通�消息�列后,�个 Runnable�象将 被运行 1. 公有、静态函数, 将被子线程调用 运行在子线程内部 Handler: : post() private static Handler handler = new Handler(); 创建Handler 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. public static void Update. GUI(double refresh. Double){ handler. post(Refresh. Lable); } private static Runnable Refresh. Lable = new Runnable(){ @Override public void run() { 实现Runnable接口 //过程代码 Handler: : post()参数 } 运行在主线程,用于更新界面 };

本地服务 q q 在Thread. Random. Service. Demo示例中, Random. Service. java文件是描述Service的文件,用来 �建�程、�生随机数和�用界面更新函数 Random. Service. java文件的完整代�如下

本地服务 q q 在Thread. Random. Service. Demo示例中, Random. Service. java文件是描述Service的文件,用来 �建�程、�生随机数和�用界面更新函数 Random. Service. java文件的完整代�如下 1. package edu. hrbeu. Thread. Random. Service. Demo; 2. 3. 4. 5. 6. import android. app. Service; import android. content. Intent; import android. os. IBinder; import android. widget. Toast; 7. 8. public class Random. Service extends Service{ 9. 10. 11. private Thread work. Thread;

本地服务 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24.

本地服务 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. @Override public void on. Create() { super. on. Create(); Toast. make. Text(this, "(1) 调用on. Create()", Toast. LENGTH_LONG). show(); work. Thread = new Thread (null, backgroud. Work, "Work. Thread"); } @Override public void on. Start(Intent intent, int start. Id) { super. on. Start(intent, start. Id); Toast. make. Text(this, "(2) 调用on. Start()", Toast. LENGTH_SHORT). show(); if (!work. Thread. is. Alive()){ work. Thread. start(); } }

本地服务 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42.

本地服务 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. @Override public void on. Destroy() { super. on. Destroy(); Toast. make. Text(this, "(3) 调用on. Destroy()", Toast. LENGTH_SHORT). show(); work. Thread. interrupt(); } @Override public IBinder on. Bind(Intent intent) { return null; }

本地服务 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55.

本地服务 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. private Runnable backgroud. Work = new Runnable(){ @Override public void run() { try { while(!Thread. interrupted()){ double random. Double = Math. random(); Thread. Random. Service. Demo. Update. GUI(random. Double); Thread. sleep(1000); } } catch (Interrupted. Exception e) { e. print. Stack. Trace(); } } }; }

本地服务 q q 1. Thread. Random. Service. Demo. java文件是界面的Activity 文件,封装Handler的界面更新函数就在这个文件中 Thread. Random. Service. Demo.

本地服务 q q 1. Thread. Random. Service. Demo. java文件是界面的Activity 文件,封装Handler的界面更新函数就在这个文件中 Thread. Random. Service. Demo. java文件的完整代� package edu. hrbeu. Thread. Random. Service. Demo; 2. 3. 4. 5. 6. 7. 8. 9. 10. import android. app. Activity; import android. content. Intent; import android. os. Bundle; import android. os. Handler; import android. view. View; import android. widget. Button; import android. widget. Text. View;

本地服务 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23.

本地服务 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. public class Thread. Random. Service. Demo extends Activity { private static Handler handler = new Handler(); private static Text. View label. View = null; private static double random. Double ; public static void Update. GUI(double refresh. Double){ random. Double = refresh. Double; handler. post(Refresh. Lable); } private static Runnable Refresh. Lable = new Runnable(){ @Override public void run() { label. View. set. Text(String. value. Of(random. Double)); } };

本地服务 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41.

本地服务 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. } @Override public void on. Create(Bundle saved. Instance. State) { super. on. Create(saved. Instance. State); set. Content. View(R. layout. main); label. View = (Text. View)find. View. By. Id(R. id. label); Button start. Button = (Button)find. View. By. Id(R. id. start); Button stop. Button = (Button)find. View. By. Id(R. id. stop); final Intent service. Intent = new Intent(this, Random. Service. class); start. Button. set. On. Click. Listener(new Button. On. Click. Listener(){ public void on. Click(View view){ start. Service(service. Intent); } }); stop. Button. set. On. Click. Listener(new Button. On. Click. Listener(){ public void on. Click(View view){ stop. Service(service. Intent); } }); }

远程服务 进程一 进程二 调用者 IPC Proxy Parcel func( ) Binder: : Transact() 服务提供者 func(

远程服务 进程一 进程二 调用者 IPC Proxy Parcel func( ) Binder: : Transact() 服务提供者 func( ) on. Transact()

远程服务 使用接口 bind. Service() un. Bind. Service() Math. Service int Add() on. Create() on.

远程服务 使用接口 bind. Service() un. Bind. Service() Math. Service int Add() on. Create() on. Start() IMath. Service. java on. Destory() Stub on. Bind() as. Interface() int Add() IMath. Service. Stub Intent BIND_AUTO_CREATE 定义接口 IMath. Service. aidl as. Interface() Acitvity 实现接口 IMath. Service. Stub Remote. Binder int Add() Service. Connection Remote. Binder on. Service. Connected on. Service. Dis. Connected Andriod操作系统 proxy int Add()

远程服务 n 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12.

远程服务 n 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. IMath. Service. java的完整代码 package edu. hrbeu. Remote. Math. Service. Demo; import java. lang. String; import android. os. Remote. Exception; import android. os. IBinder; import android. os. IInterface; import android. os. Binder; import android. os. Parcel; public interface IMath. Service extends android. os. IInterface{ /** Local-side IPC implementation stub class. */ public static abstract class Stub extends android. os. Binder implements edu. hrbeu. Remote. Math. Service. Demo. IMath. Service{ private static final java. lang. String DESCRIPTOR = "edu. hrbeu. Remote. Math. Service. Demo. IMath. Service"; /** Construct the stub at attach it to the interface. */ public Stub(){ this. attach. Interface(this, DESCRIPTOR); }

远程服务 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28.

远程服务 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. /** * Cast an IBinder object into an IMath. Service interface, * generating a proxy if needed. */ public static edu. hrbeu. Remote. Math. Service. Demo. IMath. Service as. Interface(android. os. IBinder obj){ if ((obj==null)) { return null; } android. os. IInterface iin = (android. os. IInterface) obj. query. Local. Interface(DESCRIPTOR); if (((iin!=null)&&(iin instanceof edu. hrbeu. Remote. Math. Service. Demo. IMath. Service))) { return ((edu. hrbeu. Remote. Math. Service. Demo. IMath. Service)iin); } return new edu. hrbeu. Remote. Math. Service. Demo. IMath. Service. Stub. Proxy(obj); } public android. os. IBinder as. Binder(){ return this; }

远程服务 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45.

远程服务 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. public boolean on. Transact(int code, android. os. Parcel data, android. os. Parcel reply, int flags) throws android. os. Remote. Exception{ switch (code) { case INTERFACE_TRANSACTION: { reply. write. String(DESCRIPTOR); return true; } case TRANSACTION_Add: { data. enforce. Interface(DESCRIPTOR); long _arg 0; _arg 0 = data. read. Long(); long _arg 1; _arg 1 = data. read. Long(); long _result = this. Add(_arg 0, _arg 1); reply. write. No. Exception(); reply. write. Long(_result); return true;

远程服务 51. } 52. } 53. return super. on. Transact(code, data, reply, flags); 54.

远程服务 51. } 52. } 53. return super. on. Transact(code, data, reply, flags); 54. } 55. private static class Proxy implements edu. hrbeu. Remote. Math. Service. Demo. IMath. Service{ 56. private android. os. IBinder m. Remote; 57. Proxy(android. os. IBinder remote){ 58. m. Remote = remote; 59. } 60. public android. os. IBinder as. Binder(){ 61. return m. Remote; 62. } 63. public java. lang. String get. Interface. Descriptor(){ 64. return DESCRIPTOR; 65. } 66. public long Add(long a, long b) throws android. os. Remote. Exception{ 67. android. os. Parcel _data = android. os. Parcel. obtain(); 68. android. os. Parcel _reply = android. os. Parcel. obtain(); 69. long _result;

远程服务 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82.

远程服务 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. try { _data. write. Interface. Token(DESCRIPTOR); _data. write. Long(a); _data. write. Long(b); m. Remote. transact(Stub. TRANSACTION_Add, _data, _reply, 0); _reply. read. Exception(); _result = _reply. read. Long(); } finally { _reply. recycle(); _data. recycle(); } return _result; } } static final int TRANSACTION_Add = (IBinder. FIRST_CALL_TRANSACTION + 0); } public long Add(long a, long b) throws android. os. Remote. Exception; }

远程服务 n n 1. 在Remote. Math. Service. Demo示例中,跨�程服�的�� �是 Math. Service. java 下面是Math. Service.

远程服务 n n 1. 在Remote. Math. Service. Demo示例中,跨�程服�的�� �是 Math. Service. java 下面是Math. Service. java的完整代� package edu. hrbeu. Remote. Math. Service. Demo; 2. 3. 4. 5. 6. import android. app. Service; import android. content. Intent; import android. os. IBinder; import android. widget. Toast; 7. 8. 9. 10. 11. 12. 13. public class Math. Service extends Service{ private final IMath. Service. Stub m. Binder = new IMath. Service. Stub() { public long Add(long a, long b) { return a + b; 12. } 13. };

远程服务 q 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25.

远程服务 q 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 通过继承Service���跨�程服� @Override public IBinder on. Bind(Intent intent) { Toast. make. Text(this, "远程绑定:Math. Service", Toast. LENGTH_SHORT). show(); return m. Binder; } @Override public boolean on. Unbind (Intent intent){ Toast. make. Text(this, "取消远程绑定:Math. Service", Toast. LENGTH_SHORT). show(); return false; } }

远程服务 n 下图表示edu. hrbeu. Remote. Math. Service. Demo. apk文件 的保存位置

远程服务 n 下图表示edu. hrbeu. Remote. Math. Service. Demo. apk文件 的保存位置

远程服务 n n 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11.

远程服务 n n 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. Remote. Math. Service. Demo是一个没有Activity的示例,在 Android. Manifest. xml文件中,在<application>��下只 有一个<service>�� Android. Manifest. xml文件的完整代码如下 <? xml version="1. 0" encoding="utf-8"? > <manifest xmlns: android="http: //schemas. android. com/apk/res/android" package="edu. hrbeu. Remote. Math. Service. Demo" android: version. Code="1" android: version. Name="1. 0"> <application android: icon="@drawable/icon" android: label= "@string/app_name"> <service android: name=". Math. Service" android: process=": remote"> <intent-filter> <action android: name= "edu. hrbeu. Remote. Math. Service. Demo. Math. Service" /> </intent-filter> </service> </application> <uses-sdk android: min. Sdk. Version="3" /> </manifest>

远程服务 n 下图是Remote. Math. Service. Demo的文件�构

远程服务 n 下图是Remote. Math. Service. Demo的文件�构

远程服务 1. private IMath. Service math. Service; 2. 3. 4. 5. 6. 7. 8.

远程服务 1. private IMath. Service math. Service; 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. private Service. Connection m. Connection = new Service. Connection() { @Override public void on. Service. Connected(Component. Name name, IBinder service) { math. Service = IMath. Service. Stub. as. Interface(service); } @Override public void on. Service. Disconnected(Component. Name name){ math. Service = null; } };

远程服务 n n 1. 2. 3. 绑定服务时,首先通过set. Action()方法声明服���,然 后�用 bind. Service()�定服� 服���必�与跨�程服�在 Android. Manifest.

远程服务 n n 1. 2. 3. 绑定服务时,首先通过set. Action()方法声明服���,然 后�用 bind. Service()�定服� 服���必�与跨�程服�在 Android. Manifest. xml文件 中声明的服���完全相同。因此本示例的服���� edu. hrbeu. Remote. Math. Service. Demo. Math. Service,与 跨�程服�示例 Remote. Math. Service. Demo在 Android. Manifest. xml文件声明的服���一致 final Intent service. Intent = new Intent(); service. Intent. set. Action("edu. hrbeu. Remote. Math. Service. Demo. Math. Service"); bind. Service(service. Intent, m. Connection, Context. BIND_AUTO_CREATE);

远程服务 n 1. 下面是Remote. Math. Caller. Demo. java文件的完整代� package edu. hrbeu. Remote. Math. Caller.

远程服务 n 1. 下面是Remote. Math. Caller. Demo. java文件的完整代� package edu. hrbeu. Remote. Math. Caller. Demo; 2. 3. import edu. hrbeu. Remote. Math. Service. Demo. IMath. Service; 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. import android. app. Activity; import android. content. Component. Name; import android. content. Context; import android. content. Intent; import android. content. Service. Connection; import android. os. Bundle; import android. os. IBinder; import android. os. Remote. Exception; import android. view. View; import android. widget. Button; import android. widget. Text. View;

远程服务 16. 17. public class Remote. Math. Caller. Demo extends Activity { 18. private

远程服务 16. 17. public class Remote. Math. Caller. Demo extends Activity { 18. private IMath. Service math. Service; 18. 20. private Service. Connection m. Connection = new Service. Connection() { 21. @Override 22. public void on. Service. Connected(Component. Name name, IBinder service) { 23. math. Service = IMath. Service. Stub. as. Interface(service); 24. } 25. @Override 26. public void on. Service. Disconnected(Component. Name name) { 27. math. Service = null; 28. } 29. }; 30.

远程服务 31. 32. 33. 34. 35. 36. private boolean is. Bound = false; Text.

远程服务 31. 32. 33. 34. 35. 36. private boolean is. Bound = false; Text. View label. View; @Override public void on. Create(Bundle saved. Instance. State) { super. on. Create(saved. Instance. State); set. Content. View(R. layout. main); 37. 38. 39. 40. 41. label. View = (Text. View)find. View. By. Id(R. id. label); Button bind. Button = (Button)find. View. By. Id(R. id. bind); Button unbind. Button = (Button)find. View. By. Id(R. id. unbind); Button comput. Button = (Button)find. View. By. Id(R. id. compute_add); 42. 43. 44. bind. Button. set. On. Click. Listener(new View. On. Click. Listener(){ @Override

远程服务 public void on. Click(View v) { if(!is. Bound){ final Intent service. Intent =

远程服务 public void on. Click(View v) { if(!is. Bound){ final Intent service. Intent = new Intent(); service. Intent. set. Action("edu. hrbeu. Remote. Math. Service. Demo. Math. Service"); bind. Service(service. Intent, m. Connection, Context. BIND_AUTO_CREATE); is. Bound = true; } } 45. 46. 47. 48. 49. 50. 51. 52. 53. }); 54. 55. 56. 57. 58. 59. unbind. Button. set. On. Click. Listener(new View. On. Click. Listener(){ @Override public void on. Click(View v) { if(is. Bound){ is. Bound = false;

远程服务 60. 61. 62. unbind. Service(m. Connection); math. Service = null; } } 63.

远程服务 60. 61. 62. unbind. Service(m. Connection); math. Service = null; } } 63. 64. }); 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. comput. Button. set. On. Click. Listener(new View. On. Click. Listener(){ @Override public void on. Click(View v) { if (math. Service == null){ label. View. set. Text("未绑定跨进程服务"); return; } long a = Math. round(Math. random()*100); long b = Math. round(Math. random()*100); long result = 0;

远程服务 try { result = math. Service. Add(a, b); 76. 77. } catch (Remote.

远程服务 try { result = math. Service. Add(a, b); 76. 77. } catch (Remote. Exception e) { e. print. Stack. Trace(); } String msg = String. value. Of(a)+" + "+String. value. Of(b)+ " = "+String. value. Of(result); label. View. set. Text(msg); } 78. 79. 80. 81. 82. 83. 84. }); 85. } 86. 87. }

远程服务 All. Result. aidl Acitvity Math. Service parcelable All. Result. java int Add. Result

远程服务 All. Result. aidl Acitvity Math. Service parcelable All. Result. java int Add. Result int Sub. Result 获取结果 Proxy IPC Compute. All() 计算结果 int Add. Result int Sub. Result All. Result(…) 构造对象 All. Result(Parcel parcel) writeto. Pracel( ) CREATOR Andriod操作系统

远程服务 q 1. 2. 首先建立All. Result. aidl文件,声明All. Result� package edu. hrbeu. Parcel. Math. Service.

远程服务 q 1. 2. 首先建立All. Result. aidl文件,声明All. Result� package edu. hrbeu. Parcel. Math. Service. Demo; parcelable All. Result; n 在第 2行代�中使用 parcelable声明自定��,��其他 的AIDL文件就可以使用�个自定�的�

远程服务 q 下图是Parcel. Math. Service. Demo的文件�构

远程服务 q 下图是Parcel. Math. Service. Demo的文件�构

远程服务 q 1. 2. 下面是IMath. Service. aidl文件的代码 package edu. hrbeu. Parcel. Math. Service. Demo;

远程服务 q 1. 2. 下面是IMath. Service. aidl文件的代码 package edu. hrbeu. Parcel. Math. Service. Demo; import edu. hrbeu. Parcel. Math. Service. Demo. All. Result; 3. 4. 5. 6. 7. interface IMath. Service { long Add(long a, long b); All. Result Compute. All(long a, long b); } n n 第 2行代码中引入了 edu. hrbeu. Parcel. Math. Service. Demo. All. Result,才能够使 用自定义数据结构All. Result 第 6行代��全运算增加了新的函数 Compute. All(),�函 数的返回�就是在 All. Result. aidl文件中定� All. Result

远程服务 q q 1. 构造All. Result� 。All. Result�除了基本的构造函数以外, �需要有以 Parcel�象��入的构造函数,并且需要 重�打包函数 write. To. Parcel()

远程服务 q q 1. 构造All. Result� 。All. Result�除了基本的构造函数以外, �需要有以 Parcel�象��入的构造函数,并且需要 重�打包函数 write. To. Parcel() All. Result. java文件的完整代�如下 package edu. hrbeu. Parcel. Math. Service. Demo; 2. 3. 4. import android. os. Parcel; import android. os. Parcelable; 5. 6. 7. 8. 9. 10. 11. public class All. Result implements Parcelable { public long Add. Result; public long Sub. Result; public long Mul. Result; public double Div. Result;

远程服务 12. 13. 14. 15. 16. 17. public All. Result(long add. Rusult, long sub.

远程服务 12. 13. 14. 15. 16. 17. public All. Result(long add. Rusult, long sub. Result, long mul. Result, double div. Result){ Add. Result = add. Rusult; Sub. Result = sub. Result; Mul. Result = mul. Result; Div. Result = div. Result; } 18. 19. 20. 21. 22. 23. 24. public All. Result(Parcel parcel) { Add. Result = parcel. read. Long(); Sub. Result = parcel. read. Long(); Mul. Result = parcel. read. Long(); Div. Result = parcel. read. Double(); } 25. 26. 27. 28. 29. @Override public int describe. Contents() { return 0; }

远程服务 30. 31. 32. @Override public void write. To. Parcel(Parcel dest, int flags) {

远程服务 30. 31. 32. @Override public void write. To. Parcel(Parcel dest, int flags) { dest. write. Long(Add. Result); 34. dest. write. Long(Sub. Result); dest. write. Long(Mul. Result); dest. write. Double(Div. Result); } 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 33. 34. 35. 36. 45. 46. 47. 48. public static final Parcelable. Creator<All. Result> CREATOR = new Parcelable. Creator<All. Result>(){ public All. Result create. From. Parcel(Parcel parcel){ return new All. Result(parcel); } public All. Result[] new. Array(int size){ return new All. Result[size]; } }; }

远程服务 q q 1. 2. 3. 4. 5. 6. 7. 8. 9. 在Math. Service.

远程服务 q q 1. 2. 3. 4. 5. 6. 7. 8. 9. 在Math. Service. java文件中,增加了用来�行全运算的 Comput. All()函数,并将运算�果保存在 All. Result�象 中 Comput. All()函数��代�如下 @Override public All. Result Compute. All(long a, long b) throws Remote. Exception { long add. Rusult = a + b; long sub. Result = a - b; long mul. Result = a * b; double div. Result = (double) a / (double)b; All. Result all. Result = new All. Result(add. Rusult, sub. Result, mul. Result, div. Result); return all. Result; }

远程服务 q q Parcel. Math. Caller. Demo示例 是Parcel. Math. Service. Demo 示例中Math. Service服务的 调用者

远程服务 q q Parcel. Math. Caller. Demo示例 是Parcel. Math. Service. Demo 示例中Math. Service服务的 调用者 Parcel. Math. Caller. Demo文件 结构如右图 n 其中All. Result. aidl、 All. Result. java和 IMath. Service. aidl文件务必 与Parcel. Math. Service. Demo 示例的三个文件完全一致, 否则会出现错误

远程服务 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13.

远程服务 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. comput. All. Button. set. On. Click. Listener(new View. On. Click. Listener(){ @Override public void on. Click(View v) { if (math. Service == null){ label. View. set. Text("未绑定跨进程服务"); return; } long a = Math. round(Math. random()*100); long b = Math. round(Math. random()*100); All. Result result = null; try { result = math. Service. Compute. All(a, b); } catch (Remote. Exception e) { e. print. Stack. Trace(); }

远程服务 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. String msg =

远程服务 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. String msg = ""; if (result != null){ msg += String. value. Of(a)+" + "+String. value. Of(b)+" = "+String. value. Of(result. Add. Result)+"n"; msg += String. value. Of(a)+" - "+String. value. Of(b)+" = "+String. value. Of(result. Sub. Result)+"n"; msg += String. value. Of(a)+" * "+String. value. Of(b)+" = "+String. value. Of(result. Mul. Result)+"n"; msg += String. value. Of(a)+" / "+String. value. Of(b)+" = "+String. value. Of(result. Div. Result); } label. View. set. Text(msg); } });