当前位置: 代码迷 >> Android >> Android之Service学习篇2:Service启动方式之boundService
  详细解决方案

Android之Service学习篇2:Service启动方式之boundService

热度:89   发布时间:2016-04-28 02:54:38.0
Android之Service学习篇二:Service启动方式之boundService

转于:

http://blog.csdn.net/wulianghuan/article/details/8600361

------------------------------------------------------------------------------

?

上一篇中介绍了Service的第一种方式,startService,这一篇来讲解一下另一张方式 bindService。

当创建一个能提供绑定功能的服务时,我们必须提供一个IBinder对象,客户端能使用这个对象与服务进行交换。在Android中有三种定义方式:

1、扩展Binder类 (条件:服务和应用在同一个进程当中,是最常见的情况)

2、使用Messager

3、使用AIDL (Android Interface Defination Language)

?

?

Bound Services

?

A bound service is the server in a client-server interface. A bound service allows components (such as activities) to bind to the service, send requests, receive responses, and even perform interprocess communication (IPC). A bound service typically lives only while it serves another application component and does not run in the background indefinitely.

?

这里以扩展Binder类为例讲解如何创建Bound Services.

?

Binder

extends?Object
implements?IBinder
java.lang.Object
????android.os.Binder

很明显,Binder实现了IBinder这接口。

?

通过扩展Binder类创建Bound Service的步骤如下:

a、在Service类中,创建一个Binder实例,包含客户端能调用的公共方法,返回当前服务对象;

b、在onBind()方法中返回Binder实例;

c、在客户端,从onServiceConnected方法中获得Binder实例。

?

工程目录结构:


运行界面:


源代码:

MainActivity.java:

?

[html]?view plaincopyprint?
?
  1. package?com.service.activity;??
  2. ??
  3. import?com.service.activity.BinderService.MyBinder;??
  4. import?android.app.Activity;??
  5. import?android.content.ComponentName;??
  6. import?android.content.Context;??
  7. import?android.content.Intent;??
  8. import?android.content.ServiceConnection;??
  9. import?android.os.Bundle;??
  10. import?android.os.IBinder;??
  11. import?android.view.View;??
  12. import?android.view.View.OnClickListener;??
  13. import?android.widget.Button;??
  14. ??
  15. public?class?MainActivity?extends?Activity?{??
  16. ????private?Button?btnStartService;??
  17. ????private?Button?btnstopService;??
  18. ????private?boolean?isConnected?=?false;??
  19. [email protected]
  20. ????public?void?onCreate(Bundle?savedInstanceState)?{??
  21. ????????super.onCreate(savedInstanceState);??
  22. ????????setContentView(R.layout.main);??
  23. ????????btnStartService?=?(Button)findViewById(R.id.btnStartService);??
  24. ????????btnStartService.setOnClickListener(listener);??
  25. ????????btnstopService?=?(Button)findViewById(R.id.btnStopService);??
  26. ????????btnstopService.setOnClickListener(listener);??
  27. ????}??
  28. ??????
  29. ????private?OnClickListener?listener?=?new?OnClickListener()?{??
  30. ??????????
  31. [email protected]
  32. ????????public?void?onClick(View?v)?{??
  33. ????????????switch?(v.getId())?{??
  34. ????????????case?R.id.btnStartService:??
  35. ????????????????funBindService();??
  36. ????????????????break;??
  37. ????????????case?R.id.btnStopService:??
  38. ????????????????funUnBindService();??
  39. ????????????????break;??
  40. ????????????default:??
  41. ????????????????break;??
  42. ????????????}??
  43. ????????}??
  44. ????????private?void?funBindService()?{??
  45. ????????????Intent?intent?=?new?Intent(MainActivity.this,?BinderService.class);??
  46. ????????????bindService(intent,?conn,?Context.BIND_AUTO_CREATE);??
  47. ????????}??
  48. ??
  49. ????????private?void?funUnBindService()?{??
  50. ????????????if(isConnected?==?true){??
  51. ????????????????unbindService(conn);??
  52. ????????????}??
  53. ????????}??
  54. ??????????
  55. ????};??
  56. ??????
  57. ????private?ServiceConnection?conn?=?new?ServiceConnection()?{??
  58. ??????????
  59. [email protected]
  60. ????????public?void?onServiceDisconnected(ComponentName?name)?{??
  61. ????????????isConnected?=?false;??
  62. ????????}??
  63. ????????//与Service连接时被回调??
  64. [email protected]
  65. ????????public?void?onServiceConnected(ComponentName?name,?IBinder?iBinder)?{??
  66. ????????????MyBinder?myBinder?=?(MyBinder)iBinder;??
  67. ????????????BinderService?service?=?myBinder.getService();??
  68. ????????????service.MyMethod();??
  69. ????????????isConnected?=?true;??
  70. ????????}??
  71. ????};??
  72. }??


BinderService.java

?

[html]?view plaincopyprint?
?
  1. package?com.service.activity;??
  2. ??
  3. import?android.app.Service;??
  4. import?android.content.Intent;??
  5. import?android.os.Binder;??
  6. import?android.os.IBinder;??
  7. import?android.util.Log;??
  8. ??
  9. public?class?BinderService?extends?Service{??
  10. ????private?static?final?String?TAG?=?"BinderService";??
  11. ????private?MyBinder?myBinder?=?new?MyBinder();??
  12. ????//内部类,扩展自Binder类??
  13. ????public?class?MyBinder?extends?Binder{??
  14. ????????public?BinderService?getService(){??
  15. ????????????return?BinderService.this;??
  16. ????????}??
  17. ????}??
  18. ????//复写onBind方法,并且返回IBinder的实现类??
  19. [email protected]
  20. ????public?IBinder?onBind(Intent?intent)?{??
  21. ????????Log.i(TAG,?"onBind");??
  22. ????????return?myBinder;??
  23. ????}??
  24. ??????
  25. ????public?void?MyMethod(){??
  26. ????????Log.i(TAG,?"MyMethod");??
  27. ????}??
  28. ??
  29. [email protected]
  30. ????public?boolean?onUnbind(Intent?intent)?{??
  31. ????????Log.i(TAG,?"onUnbind");??
  32. ????????return?super.onUnbind(intent);??
  33. ????}??
  34. ??????
  35. [email protected]
  36. ????public?void?onDestroy()?{??
  37. ????????Log.i(TAG,?"onDestroy");??
  38. ????????super.onDestroy();??
  39. ????}??
  40. ??????
  41. }??



?

?


main.xml:

?

?

[html]?view plaincopyprint?
?
  1. <?xml?version="1.0"?encoding="utf-8"?>??
  2. <LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"??
  3. ????android:layout_width="fill_parent"??
  4. ????android:layout_height="fill_parent"??
  5. ????android:orientation="vertical"?>??
  6. ??
  7. ????<Button???
  8. ????????android:layout_width="wrap_content"??
  9. ????????android:layout_height="wrap_content"??
  10. ????????android:text="启动service"??
  11. ????????android:id="@+id/btnStartService"/>??
  12. ????<Button???
  13. ????????android:layout_width="wrap_content"??
  14. ????????android:layout_height="wrap_content"??
  15. ????????android:text="停止service"??
  16. ????????android:id="@+id/btnStopService"/>??
  17. ??
  18. </LinearLayout>??


点击启动service,日志输出信息:

?

点击停止service,日志输出信息:


?

  相关解决方案