安卓组件之---Service(文档导读)

LocalServiceComponents.exe 立即下载

从类的角度来看,Service是抽象类,public abstract class ,这意味着在继承开发Service子类时,需要实现其抽闲方法abstract method。往文档下面扫了一眼,需要重写的abstract mthod只有一个,是:public abstract IBinder onBind( Intent intent ).暂时不深究啥意思….

回到类的角度,Service直接extends 了ContextWraper类,而ContextWrapper又继承自Context。也就是说,Context类中的方法,Service都可以自由的调用,比如说getResources( )什么的。

那Service有哪些子类呢?看文档有一大堆子类,挑一个出来看:IntentService。IntentService is a base class for Services that handle asynchronous requests (expressed as Intents) on demand. 显然,IntentService可以用来处理异步请求。
对于Service的子类,不做过多介绍,有必要再深究。

public abstract class
Service
extends ContextWrapper
implements ComponentCallbacks2
java.lang.Object
↳ android.content.Context
↳ android.content.ContextWrapper
↳ android.app.Service

粗糙的看了一下Service的父类,子类。积累了一点感性认识,下面开始顺着文档翻译。

Class Overview

A Service is an application component representing either an application’s desire to perform a longer-running operation while not interacting with the user or to supply functionality for other applications to use. Each service class must have a corresponding declaration in its package’s AndroidManifest.xml. Services can be started with Context.startService() and Context.bindService(). Service是一个应用组件,一般用于两件事件,一是用以执行一个无须与用户交互的耗时任务,二是为其他应用提供功能(functionality)。每一个service类,必须要在AndroidManifest.xml文件中声明。Service可以被用两种方式启动,用Context.startService( ) ,或者用Context.bindService( )来启动service.

Note that services, like other application objects, run in the main thread of their hosting process. This means that, if your service is going to do any CPU intensive (such as MP3 playback) or blocking (such as networking) operations, it should spawn its own thread in which to do that work. More information on this can be found in Processes and Threads. The IntentService class is available as a standard implementation of Service that has its own thread where it schedules its work to be done. 这一段主要是讲service这个组件运行在何种进程当中的。并且给建议说,如果你的service要提供的服务是CPU密集型任务,比如说播放MP3或者堵塞操作(连接网络),那么你应该分化出一个自己的线程,并将工作在这个线程线程中完成。

Topics covered here: 文档覆盖的话题有:

 1. What is a Service?啥子是Service
 2. Service Lifecycle Service的生命周期
 3. Permissions      许可制---就是要在AndroidManifest.xml去声明该Service
 4. Process Lifecycle
 5.  Local Service Sample  一个栗子

Developer Guides开发者指南….自己去看洛…..

For a detailed discussion about how to create services, 
read the Services developer guide.

What is a Service? 啥子是Service??

Most confusion about the Service class actually revolves around what it is not:
关于Service的大部分困惑,其实并不是围绕着Service是什么,而是关于Service不是什么:

A Service is not a separate process. The Service object itself does
not imply it is running in its own process; unless otherwise
specified, it runs in the same process as the application it is part
of.一个Service不是一个独立的进程。Service组件本身并不意味着它能运行于自己的进程。除非其他组件明确规定,Service与其他组件运行在同一个进程当中,好像Service是应用的一部分。
A Service is not a thread. It is not a means itself to do work off of the main thread (to avoid Application Not Responding errors).
Service不是线程。Service不能脱离主线程工作(避免程序出现该种类型错误:Not Responding errors)。

Thus a Service itself is actually very simple, providing two main
features:由此一个Service就很简单了,它主要具有两个特征:

