如何不显示通知就启动startForeground()? [英] How to startForeground() without showing notification?

查看:356
本文介绍了如何不显示通知就启动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平台的安全功能,在 any 情况下不能服务也没有通知.这是因为与后台服务相比,前台服务会消耗大量资源,并且受制于不同的调度约束(即不会很快消失),并且用户需要知道可能在消耗什么电量.所以,不要这样做.

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天全站免登陆