1、Android Service生命周期及用法大家好,上一节我讲解了Android Activity的生命周期,这一节我将讲解一下Service,首先我们要知道Service具体是干什么的,什么时候用到?以及它的生命周期等。Service概念及用途:Android中的服务,它与Activity不同,它是不能与用户交互的,不能自己启动的,运行在后台的程序,如果我们退出应用时,Service进程并没有结束,它仍然在后台运行,那 我们什么时候会用到Service呢?比如我们播放音乐的时候,有可能想边听音乐边干些其他事情,当我们退出播放音乐的应用,如果不用Service,我 们就听不到歌了,所以这时候就
2、得用到Service了,又比如当我们一个应用的数据是通过网络获取的,不同时间(一段时间)的数据是不同的这时候我们可以 用Service在后台定时更新,而不用每打开应用的时候在去获取。Service生命周期:Android Service的生命周期并不像Activity那么复杂,它只继承了onCreate(),onStart(),onDestroy()三个方法,当我们第一次启动Service时,先后调用了onCreate(),onStart()这两个方法,当停止Service时,则执行onDestroy()方法,这里需要注意的是,如果Service已经启动了,当我们再次启动Service时,不会
3、在执行onCreate()方法,而是直接执行onStart()方法,具体的可以看下面的实例。Service与Activity通信:Service后端的数据最终还是要呈现在前端Activity之上的,因为启动Service时,系统会重新开启一个新的进程,这就涉及到不同进程间通信的问题了(AIDL)这一节我不作过多描述,当我们想获取启动的Service实例时,我们可以用到bindService和onBindService方法,它们分别执行了Service中IBinder()和onUnbind()方法。为了让大家 更容易理解,我写了一个简单的Demo,大家可以模仿着我,一步一步的来。第一步:新建一个
4、Android工程,我这里命名为ServiceDemo.第二步:修改main.xml代码,我这里增加了四个按钮,代码如下:view plain1. 2. 7. 13. 19. 25. 31. 37. 第三步:新建一个Service,命名为MyService.java代码如下:view plain1. packagecom.tutor.servicedemo;2. importandroid.app.Service;3. importandroid.content.Intent;4. importandroid.os.Binder;5. importandroid.os.IBinder;6. i
5、mportandroid.text.format.Time;7. importandroid.util.Log;8. publicclassMyServiceextendsService9. /定义个一个Tag标签10. privatestaticfinalStringTAG=MyService;11. /这里定义吧一个Binder类,用在onBind()有方法里,这样Activity那边可以获取到12. privateMyBindermBinder=newMyBinder();13. Override14. publicIBinderonBind(Intentintent)15. Log.e
6、(TAG,startIBinder);16. returnmBinder;17. 18. Override19. publicvoidonCreate()20. Log.e(TAG,startonCreate);21. super.onCreate();22. 23. 24. Override25. publicvoidonStart(Intentintent,intstartId)26. Log.e(TAG,startonStart);27. super.onStart(intent,startId);28. 29. 30. Override31. publicvoidonDestroy()
7、32. Log.e(TAG,startonDestroy);33. super.onDestroy();34. 35. 36. 37. Override38. publicbooleanonUnbind(Intentintent)39. Log.e(TAG,startonUnbind);40. returnsuper.onUnbind(intent);41. 42. 43. /这里我写了一个获取当前时间的函数,不过没有格式化就先这么着吧44. publicStringgetSystemTime()45. 46. Timet=newTime();47. t.setToNow();48. retu
8、rnt.toString();49. 50. 51. publicclassMyBinderextendsBinder52. MyServicegetService()53. 54. returnMyService.this;55. 56. 57. 第四步:修改ServiceDemo.java,代码如下:view plain1. packagecom.tutor.servicedemo;2. importandroid.app.Activity;3. importandroid.content.ComponentName;4. importandroid.content.Context;5.
9、importandroid.content.Intent;6. importandroid.content.ServiceConnection;7. importandroid.os.Bundle;8. importandroid.os.IBinder;9. importandroid.view.View;10. importandroid.view.View.OnClickListener;11. importandroid.widget.Button;12. importandroid.widget.TextView;13. publicclassServiceDemoextendsAct
10、ivityimplementsOnClickListener14. 15. privateMyServicemMyService;16. privateTextViewmTextView;17. privateButtonstartServiceButton;18. privateButtonstopServiceButton;19. privateButtonbindServiceButton;20. privateButtonunbindServiceButton;21. privateContextmContext;22. 23. /这里需要用到ServiceConnection在Con
11、text.bindService和context.unBindService()里用到24. privateServiceConnectionmServiceConnection=newServiceConnection()25. /当我bindService时,让TextView显示MyService里getSystemTime()方法的返回值26. publicvoidonServiceConnected(ComponentNamename,IBinderservice)27. /TODOAuto-generatedmethodstub28. mMyService=(MyService.M
12、yBinder)service).getService();29. mTextView.setText(IamfromeService:+mMyService.getSystemTime();30. 31. 32. publicvoidonServiceDisconnected(ComponentNamename)33. /TODOAuto-generatedmethodstub34. 35. 36. ;37. publicvoidonCreate(BundlesavedInstanceState)38. super.onCreate(savedInstanceState);39. setCo
13、ntentView(R.layout.main);40. setupViews();41. 42. 43. publicvoidsetupViews()44. 45. mContext=ServiceDemo.this;46. mTextView=(TextView)findViewById(R.id.text);47. 48. 49. 50. startServiceButton=(Button)findViewById(R.id.startservice);51. stopServiceButton=(Button)findViewById(R.id.stopservice);52. bi
14、ndServiceButton=(Button)findViewById(R.id.bindservice);53. unbindServiceButton=(Button)findViewById(R.id.unbindservice);54. 55. startServiceButton.setOnClickListener(this);56. stopServiceButton.setOnClickListener(this);57. bindServiceButton.setOnClickListener(this);58. unbindServiceButton.setOnClick
15、Listener(this);59. 60. 61. publicvoidonClick(Viewv)62. /TODOAuto-generatedmethodstub63. if(v=startServiceButton)64. Intenti=newIntent();65. i.setClass(ServiceDemo.this,MyService.class);66. mContext.startService(i);67. elseif(v=stopServiceButton)68. Intenti=newIntent();69. i.setClass(ServiceDemo.this
16、,MyService.class);70. mContext.stopService(i);71. elseif(v=bindServiceButton)72. Intenti=newIntent();73. i.setClass(ServiceDemo.this,MyService.class);74. mContext.bindService(i,mServiceConnection,BIND_AUTO_CREATE);75. else76. mContext.unbindService(mServiceConnection);77. 78. 79. 80. 81. 82. 第五步:修改A
17、ndroidManifest.xml代码(将我们新建的MyService注册进去如下代码第14行:)view plain1. 2. 6. 7. 9. 10. 11. 12. 13. 14. 15. 16. 17. 六步:执行上述工程,效果图如下:点击startServie按钮时先后执行了Service中onCreate()-onStart()这两个方法,打开Logcat视窗效果如下图:我们这时可以按HOME键进入Settings(设置)-Applications(应用)-Running Services(正在运行的服务)看一下我们新启动了一个服务,效果如下:点击stopService按钮时,S
18、ervice则执行了onDestroy()方法,效果图如下所示:这时候我们再次点击startService按钮,然后点击bindService按钮(通常bindService都是bind已经启动的Service),我们看一下Service执行了IBinder()方法,以及TextView的值也有所变化了,如下两张图所示:最后点击unbindService按钮,则Service执行了onUnbind()方法,如下图所示:大家好,好久不见,今天在开发中遇到的一个问题给大家分享一下,我先前做了一个音乐播放器,因为播放音乐一般都是用服务来处理的。当用户点击播放按钮时,音乐在服务中播放,然后用用户退出程
19、序(服务进程还在,音乐还继续),所以妥用户点再次进入应用时,我们播放器要处于播放状态,这里我作了简单的处理,判断这个音乐服务是否开启来完成的。今天给大家的小例子是列出Android设备中所有启动的服务,及判断某个服务是否开启,具体步骤如下了:第一步:新建一个Android工程,命名为RunningService。第二步:修改RunningService.java代码如下:view plain1. packagecom.tutor.runningservice;2. importjava.util.List;3. importandroid.app.Activity;4. importandro
20、id.app.ActivityManager;5. importandroid.os.Bundle;6. importandroid.widget.TextView;7. publicclassRunningServiceextendsActivity8. /*Calledwhentheactivityisfirstcreated.*/9. Override10. publicvoidonCreate(BundlesavedInstanceState)11. super.onCreate(savedInstanceState);12. /setContentView(R.layout.main);
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1