Android之使用bindService启动服务
来源:互联网 发布:php microservices 编辑:程序博客网 时间:2024/06/14 07:57
一般情况下我们使用startService(Intent service)来启动一个服务,但这种情况下无法得到Service对象的引用,通过bindService方法启动服务则可以实现此功能。下面给一个小例子演示一下:
1.调用者
1.package com.zhf.local;
2.
3.
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.
12./** 13. * 此例的目的就是拿到MyService的引用,从而可以引用其内部的方法和变量
14. * @author Administrator
15. *
16. */
17.public class LocalServiceActivity extends Activity { 18. /** Called when the activity is first created. */ 19. private MyService myService; 20.
21. @Override 22. public void onCreate(Bundle savedInstanceState) { 23. super.onCreate(savedInstanceState); 24. setContentView(R.layout.main);
25.
26. Intent intent = new Intent(this, MyService.class); 27. bindService(intent, connection, Context.BIND_AUTO_CREATE);
28. }
29.
30. private ServiceConnection connection = new ServiceConnection() { 31.
32. @Override 33. public void onServiceDisconnected(ComponentName name) { 34. myService = null; 35. }
36.
37. @Override 38. public void onServiceConnected(ComponentName name, IBinder service) { 39. myService = ((MyService.MyBinder) service).getService();
40. System.out.println("Service连接成功"); 41. //执行Service内部自己的方法 42. myService.excute();
43. }
44. };
45.
46. protected void onDestroy() { 47. super.onDestroy(); 48. unbindService(connection);
49. };
50.}
2.服务者
1.package com.zhf.local;
2.
3.import Android.app.Service; 4.import Android.content.Intent; 5.import Android.os.Binder; 6.import Android.os.IBinder; 7.
8.public class MyService extends Service { 9. private final IBinder binder=new MyBinder(); 10. @Override 11. public IBinder onBind(Intent intent) { 12. return binder; 13. }
14.
15. public class MyBinder extends Binder{ 16. MyService getService(){
17. return MyService.this; 18. }
19. }
20.
21. public void excute(){ 22. System.out.println("通过Binder得到Service的引用来调用Service内部的方法"); 23. }
24.
25. @Override 26. public void onDestroy() { 27. //当调用者退出(即使没有调用unbindService)或者主动停止服务时会调用 28. super.onDestroy(); 29. }
30.
31. @Override 32. public boolean onUnbind(Intent intent) { 33. //当调用者退出(即使没有调用unbindService)或者主动停止服务时会调用 34. System.out.println("调用者退出了"); 35. return super.onUnbind(intent); 36. }
37.}
本篇文章来源于 Linux公社网站(www.linuxidc.com) 原文链接:http://www.linuxidc.com/Linux/2011-10/46074.htm
- Android之使用bindService启动服务
- 服务启动方式三-使用bindService方式启动服务
- Android服务之bindService源码分析
- Android四大组件Service之bindService启动
- Android启动Service的两种方式之bindService()
- android中使用bindservice无法启动service解决方法
- Android Service 同时使用bindservice和startservice启动的问题
- Android 服务(二)BindService
- Android Service之bindService
- Android基础知识之BindService
- Android为什么使用bindService
- 使用bindService方式启动Service
- Android 服务之startService和bindService以及前台服务的创建
- android 之 Service(Context BindService())
- android中bindService的使用
- Android中Service使用bindService
- android bindService 来调用服务中的方法
- android开发步步为营之36:四大组件之Service(中)通过bindService启动
- 新浪笔试题
- Android之Service与IntentService的比较
- CSS Sprites 图片整合技术
- 过滤器浅谈
- java与c/c++进行socket通信(1)
- Android之使用bindService启动服务
- 如何使用struts2结合MySQL实现分页功能?
- 真相永难揭晓
- Android控件Gallery 3D效果
- 设计模式-桥接模式(Bridge Pattern)
- 老师点名系统设计
- Android 图片水平显示 类Gallery效果
- KALMAN滤波器入门总结
- php_mbstring