A facility for the application to tell the system about something it wants to be doing in the background (even when the user is not

directly interacting with the application). This corresponds to calls
to Context.startService(), which ask the system to schedule work
for the service, to be run until the service or someone else
explicitly stop
it.Service可以作为一个在后台运行的组件,无须交互。当Service用于这种用途时,用Context.startService()来启动。
A facility for an application to expose some of its functionality to other applications. This corresponds to calls to
Context.bindService(), which allows a long-standing connection to be made to the service in order to interact with it.
Service作为一个组件,用来响应其他程序的请求,提供某种功能。当Service用于这种用途时,用Context.bindService()来启动。

When a Service component is actually created, for either of these
reasons, all that the system actually does is instantiate the
component and call its onCreate() and any other appropriate callbacks
on the main thread. It is up to the Service to implement these with
the appropriate behavior, such as creating a secondary thread in which
it does its work.

Note that because Service itself is so simple, you can make your
interaction with it as simple or complicated as you want: from
treating it as a local Java object that you make direct method calls
on (as illustrated by Local Service Sample), to providing a full
remoteable interface using AIDL.

**Service Lifecycle**Service的生命周期

There are two reasons that a service can be run by the system. If
someone calls Context.startService() then the system will retrieve the
service (creating it and calling its onCreate() method if needed) and
then call its onStartCommand(Intent, int, int) method with the
arguments supplied by the client. The service will at this point
continue running until Context.stopService() or stopSelf() is called.
Note that multiple calls to Context.startService() do not nest (though
they do result in multiple corresponding calls to onStartCommand()),
so no matter how many times it is started a service will be stopped
once Context.stopService() or stopSelf() is called; however, services
can use their stopSelf(int) method to ensure the service is not
stopped until started intents have been
processed.简单来说就是,如果你想启动一个Service,调用Context.startService(),
系统会调用onCreate(如果有必要),然后再调用onStartCommand(Intent, int,
int)。Service就运行起来了。。。一直运行到方法Context.stopService() 或 stopSelf()被调用。

For started services, there are two additional major modes of
operation they can decide to run in, depending on the value they
return from onStartCommand(): START_STICKY is used for services that
are explicitly started and stopped as needed, while START_NOT_STICKY
or START_REDELIVER_INTENT are used for services that should only
remain running while processing any commands sent to them. See the
linked documentation for more detail on the semantics.

Clients can also use Context.bindService() to obtain a persistent
connection to a service. This likewise creates the service if it is
not already running (calling onCreate() while doing so), but does not
call onStartCommand(). The client will receive the IBinder object that
the service returns from its onBind(Intent) method, allowing the
client to then make calls back to the service. The service will remain
running as long as the connection is established (whether or not the
client retains a reference on the service’s IBinder). Usually the
IBinder returned is for a complex interface that has been written in
aidl.
客户端也可以用方法Context.bindService()来建立一个与Service持续接连的绑定。如果Service还不存在,就需要调用onCreate(),但不像前面一种方式,这里不需要调用onStartCommand()。客户端会接受IBinder对象(IBinder对象是service从它的onBind(Intent)方法中返回的对象),IBinder对象使得客户端能够回调该service。用Context.只要绑定的连接还存在,Service就会一直运行。

A service can be both started and have connections bound to it. In
such a case, the system will keep the service running as long as
either it is started or there are one or more connections to it with
the Context.BIND_AUTO_CREATE flag. Once neither of these situations
hold, the service’s onDestroy() method is called and the service is
effectively terminated. All cleanup (stopping threads, unregistering
receivers) should be complete upon returning from onDestroy().
前面所说的两种方式,可以同时存在与一个service上。既一个service既可以被Context.startService()启动,也可以用方法Context.bindService()绑定。在这种情况,该service的运行时间该如何确定?该如何终结?自己找咯….答案就在眼前…

Local Service Sample 举一个栗子

One of the most common uses of a Service is as a secondary component
running alongside other parts of an application, in the same process
as the rest of the components. All components of an .apk run in the
same process unless explicitly stated otherwise, so this is a typical
situation.Service最常见的用法是将service作为第二个组件运行于程序当中,与其他组件一样,运行在同一个进程当中。

