ImageVerifierCode 换一换
格式:DOCX , 页数:16 ,大小:231.78KB ,
资源ID:30463435      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/30463435.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(Android Service生命周期及用法.docx)为本站会员(b****8)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

Android Service生命周期及用法.docx

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