位置: 编程技术 - 正文

Services 翻译第二集(services的翻译)

编辑:rootadmin

推荐整理分享Services 翻译第二集(services的翻译),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:service worker翻译,第二部分翻译,services英语翻译,service worker翻译,service worker翻译,services的翻译,services的翻译,services的翻译,内容如对您有帮助,希望把文章链接给更多的朋友!

原文地址: Basics基础Should you use a service or a thread?

A service is simply a component 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.

If you need to perform work outside your main thread, but only while 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 some music, but only while your activity is running, you might create a thread inonCreate(), start running it inonStart(), then stop it in onStop(). Also consider using AsyncTask orHandlerThread, instead of the traditional Thread class. See theProcesses and Threading document for more information about threads.

Remember that if you do use a service, it still runs in your application's main thread by default, so you should still create a new thread within the service if it performs intensive or blocking operations.

To create a service, you must create a subclass of Service (or one of its existing subclasses). In your implementation, you need to override some callback methods that handle key aspects of the service lifecycle and provide a mechanism for components to bind to the service, if appropriate. The most important callback methods you should override are:

创建一个service,你必须创建一个Service的子类(或者创建一个已经存在的子类的子类)。在你的实现里,你需要重写一些回调方法,这些方法处理着service生命周期的关键环节,也提供一个机制方便组件绑定service。非常重要的一些回调方法你需要重写的是一下几个:

