如何在不显示通知的情况下 startForeground()? [英] How to startForeground() without showing notification?

查看:46
本文介绍了如何在不显示通知的情况下 startForeground()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个服务并让它在前台运行.

I want to create a service and make it run in the foreground.

大多数示例代码都有通知.但我不想显示任何通知.那可能吗?

Most example codes have notifications on it. But I don't want to show any notification. Is that possible?

你能给我举一些例子吗?有没有其他选择?

Can you give me some examples? Are there any alternatives?

我的应用服务正在做媒体播放器.如何让系统不终止我的服务,除非应用程序自行终止它(例如通过按钮暂停或停止音乐).

My app service is doing mediaplayer. How to make system not kill my service except the app kill it itself (like pausing or stopping the music by button).

推荐答案

作为 Android 平台的一项安全功能,您不能,在任何情况下,有一个前景服务而没有通知.这是因为前台服务比后台服务消耗更多的资源并且受到不同的调度约束(即,它不会很快被终止),并且用户需要知道什么可能会消耗他们的电池.所以,不要这样做.

As a security feature of the Android platform, you cannot, under any circumstance, have a foregrounded service without also having a notification. This is because a foregrounded service consumes a heavier amount of resources and is subject to different scheduling constraints (i.e., it doesn't get killed as quickly) than background services, and the user needs to know what's possibly eating their battery. So, don't do this.

然而,它可能有一个假"通知,即,您可以制作一个透明的通知图标 (iirc).这对您的用户来说是非常不诚实的,而且您没有理由这样做,除了耗尽他们的电池并因此创建恶意软件.

However, it is possible to have a "fake" notification, i.e., you can make a transparent notification icon (iirc). This is extremely disingenuous to your users, and you have no reason to do it, other than killing their battery and thus creating malware.

这篇关于如何在不显示通知的情况下 startForeground()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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