When used in this way, by assuming the components are in the same
process, you can greatly simplify the interaction between them:
clients of the service can simply cast the IBinder they receive from
it to a concrete class published by the
service.当service是这样的用途的时候,既所有组件运行在同一个进程当中。当service是这种用途时,你可以极大的简化service与其他组件之间的交互:service的委托人(client)可以将从service中接受的IBinder对象投射到具体的类上面去。clients
of the service can simply cast the IBinder they receive from it to a
concrete class published by the service,这一句没有太看明白…不要急,等下看看例子就好了。

An example of this use of a Service is shown here. First is the
Service itself, publishing a custom class when bound:

Service提供的方法

public abstract IBinder onBind (Intent intent)
Return the communication channel to the service. 该方法返回一个与service的通信通道,这个方法必须被重写。

public void onCreate ()
Called by the system when the service is first created. Do not call this method directly. service第一次被create时调用。。。不要直接调用该方法

public int onStartCommand (Intent intent, int flags, int startId)
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. Do not call this method directly. 每次开启service都要调用该方法

还有一些自己看,也很简单。

《疯狂的Android讲义》在讲解Service这一章时,还附带讲解了TelephonyManager、SmsManager、AudioManager、Vibrator、AlarmManage的功能与用法。我好奇的是,Serviece与这些管理类的关系,该书并没有说明。只是解释说Android系统本身提供了大量的Service组件…莫非这些就是Service组件??

LocalService 网页启动Word的示例本地服务,仅作参考 立即下载

相关推荐

均匀分布产生高斯分布

均匀分布产生高斯分布 文章目录均匀分布产生高斯分布简介方法和证明代码效果引用 简介 方法和证明 代码 效果 引用

gy_Rick的博客 5317

谷歌最新版本浏览器安装海康插件后仍不能预览摄像头视频的解决方法

谷歌浏览器版本

david_89的博客 6万+

SAP S4 HANA 新资产会计中的平行分类账估值

在资产会计中,我们可以设置使用折旧区域的平行分类账会计 我们需要根据涉及的会计原则定义折旧区域,在我们的方案中,公司 FR01 是一家国际集团的当地分支机构,公司 FR01 需要按照集团会计准则 IFRS 和当地 GAAP 会计准则报告财务报表。过账到 LG 会计原则(分类帐 2L)-->分类账 2L的 150 欧元损失 -->在 IFRS 折旧区域,我们有 100 欧元的收益,在 LG 折旧区域,我们有 150 欧元的损失,使用平行分类账 0L 和 2L,我们能够根据不同的会计原则进行平行估值。

yanzibing99的博客 1328

Context详解

Context概述 类位置:android.content.Context 类声明:public abstract class Context extends Object //直接继承java.lang.Object类 直接子类:ContextWrapper和MockContext ContextWrapper:代理执行Context,将其所有调用简单地委托给另一个Context。...

weixin_45830683的博客 758

银河麒麟V10 SP3离线安装Nginx1.21.5全记录(附11个rpm包下载清单)

本文详细记录了在银河麒麟V10 SP3操作系统上离线安装Nginx 1.21.5的全过程,包括11个必备rpm包的下载清单。通过分步骤的实战指南,帮助用户在国产化环境中高效部署Nginx服务,涵盖安装、配置、调优及故障排查等关键环节,特别适合金融、政务等对安全性要求高的场景。

weixin_30872337的博客 604

R语言的下载、安装及环境配置(Rstudio&VSCode)

R for Windows是一个免费的用于统计计算和统计制图的优秀工具,是R语言开发工具。它拥有数据存储和处理系统、数组运算工具(其向量、矩阵运算方面功能尤其强大)、完整连贯的统计分析工具、优秀的统计制图等功能。提供的图形界面,可以在其中访问控制台、创建脚本或安装其他软件包。

zegeai的博客 2万+

