简体   繁体   English

当我的应用程序启动前台服务时,如果用户在设置中关闭了通知权限,那么服务是否在前台?

[英]When my app starts a foreground service, is the service in foreground if user has turned off notification permission in settings?

In Android system, when my app starts a foreground service, is the service really a foreground service, if user has turned off the notification permission in the settings ? 在Android系统中,当我的应用程序启动前台服务时,如果用户已关闭设置中的通知权限,该服务是否真的是前台服务?

I am afraid that without a notification, it is a background service in fact, and can't be guaranteed to be finished. 我担心没有通知,它实际上是后台服务,不能保证完成。

I do such work in my service's onStartCommand function: 我在我的服务的onStartCommand函数中做了这样的工作:

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "myId")
                .setSmallIcon(R.drawable.all_downloads_icon)
                .setContentTitle("my title")
                .setContentText("you have started a notificaiton")
                .setPriority(NotificationCompat.PRIORITY_DEFAULT);
        Notification notification = builder.build();
        startForeground(110,notification);

The answer is yes. 答案是肯定的。 I use adb shell to see the service information, it is still a foreground service. 我使用adb shell来查看服务信息,它仍然是一个前台服务。 And thanks for the question edit suggestion. 并感谢编辑建议的问题。

adb shell
activity services DownloadService(your service's class name)

isForeground=true foregroundId=110 foregroundNoti=Notification(channel=myId pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x40 color=0x00000000 vis=PRIVATE) createTime=-2s764ms startingBgTimeout=-- lastActivity=-1s772ms restartTime=-2s763ms createdFromFg=true startRequested=true delayedStop=false stopIfKilled=false callStart=true lastStartId=5 isForeground = true foregroundId = 110 foregroundNoti =通知(channel = myId pri = 0 contentView = null vibrate = null sound = null defaults = 0x0 flags = 0x40 color = 0x00000000 vis = PRIVATE)createTime = -2s764ms startingBgTimeout = - lastActivity = -1s772ms restartTime = -2s763ms createdFromFg = true startRequested = true delayedStop = false stopIfKilled = false callStart = true lastStartId = 5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM