Android Service系列(十一)starting Service 以及stopping a service

Starting a service

You can start a service from an activity or other application component by passing an Intent to startService() or startForegroundService(). The Android system calls the service's onStartCommand() method and passes it the Intent, which specifies which service to start.

翻译:在activity或者其他组件启动service,(通过把intent 传递给startService 或者startForgroundService 的方式)

安卓系统会调用 onStartCommand 方法然后传递intent

 

Note: If your app targets API level 26 or higher, the system imposes restrictions on using or creating background services unless the app itself is in the foreground. If an app needs to create a foreground service, the app should call startForegroundService(). That method creates a background service, but the method signals to the system that the service will promote itself to the foreground. Once the service has been created, the service must call itsstartForeground() method within five seconds.

翻译:API 26以后,系统对后台service进行了限制,除非这个app自身是在前台。如果需要create 前台service,调用startForgroundService即可,这个方法创建了一个后台service,但是这个方法告诉告诉系统他会把自己提升到前台。

service创建以后,要在五秒内调用startForground方法。

 

For example, an activity can start the example service in the previous section (HelloService) using an explicit intent with startService(), as shown here:

例如:

Intent intent = new Intent(this, HelloService.class);
startService(intent);

The startService() method returns immediately, and the Android system calls the service's onStartCommand()method. If the service isn't already running, the system first calls onCreate(), and then it calls onStartCommand().

翻译:然后会调用onStartCommand方法,如果service没有运行,则会先调用onCreate方法,然后才是onStartCommand

 

If the service doesn't also provide binding, the intent that is delivered with startService() is the only mode of communication between the application component and the service. However, if you want the service to send a result back, the client that starts the service can create a PendingIntent for a broadcast (with getBroadcast()) and deliver it to the service in the Intent that starts the service. The service can then use the broadcast to deliver a result.

翻译:如果service没有binding的话,唯一通信方式是通过startService。然而,如果你想获得result back的话,可以用PendingIntent for a broadcast 。然后 result 可以通过广播方式传递。

 

Multiple requests to start the service result in multiple corresponding calls to the service's onStartCommand(). However, only one request to stop the service (with stopSelf() or stopService()) is required to stop it.

翻译:多次调用 start service就会多次调用  onStartCommand,然而,一次stop 就可以stop 这个service了。

 

stopping a service

A started service must manage its own lifecycle. That is, the system doesn't stop or destroy the service unless it must recover system memory and the service continues to run after onStartCommand() returns. The service must stop itself by calling stopSelf(), or another component can stop it by calling stopService().

翻译:service启动后要自己管理自己的生命周期。。。balabala..

Once requested to stop with stopSelf() or stopService(), the system destroys the service as soon as possible.

翻译:stopSelf() or stopService() 只要一次就好

 

If your service handles multiple requests to onStartCommand() concurrently, you shouldn't stop the service when you're done processing a start request, as you might have received a new start request (stopping at the end of the first request would terminate the second one). To avoid this problem, you can use stopSelf(int) to ensure that your request to stop the service is always based on the most recent start request. That is, when you call stopSelf(int), you pass the ID of the start request (the startId delivered to onStartCommand()) to which your stop request corresponds. Then, if the service receives a new start request before you are able to call stopSelf(int), the ID doesn't match and the service doesn't stop.

翻译:如果同时处理多个onStartCommand,注意结束的时机。所以可以用stopSelf(int)(注意这里带参数)方法,

这样如果stopSelf(int)调用的时候还有别的request,则ta就不会stop这个service.

 

Caution: To avoid wasting system resources and consuming battery power, ensure that your application stops its services when it's done working. If necessary, other components can stop the service by calling stopService(). Even if you enable binding for the service, you must always stop the service yourself if it ever receives a call to onStartCommand().

service用完记得关,另外,bindservice如果onStartCommand也调用过一次 ,记得要手动关。。

 

For more information about the lifecycle of a service, see the section below about Managing the Lifecycle of a Service.

参考。。。

------------------------------------

这段原文好像没什么干货。。。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值