从源码中Activity 的定义来理解 Activity

首先我们来看下源码中源于Activity的定义: public class Activity extends ContextThemeWrapper implements LayoutInflater.Factory2, Window.Callback, KeyEvent.Callback, OnCreateContextMenuListene...

mmdev 341

T507 buildroot linux4.9之RLT8211F 1000M以太网开发调试

调试T507的gmac,解决该RTL8211F 1000m以太网中调试遇到的问题

目前从事linux驱动开发,多交流多学习吧 686

Android 官方文档导读(Introduction)

学习Android 2个月左右了,基础的学得差不多了,但是感觉知识点比较零碎,借着这个机会开始从头开始阅读一下google的官方文档,training部分的在刚开始学的时候就差不多打了一遍,这次主要是guide部分,形式主要是以官方的解释+自己的理解+测试代码+疑问的形式记录,难免存在偏颇,如有不对请各位看官也能及时指出。 一. Introduction 一些基本概念,主要介绍了两样东西:

u011734080的博客 392

SAP Build Work Zone standard edition 订阅报错 OIDC trust missing 的成因与完整解决路径

摘要:用户订阅SAP Build Work Zone时遇到422错误,提示未配置OpenID Connect信任。该错误表明业务用户需通过SAP Cloud Identity Services的OIDC协议认证,而当前子账户仅配置了SAML信任或未配置信任。解决方法是进入子账户的Trust Configuration页面,使用Establish Trust功能建立与SAP Cloud Identity Services的OIDC信任关系。这是SAP新版服务的强制要求,SAML协议已不被支持。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 197

锐捷 MPLS VPN 配置

ISP内部配置 , PE设备和CE设备之间的接口不开启 ( 只在物理接口上配置 , loopback 口不配置 )最后一步关闭 ipv4 单播 , 如果你发现路由奇奇怪怪的 , 可以尝试一下。both --> 同时配置 import export 值。绑定后接口会重置 , 且接口的路由会转到VRF路由表中。这里基本上绑定了 vrf 实例的接口都需要宣告进去。vpnv 4 通道是用来传递 mpls 路由的。先建立邻居管理,建立VPNV4通道。这里要做双向引入 , 才会有路由。export 导出值。

dekangzou的博客 1711

Android SDK Document 框架导读的翻译和注解[3]

《Android SDK Document 框架导读的翻译和注解》系列的文章将对Android应用程序的架构做一个基本的介绍,本文源于Android SDK ducument的第一个部分,描绘了Android 应用程序系统的骨架,因此,这部分对理解Android系统是最有帮助的。在翻译的时候,增加本人对Android的理解和评价。请指正。 本文将介绍Service. Service:

X-Teamer提炼商业万有引力模型, 低成本定制化开发 2260

【QNX+Android虚拟化方案】136 - QNX 侧 Coredump 文件解析

【QNX+Android虚拟化方案】136 - QNX 侧 Coredump 文件解析

|~~~热爱生活、努力学习的小伙汁~~~| 1909

Android 官方文档导读(App Fundamentals)

上一篇博客中我们和简要的介绍了一下android,我们将按照官方文档的顺序来继续讲解,这次讲解的是app。我们先来看看android对于一款app在安装进手机之后是怎么处理的:android操作系统是一个多用户的Linux操作系统,而每一个app都是一个不同的用户。 默认情况下,操作系统会给每个app一个不同的Linux 用户ID(app 并不知道这个ID)。操作系统将app下的所有文件的权限都只给

u011734080的博客 368

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

Android SDK Document 框架导读的翻译和注解[7]——Intents and Intent Filters

Android应用程序中三个主要的组件——Activity, Service, Broadcast receiver——都是由Intent中传递过来的消息激活的,Intent消息传递是这样一个基础手段,它把相同或者不同应用程序的组件后期运行绑定起来。Intent对象本身是一个被动的数据结构,它用来承载一个关于将要执行动作的抽象描述——在 broadcast的情况下,这个Intent是描述是已经发生

