使服务成为前台服务 [英] making a service a foreground service

查看:41
本文介绍了使服务成为前台服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个服务,我想制作一个前台服务.我的服务不需要与用户沟通.我在这个论坛看到的最简单的例子就是把这段代码放到服务中...

I have a service that I want to make a Foreground service. My service has no need to communicate to the user. The simplest example I have seen in this forum is to put this code in the service...

    Notification notification = new Notification();
    startForeground(5481, notification);

我在 Commonsware book pp 606 中看到了一个更复杂的例子,它使用了一个通知生成器 (NotificationCompat.Builder).这个和其他例子需要提供复杂的通知.我没有这个需求.

I have seen a much more complicated example in the Commonsware book pp 606 that uses a Notification Builder (NotificationCompat.Builder). This and other examples have a need to provide complex notifications. I don't have this need.

我明白,为了让我的软件能够被接受,我必须至少显示一个图标,让用户知道我的服务正在运行,并为用户提供取消它的方法.此外,如果我的服务决定自行停止,则该图标应该消失.但就是这样.这就是我需要做的.

I understand that, for my software to be acceptable, I must at a minimum display an icon that makes the user aware that my service is running and give the user a way to cancel it. Also If my service decides to stop on it's own, the icon should go away. But that's it. That's all I need to do.

我需要在上面的tode中添加什么才能实现这一点?谢谢,加里

What would I have to add to the above tode to accomplish that? thanks, Gary

推荐答案

你读过 ​​Running a Service in前景?

您的通知需要一个图标才能显示.系统不会显示没有图标的通知.

Your notification needs an icon so that it will show up. The system won't display notifications that don't have icons.

使用 NotificationCompat.Builder 的原因是 a) 它保持跨平台的兼容性 b) 使用它可以更容易地创建简单的通知.您可以使用 Builder 创建复杂的通知,但也可以使用它来构建简单的通知.

The reasons to use NotificationCompat.Builder are a) it maintains compatibility across platforms and b) with it, it's easier to create simple notifications. You can use Builder to create complex notifications, but you can use it to build simple ones as well.

如果不知道你想做什么,很难说更多.

It's hard to say more without knowing what you're trying to do.

这篇关于使服务成为前台服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