2022年Android之Service学习总结 .pdf

上传人:C****o 文档编号:32488411 上传时间:2022-08-09 格式:PDF 页数:9 大小:380.09KB
返回 下载 相关 举报
2022年Android之Service学习总结 .pdf_第1页
第1页 / 共9页
2022年Android之Service学习总结 .pdf_第2页
第2页 / 共9页
点击查看更多>>
资源描述

《2022年Android之Service学习总结 .pdf》由会员分享,可在线阅读,更多相关《2022年Android之Service学习总结 .pdf(9页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、目录第十五课: Service学习 (1) . 11.1 什么是 Service 服务(例如,音乐播放器;下载链接 . 11.2 Service和 Thread(什么时候使用线程,什么时候使用服务 . 11.3 调用 Service的方式 startService(); bindService() . 21.4 通过 startService()调用 Service. 21.4.1 编写类继承Service或其子类 . 21.4.2 复写方法onStartCommand() onBind() onCreate() onDestroy() . 51.4.3 在 manifest 文件中声服务 .

2、 51.4.4 启动服务StartService . 51.4.5 关闭服务 StopService . 51.5 MainActivity.java . 51.6 ExampleService.java . 71.7 Main.xml . 81.8 AndroidManifest.java . 8Android之 Service学习总结我们的 windows 操作系统也是有服务的。开始,运行:services.msc 1.1 什么是 Service 服务(例如,音乐播放器;下载链接A Service is an application component 应用程序组件that can per

3、form long-running 耗时的operations in the background后台and does not provide a user interface 界面 . 例子: 音乐播放器我们播放音乐的同时浏览网页如果使用Activity ,我们知道Activity 如果有活动在它前面,它就会执行onStop()方法,做其它事情会被终止掉1.2 Service和 Thread (什么时候使用线程,什么时候使用服务Dev GuideServices Should you use a service or a thread? A service is simply a compo

4、nent that can run in the background even when the user is not interacting with your application. Thus, you should create a service only if that is what you need. 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 9 页 - - - - - - - - - 执行service ,长的耗时操作,并且不需要和用户进行交互

5、。但是如果需要和用户进行交互时,我们要创建的是线程thread ,而不是service ,比如:我们需要在Activity 中播放一段音乐,此时要使用线程。If you need to perform work outside your main thread , but onlywhile the user is interacting with your application, then you should probably instead create a new thread and not a service. For example, if you want to play s

6、ome music, but only while your activity is running, you might create a thread inonCreate(), start running it in onStart(), then stop it in onStop(). Also consider using AsyncTask orHandlerThread, instead of the traditional Threadclass. See the Processes and Threading document for more information ab

7、out threads. Remember that if you do use a service, it still runs in your applications main thread by default, so you should still create a new thread within the service if it performs intensive or blocking operations. 1.3 调用 Service 的方式 startService();bindService()StartedstartService():调用者( Activit

8、y )和服务之间没有联系,即使调用者退出了,服务仍然进 行 : 点 击btnStartService按 钮 , 执 行onCreate()-onStart()-startService()- 点 击btnStopService onDestory()Bound绑定bindService(): 调 用 者 和 服 务 绑 在 一 起 , 调 用 者 一 旦 退 出 服 务 也 就 终 止onCreate()-onBind()-onUnbind()-onDestory() 1.4 通过 startService()调用 Service 1.4.1 编写类继承 Service或其子类ExampleS

9、ervice.java ,视频中提供了一种方法创建类快捷方式:右击,选, class删除该方法名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 9 页 - - - - - - - - - ExampleService.java 创建类快捷方式:右击,选, classpackage com.szy.service; import android.app.Service; import android.content.Intent; import android.os.IBin

10、der; import android.util.Log; public class ExampleService extends Service private static final String TAG=ExampleService; Override public void onCreate() Log.i(TAG, ExampleService-onCreate); super.onCreate(); System.out.println(oncreate); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心

11、整理 - - - - - - - 第 3 页,共 9 页 - - - - - - - - - Override public void onStart(Intent intent, int startId) / TODO Auto-generated method stub super.onStart(intent, startId); System.out.println(onstart); Override public void onDestroy() Log.i(TAG, ExampleService-onDestroy); System.out.println(ondestroy);

12、 super.onDestroy(); Override public int onStartCommand(Intent intent, int flags, int startId) Log.i(TAG, ExampleService-onStartCommand); System.out.println(onstartcommand); return START_NOT_STICKY; 详解 onStartCommand 方法:三个参数Referenceandroid.appServiceonStartCommand(Intent intent, int flags, int start

13、Id) Called by the system every time a client explicitly starts the service by calling startService(Intent), providing the arguments it supplied and a unique integer token representing the start request.Intent :通过调用组件,传递进来的参数Flags:startId :有了这里的Id,是线程唯一的标识符,相当于身份证号,通过这个id 可以找到我们的服务,我们可以用它来结束程序,而不需要方法

14、onstop() 该方法返回值为int 类型,只能有三种类型:三个常量START_STICKY:当服务进行在运行时被杀死,系统将会把它值为started 状态,但是并不保存其传递的 Intent 对象START_NOT_STICKY:当服务进行在运行时被杀死,并且没有新的Intent 对象传递过来,统将会把它值为 started 状态,但是并不会再次创建进程,直到startService(Intent) 方法被调用。START_REDELIVER_INTENT:当服务进行在运行时被杀死,它将会间隔一段时间后重新被创建,并且最后一个传递的Intent 对象将会再次传递过来。名师资料总结 - -

15、-精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 9 页 - - - - - - - - - Override public IBinder onBind(Intent intent) System.out.println(onbind); return null; 下面我们创造布局文件,创建两个Button,一个开启服务,一个关闭服务1.4.2 复写方法 onStartCommand() onBind() onCreate() onDestroy()onStartCommand() onBin

16、d() onCreate() onDestroy()1.4.3 在 manifest文件中声服务 1.4.4 启动服务 StartService 1.4.5 关闭服务 StopService 1.5 MainActivity.java package com.szy.service; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - -

17、- - - - - - 名师精心整理 - - - - - - - 第 5 页,共 9 页 - - - - - - - - - import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity private Button btnStartService; private Button btnStopService; Override public void onCreate(Bundle savedInstanceState) su

18、per.onCreate(savedInstanceState); setContentView(R.layout.main); btnStartService = (Button) findViewById(R.id.btnStartService); btnStopService = (Button) findViewById(R.id.btnStopService); btnStartService.setOnClickListener(listener); btnStopService.setOnClickListener(listener); private OnClickListe

19、ner listener=new OnClickListener() Override public void onClick(View v) Intent intent= new Intent(MainActivity.this, ExampleService.class); switch (v.getId() case R.id.btnStartService: startService(intent); / 创建服务break; case R.id.btnStopService: stopService(intent); / 终止服务break;default: break; ; 名师资

20、料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 6 页,共 9 页 - - - - - - - - - 1.6 ExampleService.java 创建类快捷方式:右击,选, classpackage com.szy.service; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.util.Log; public class Exa

21、mpleService extends Service private static final String TAG=ExampleService; Override public void onCreate() Log.i(TAG, ExampleService-onCreate); super.onCreate(); System.out.println(oncreate); Override public void onStart(Intent intent, int startId) / TODO Auto-generated method stub super.onStart(in

22、tent, startId); System.out.println(onstart); Override public void onDestroy() Log.i(TAG, ExampleService-onDestroy); System.out.println(ondestroy); super.onDestroy(); Override public int onStartCommand(Intent intent, int flags, int startId) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精

23、心整理 - - - - - - - 第 7 页,共 9 页 - - - - - - - - - Log.i(TAG, ExampleService-onStartCommand); System.out.println(onstartcommand); return START_NOT_STICKY; Override public IBinder onBind(Intent intent) System.out.println(onbind); return null; 1.7 Main.xml 1.8 AndroidManifest.java 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 9 页,共 9 页 - - - - - - - - -

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 高考资料

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