Service的理解
Service的理解
Service是什么?
Service是一种可以在后台运行相关任务的组件。没有界面。其存在的线程是主线程,一般会通过启动子线程来执行耗时操作。
Service有什么用?
可用于在后台执行下载任务。从而不影响用户界面操作。
Service分启动型服务、绑定型服务两种。
绑定服务中的创建Binder的扩展类的目的是什么?
该类主要用于获取Service的实例,以及执行相关的方法。
额外知识
1.不绑定服务,直接点击取消绑定服务会报错。因此需要通过判断Binder对象是否为空来处理。
2.服务可以不定义Action,通过Intent指定要跳转的Service即可。
3.Service的有效范围是所有满足条件的Activity。
4.在Service里执行耗时操作会导致ANR,启动新的线程执行耗时操作可有效避免。
两种服务的区别
Service类型 | 说明 | 使用步骤 |
---|---|---|
启动型服务 | 1.生命周期为onCreate->onStartCommand->onDestroy 2.服务一旦启动后,需要调用stopService方法或stopSelf方法才能停止。 3.可常驻系统。 |
1.通过调用startService启动服务 2.通过stopService停止服务 |
绑定型服务 | 1.生命周期为onCreate->onBind->onUnbind->onDestory 2.需要返回Binder对象,即定义Binder的子类。 3.通过Binder子类的方法来获取Service对象,用于与Activity交互。 4.在Activity中需要通过创建ServiceConnection对象来获取service实例。从而调用service里的方法。 |
1.通过bindService绑定服务 2.通过unbindService取消绑定服务 |
Service生命周期
Service的类型及其示例
启动型服务 - StartService
使用
1.创建java类继承Service
2.重写onStart、onCreate等方法。
3.在onCreate中新开子线程,用于执行任务。
示例 - 启动型服务
Service作用
打印0-2000的数字,完毕后停止服务
1 |
public class CalcService extends Service { |
1 |
// Activity中的代码 |
绑定型服务 - BindService
BindService(Intent,ServiceConnection,Context.BIND_AUTO_CREATE)
参数分别为Intent,ServiceConnection,标识号。通常为BIND_AUTO_CREATE。
使用
1.创建java类继承Service
2.创建Java类继承Binder类。用于返回Service实例。
3.重写onBind、onUnBind等方法。
4.在Activity中创建ServiceConnection对象,并获取Binder实例。
ps:为防止未绑定服务,用户点击取消绑定而导致的崩溃。需要对unbindService进行边缘性处理。
示例 - 普通绑定型服务
1 |
public class BindService extends Service { |
Activity中的代码
1 |
private ServiceConnection connection = new ServiceConnection() { |
示例 - 绑定型远程服务
理解
如何定义远程服务?
在配置文件中添加代码android:progress=”:remote”即可
为什么设置了Service为远程Service就不会导致ANR了?
远程服务是指将服务放到另一个进程中执行,所以就不会导致程序的ANR现象。
如何跨进程与Activity进行通信? 见示例
如何实现跨程序通信?
示例 - 实现跨进程与Activity交互
步骤
1.创建aidl文件,定义接口(TestInterface)及相关方法。点击make project,自动生成Stub相关的接口。
2.创建Service类,并在ManiFest文件设置远程属性。android:process=”:remote”
3.在Service中创建接口TestInterface.Stub 对象,重写接口方法。
4.在Activity中通过AIDL接口的asInterface方法来获取AIDL对象。
5.利用接口对象调用相关方法。
1 |
// 创建aidl文件,定义接口。 |
1 |
// 配置服务远程属性 |
1 |
// Service中创建远程接口的Stub对象 |
1 |
// Activity中调用远程接口的方法 |
示例 - 实现跨程序与Activity交互
步骤
实现步骤跟上面类似。只是需要为Service定义Action,以及将AIDL接口及其包名复制到另一程序中。即可实现跨程序交互。
IntentService
理解
为什么有这个类?
该类是Service的子类,主要是简化了异步操作繁琐复杂的代码。
有什么特点?
1.不需要自己创建子线程。
2.不需要考虑何时关闭服务。
使用情景是什么?
多次调用startService。
使用
1.Service继承IntentService。
2.以类名为参数调用父类构造方法。
3.重写onHandleIntent核心方法以及其他方法。
前台服务
什么是前台服务?
普通服务在内存不足的情况下会被系统杀掉,而前台服务不会因为内存不足而被杀掉。
前台服务有什么特点?
前台服务有通知栏提示,可设定相关的标题、提示灯。例如天气、播放器等。
前台服务的使用
1.构建Notification对象。
2.通过startForeground方法启动通知栏。
3.通过stopForeground方法关闭通知栏。
注意
Notification的构建中,若不调用setSmallIcon方法设置Icon,其他相关设置(标题、提示)均无效。
1 |
@Override |
Service的理解的更多相关文章
- 【Java】对Web Service的理解
WSDL(Web Service Description Language)是描述Web Service的语言. 你会怎样向别人介绍你的Web service有什么功能,以及每个函数调用时的参数呢?你 ...
- Java Web Action DAO Service层次理解
参考来源:http://blog.csdn.net/inter_peng/article/details/41021727 1. Action/Service/DAO简介: Action是管理业务(S ...
- Springboot的entity,dao,controller,service层级理解
1.Dao层:持久层,主要与数据库交互 DAO层首先会创建Dao接口,接着就可以在配置文件中定义该接口的实现类:接着就可以在模块中调用Dao的接口进行数据业务的处理,而不用关注此接口的具体实现类是哪一 ...
- web service的理解
http://blog.csdn.net/u011075267/article/details/41522643
- Android中Service(服务)详解
http://blog.csdn.net/ryantang03/article/details/7770939 Android中Service(服务)详解 标签: serviceandroidappl ...
- Android组件系列----Android Service组件深入解析
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...
- Android应用开发基础篇(6)-----Service
链接地址:http://www.cnblogs.com/lknlfy/archive/2012/02/20/2360336.html 一.概述 我们知道,Service是Android的四大组件之一. ...
- service and intentservice
Service是Android中四大组件之一,在Android开发中起到非常重要的作用,先来看一下官方对Service的定义: A Service is an application componen ...
- 关于Service中bindService注意的几个问题
最近有用到Activity需要不断的从Service中获取数据,第一个想法肯定就是通过bind回调机制了,有几点概念模糊特此记录下: 单独使用bindService(),unbindService() ...
随机推荐
- python3 简单爬虫
爬取虎牙直播分类页面的主播的头像,名字,人气 今天学习了python3爬虫,上课闲着无聊,自己写了一个爬虫 就顺着老师思路 爬了虎牙直播分类页面的主播,头像,名字,和人气 HuYaCateScrapy ...
- 剩余参数(rest arguments) Mixin
Mixin – Pug 中文文档 https://pug.bootcss.com/language/mixins.html 混入 Mixin 混入是一种允许您在 Pug 中重复使用一整个代码块的方法. ...
- 关于this,作用域,属性,原型链的一个小练习
function p () { this.name = 'x'; var name = 'y'; this.getName = function () { return name; } } // 求值 ...
- ES6 Promise对象then方法链式调用
then()方法的作用是Promise实例添加解决(fulfillment)和拒绝(rejection)状态的回调函数.then()方法会返回一个新的Promise实例,所以then()方法后面可以继 ...
- IO 流之字节流和转换流
基本读取操作: InputStream(); OutputStream(); // 直接写入目的地中, 不需要 flush() 刷新 write(byte[] b); // 参数为 byte 数组 字 ...
- java URL 利用网址api 查出手机号归属地
手机号码归属地查询api接口 1.淘宝网API地址: http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=手机号码参数:tel:手机号码返 ...
- 访问HDFS报错:org.apache.hadoop.security.AccessControlException: Permission denied
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apac ...
- CWM是什么?
CWM [1] (CommonWarehouseMetamodel公共仓库元模型)是OMG组织在数据仓库系统中定义了一套完整的元模型体系结构,用于数据仓库构建和应用的元数据建模.公共仓库元模型指定的 ...
- Java-idea-创建maven项目,部署项目,部署服务器,简单测试
spring-boot项目创建 1.项目创建 使用Idea,File→Project→Spring initalizr,点击next,进行基本配置.此时 一个spring boot项目创建完成. 之后 ...
- android studio本地gradle
1.从网站上下载http://services.gradle.org/distributions/ 2.打开工程里的gradle-wrapper.properties, distributionUrl ...