X-Teamer提炼商业万有引力模型, 低成本定制化开发 2126

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

安卓 LayoutInflater详解

导读 在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById()。 不同点是: LayoutInflater是用来找res/layout/下的xml布局文件,并且实例化; findViewById()是找已被载入到内存的布局对象下的具体widget控件(如Button、TextView等)。 具体作用: 1、对于一个没有被载入或者想要动态载入的界面,都需要使用LayoutInflater.inflate()来载入; 2、对于一个已经载入的界面,就可以使用A

Rey的技术博客 3643

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

牛B了,耗时6个月小米,京东,百度的Android高级工程师联名,纯手稿打造了这份2246页PDF面试学习笔记

下面的题目都是楼主在Android交流群大家在面试字节跳动时遇到的,如果大家有好的题目或者好的见解欢迎分享,楼主将长期维护此帖。:小米,京东,百度高级工程师:郭霖、鸿洋、玉刚、极客时间、腾讯课堂…:大神联合手稿整理资料,条理清晰,含图像化表示更加易懂。:包括 Handler、Activity相关、Fragment、service、布局优化、AsyncTask相关。

2401_84123584的博客 939

Android大厂面试题锦集附答案(BAT TMD JD 小米)

热文导读 | 点击标题阅读欢迎加入Java和Android架构知识星球吊炸天!74款APP完整源码!程序员遇到瓶颈时如何提升自己?来源:https://www.jianshu.com/p/cf5092fa2694这一年我兜兜转转从android到java又回到android,校招面了很多大厂,阿里、京东、小米、头条、知乎、腾讯、有赞,也收获了几个offer。感谢大家的关注,为了回馈大家,一篇最完全

Java和Android架构 2048

Android开发3年,靠着这份PDF面试资料,成功跳槽字节跳动

导读 本人末流一本毕业,在小公司待了三年,感觉没什么前途,就想跳槽去大公司,于是去年10月份辞职了,开始了大厂的面试之路,刚开始并不是很顺利,处处碰壁。感觉这样不行,于是我开始结合面试中遇到的问题进行复习,最后花了26天的时间复习,成功入职字节跳动。 今天就把我复习所整理的面试资料分享给正在求职的朋友,希望对你们提供一些帮助。 由于文章篇幅有限,我只把一些知识点列了出来,文末有详细PDF文档的免费领取方式。 这份PDF包括了Activity基础相关、Fragment基础相关、Service基础相关、Broa

m0_46962786的博客 765

亲爱的面试官,这个我可没看过!(Android部分)

http://www.jianshu.com/p/89f19d67b348#导读:Android面试中高频率出现的题都在这了。试题大部分从互联网收集,博主下了一番功夫进行梳理总结,难免有不足之处,还请见谅。这篇博客属于Androi,你够了!!!专题中的一篇,其余文章会陆续发表,第一时间会发布在本人Github上,敬请关注。这篇博客包括五个部分:热点,基础,进阶,性能优化,高级。后续会不断补充完善,希

doomvsjing的博客 439

Android系统源码分析团体项目BeesAndroid正式上线啦

嗨,BeesAndroid开源技术小组正式成立啦,Bees,即蜜蜂,取义分享、合作与奉献的意思,这也是BeesAndroid小组的宗旨,我们第一个团体项目BeesAndroid也于2018年3月6日同步上线,该项目的前 身是android-open-source-project-analysis,这个项目提供了一系列的Android系统源码分析文章,收到了良好的反馈。 但是一个人的力量是有限的,因...

weixin_34375233的博客 1461

Android 消息机制 Handler 源码解读

Android 消息机制 Handler 源码解读

OuO-1 的博客 374
上一篇: 安卓传感器开发
下一篇: 哈夫曼树
code_monky
博客等级 码龄11年 4粉丝 57原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值