onStartCommand()The system calls this method when another component, such as an activity, requests that the service be started, by callingstartService(). Once this method executes, the service is started and can run in the background indefinitely. If you implement this, it is your responsibility to stop the service when its work is done, by calling stopSelf() or stopService(). (If you only want to provide binding, you don't need to implement this method.)系统会调用这个方法,当另一个组件例如activity通过调用startService()请求这个service被启动。一旦这个方法执行了,这个service就会被启动并且在后台永久的运行。如果你实现这个方法,你需要在你的工作完成后停止这个service,通过调用stopSelf() 或 stopService()。(如果你仅仅想去提供一个被绑定的机制,你不需要实现这个方法。)onBind()The system calls this method when another component wants to bind with the service (such as to perform RPC), by callingbindService(). In your implementation of this method, you must provide an interface that clients use to communicate with the service, by returning an IBinder. You must always implement this method, but if you don't want to allow binding, then you should return null.系统会调用这个方法,当另一个组件通过调用bindService()想去绑定这个service的时候(例如执行RPC)。在这个方法的实现中,你必须提供一个接口通过返回一个IBinder用于客户端与service进行通信。你必须总是实现这个方法,但是你如果不想允许绑定,你应该返回null。onCreate()The system calls this method when the service is first created, to perform one-time setup procedures (before it calls eitheronStartCommand() or onBind()). If the service is already running, this method is not called.系统会调用这个方法,当这个service第一次被创建,这个方法执行一次(在它被执行前调用onStartCommand() 或 onBind())。如果这个service已经运行,这个方法不会被调用。onDestroy()The system calls this method when the service is no longer used and is being destroyed. Your service should implement this to clean up any resources such as threads, registered listeners, receivers, etc. This is the last call the service receives.系统会调用这个方法当这个service不再使用和被摧毁的时候。你的service应当实现这个方法去清除所有的不需要的资源,例如线程,监听器,广播接收器等等。这个方法是service接收的最后一个调用。

If a component starts the service by calling startService() (which results in a call to onStartCommand()), then the service remains running until it stops itself with stopSelf() or another component stops it by callingstopService().

如果一个组件调用 startService() 来启动这个service,那么系统会回调onStartCommand()方法,然后这个service保持运行直到它调用stopSelf() 停止自己,或者另一个组件调用stopService()来停止这个service。

If a component calls bindService() to create the service (and onStartCommand() is not called), then the service runs only as long as the component is bound to it. Once the service is unbound from all clients, the system destroys it.

如果一个组件调用 bindService()创建一个service(此时onStartCommand() 不会被调用),然后这个service生命周期和绑定它的组件的生命周期一样长。一旦这个service从所有的客户端解绑,系统就会销毁这个service。

The Android system will force-stop a service only when memory is low and it must recover system resources for the activity that has user focus. If the service is bound to an activity that has user focus, then it's less likely to be killed, and if the service is declared to run in the foreground (discussed later), then it will almost never be killed. Otherwise, if the service was started and is long-running, then the system will lower its position in the list of background tasks over time and the service will become highly susceptible to killing—if your service is started, then you must design it to gracefully handle restarts by the system. If the system kills your service, it restarts it as soon as resources become available again (though this also depends on the value you return fromonStartCommand(), as discussed later). For more information about when the system might destroy a service, see the Processes and Threading document.

Android系统仅仅只会在内存不足的时候强制停止一个service,并且回收资源用于用户聚焦的activity。如果这个service被绑定到了一个用户聚焦的activity,那么这个service最不可能被kill掉,如果这个service被声明运行在后台(稍后讨论),那么这个service几乎不会被kill。此外,如果这个service被启动并且一直长时间运行,那么系统将会降低它在超时后台任务列表中的位置,这个service将有很高的风险被kill掉--如果你的service被启动,你必须周全的设计service以便能够让系统重启。如果系统kill你的service,系统会快速重启这个service和资源变得可用一样快(尽管这样需要依赖于你从onStartCommand()返回的&#;,稍后讨论)。更多的信息关于系统可能销毁一个service,请看Processes and Threading文档。

Services 翻译第二集(services的翻译)

In the following sections, you'll see how you can create each type of service and how to use it from other application components.

下面部分,你将看到怎么创建每种类型的service和怎么从其它应用的组件里使用它。

Declaring a service in the manifest在manifest文件中声明一个service

Like activities (and other components), you must declare all services in your application's manifest file.

像activities(和其它的组件),你必须在你的应用的manifest文件中声明所有的services。

To declare your service, add a <service> element as a child of the <application> element. For example:

声明你的service,添加<service> 标签元素作为<application> 标签元素的子标签。例如:

See the <service> element reference for more information about declaring your service in the manifest.

更多信息关于在manifest文件里声明你的service,请看<service> 标签元素参考文档。

There are other attributes you can include in the <service> element to define properties such as permissions required to start the service and the process in which the service should run. The android:name attribute is the only required attribute—it specifies the class name of the service. Once you publish your application, you should not change this name, because if you do, you risk breaking code due to dependence on explicit intents to start or bind the service (read the blog post, Things That Cannot Change).

还有其它的属性你能够包含在<service> 标签元素里,例如需要启动这个service的权限和这个service应该运行的进程。android:name 属性是唯一要求的属性--它指定了service的类名。一旦你发布你的应用,你不应该改变这个名字,因为如果你这样做,有打断代码的风险由于依赖显示intents去启动或绑定这个service(阅读博客, Things That Cannot Change)。

To ensure your app is secure, always use an explicit intent when starting or binding your Service and do not declare intent filters for the service. If it's critical that you allow for some amount of ambiguity as to which service starts, you can supply intent filters for your services and exclude the component name from the Intent, but you then must set the package for the intent with setPackage(), which provides sufficient disambiguation for the target service.

确保你的应用是安全的,总是使用显示intent当启动或绑定你的Service ,并且不要为你的service声明intent filters。如果你允许隐式启动service是至关重要的,你可以为你的services运用intent filters,并且从Intent里排除组件名,但是你必须使用setPackage()为你的intent设置包名,这样能够为你想启动的service消除歧义。

Additionally, you can ensure that your service is available to only your app by including the android:exportedattribute and setting it to "false". This effectively stops other apps from starting your service, even when using an explicit intent.

另外,你能确保你的service只对你自己的应用可用,通过设置android:exported属性并设置为"false"。这样能有效的阻止其它应用启动你的service,甚至当使用一个显示intent的时候。

Android 获取手机屏幕的宽度和高度 WindowManagerwm=(WindowManager)getContext().getSystemService(Context.WINDOW_SERVICE);intwidth=wm.getDefaultDisplay().getWidth();intheight=wm.getDefaultDisplay().getHeight();2、WindowManagerwm=this.ge

ScrollView用法(一) 理论部分1、ScrollView和HorizontalScrollView是为控件或者布局添加滚动条2、上述两个控件只能有一个孩子,但是它并不是传统意义上的容器3、上述两个控件

计算两个GPS坐标的距离 方法四 - Java语言 Java计算两个GPS坐标点之间的距离1.Lat1Lung1表示A点经纬度,Lat2Lung2表示B点经纬度;2.a=Lat1–Lat2为两点纬度之差b=Lung1-Lung2为两点经度之差;3..为地球

标签: services的翻译

本文链接地址:https://www.jiuchutong.com/biancheng/386102.html 转载请保留说明!

上一篇:[置顶] Android GradientDrawable高级应用 以后完全用不上美工了([置顶]bilinovel)

下一篇:Python 中的 else详解(pythonfor else)

  • 房产税城镇土地使用税税率
  • 税务师考试报名
  • 购买厂房需要交土地使用税吗
  • 小微企业开专票税率是1%还是3%
  • 进口关税增值税税率
  • 资产季末值
  • 公司注销实收资本怎么处理
  • 发生成本支出发票未到
  • 管理费用月末怎么结账
  • 公司向个人借款利率最高多少
  • 税控技术服务费计入什么科目
  • 小规模纳税人月超10万季度不超30万
  • 文化事业建设税怎么申报
  • 增值税专用发票几个点
  • 白酒消费税税率多少钱一吨
  • 商业承兑汇票适用于
  • 出口信用证议付支出是什么
  • 个税如何绑定新公司
  • 零余额账户用款额度年末转入什么科目
  • 生产的废材料处理如何记账是否缴税?
  • 农产品免税产品可以开增值税专用发票吗
  • 税控专用设备全额抵扣
  • 苹果14如何强制关机重启
  • 财务报表包括哪几个表
  • linux中添加用户和组的操作
  • 笔记本电池的正负极区分
  • 出租设备的租金收入应计入什么科目
  • 处置动产减按2%税率是什么意思
  • 建筑工程甲级什么意思
  • 以前年度损益调整借贷方向
  • 其他业务支出的二级科目有哪些
  • php中正则表达式
  • 下岗再就业有什么优惠政策
  • css实现轮播图侧边阴影效果
  • 解决城市内涝的题目
  • php快速排序原理
  • 常用的成本计算方法有哪些
  • 印花税不足一元需要缴纳吗
  • javascript获取数组索引
  • 【Pytorch深度学习50篇】·······第六篇:【常见损失函数篇】-----BCELoss及其变种
  • 不仅仅是喜欢原唱
  • jquery 兄弟选择器
  • Yii基于CActiveForm的Ajax数据验证用法示例
  • 企业利息支出怎么算
  • chkdsk.exe/f命令
  • 行政单位其他应付款
  • css选择器怎么用
  • 新法典离职
  • 小规模纳税人无票收入怎么申报
  • 对公账户的种类有几种
  • arraylist源码分析1.8
  • 建筑业用的会计账簿
  • 铝合金门窗行业利润率
  • 租厂房需要去缴房产税吗
  • 火车票丢了还能再取吗
  • 土地出让金抵减增值税申报怎么填
  • 缴纳社保公积金的会计分录
  • 收取招标代理费的规定
  • 实收资本在利润里怎么算
  • 怎么开具
  • 建筑公司项目
  • 右键菜单管理 win10
  • win10动态磁贴不更新
  • slpv24s.exe - slpv24s是什么进程
  • ubuntu netbook
  • nvvsvc.exe是什么进程
  • linux开启关闭命令
  • win7连接投屏器
  • [置顶]bilinovel
  • 基于javascript数独游戏论文参考文献
  • 删除了c盘安全组或用户
  • 点击按钮显示
  • 蓝色的css代码
  • angularjs2
  • python给批量图片添加文字
  • 安卓手机管家哪个好用
  • Android ImageLoader 本地缓存
  • 税控盘口令密码怎么修改
  • 国税和地税在一起吗
  • 地税可以跨区交吗
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设