Android Service系列(十四)运行前台service

A foreground service is a service that the user is actively aware of and isn't a candidate for the system to kill when low on memory. A foreground service must provide a notification for the status bar, which is placed under the Ongoing heading. This means that the notification cannot be dismissed unless the service is either stopped or removed from the foreground.

翻译:前台service用户可感知,系统低内存时候也不杀。前台service要有个notification

.前台service要和notification共存亡

 

Caution: Limit your app's use of foreground services.

You should only use a foreground service when your app needs to perform a task that is noticeable by the user even when they're not directly interacting with the app. For this reason, foreground services must show a status bar notification with a priority of PRIORITY_LOW or higher, which helps ensure that the user is aware of what your app is doing. If the action is of low enough importance that you want to use a minimum-priority notification, you probably shouldn't be using a service; instead, consider using a scheduled job.

翻译:前台service该用才用,前台通知的priority要大于等于 PRIORITY_LOW.  如果你不想这样,那么用 scheduled job 吧。

 

Every app that runs a service places an additional load on the system, consuming system resources. If an app tries to hide its services by using a low-priority notification, this can impair the performance of the app the user is actively interacting with. For this reason, if an app tries to run a service with a minimum-priority notification, the system calls out the app's behavior in the notification drawer's bottom section.

翻译:启动service要消耗系统资源,所以如果你用低priority的notification , 这回损害应用性能。

所以如果你不做notification提醒用户,系统帮你做!

 

For example, a music player that plays music from a service should be set to run in the foreground, because the user is explicitly aware of its operation. The notification in the status bar might indicate the current song and allow the user to launch an activity to interact with the music player. Similarly, an app to let users track their runs would need a foreground service to track the user's location.

翻译:例如,音乐播放器要用前台service.balabala    路径记录软件(跑步软件)也需要用前台service来记录用户的位置。

 

Note: Apps that target Android 9 (API level 28) or higher and use foreground services must request theFOREGROUND_SERVICE permission. This is a normal permission, so the system automatically grants it to the requesting app.

If an app that targets API level 28 or higher attempts to create a foreground service without requesting FOREGROUND_SERVICE, the system throws a SecurityException.

API 28以后前台service  要加FOREGROUND_SERVICE 权限,否则跑出异常

 

To request that your service run in the foreground, call startForeground(). This method takes two parameters: an integer that uniquely identifies the notification and the Notification for the status bar. The notification must have a priority of PRIORITY_LOW or higher. Here is an example:

翻译:startForground方法 请求service在前天。这个方法有两个参数(唯一标识符以及Notification 对象)

notificaition的priority要大于 PRIORITY_LOW

举例:

Intent notificationIntent = new Intent(this, ExampleActivity.class);
PendingIntent pendingIntent =
        PendingIntent.getActivity(this, 0, notificationIntent, 0);

Notification notification =
          new Notification.Builder(this, CHANNEL_DEFAULT_IMPORTANCE)
    .setContentTitle(getText(R.string.notification_title))
    .setContentText(getText(R.string.notification_message))
    .setSmallIcon(R.drawable.icon)
    .setContentIntent(pendingIntent)
    .setTicker(getText(R.string.ticker_text))
    .build();

startForeground(ONGOING_NOTIFICATION_ID, notification);

Caution: The integer ID that you give to startForeground() must not be 0.

注意:第一个参数不能为0

 

To remove the service from the foreground, call stopForeground(). This method takes a boolean, which indicates whether to remove the status bar notification as well. This method does not stop the service. However, if you stop the service while it's still running in the foreground, the notification is also removed.

For more information about notifications, see Creating Status Bar Notifications.

翻译:调用stopForeground 取消service的前台属性。 这个方法不stop service, 然而,如果你在service还在前台的时候stop service,notification 也会一并取消。

参考:Creating Status Bar Notifications.

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值