服务可以在Android M中请求许可吗? [英] Can a service request for permission in Android M?

查看:70
本文介绍了服务可以在Android M中请求许可吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为在M个requestPermissions()中只能由Activity类调用.我有一个没有UI屏幕的应用程序.它作为后台服务运行.这是否意味着如果checkSelfPermissions返回拒绝,我必须放置微调器屏幕?

I figured that in M requestPermissions() can only be called by an Activity class. I have an app that has no UI screen. It runs as a background service. Does this mean that I have to put a spinner screen if checkSelfPermissions returns denied?

推荐答案

我有一个没有UI屏幕的应用程序.

I have an app that has no UI screen.

然后它将永远不会运行,除非它已预先安装在Android设备或自定义ROM上,或者它是某些其他应用程序的插件.否则,您的应用将永远保持停止状态.通过正常渠道(包括Play商店)分发的每个应用程序几乎都需要一项活动.

Then it will never run, unless it is preinstalled on an Android device or custom ROM, or it is a plugin to some other app. Otherwise, your app will remain in the stopped state forever. Pretty much every app distributed through normal channels, including the Play Store, needs an activity.

这是否意味着如果checkSelfPermissions返回被拒绝,我必须放置一个微调器屏幕?

Does this mean that I have to put a spinner screen if checkSelfPermissions returns denied?

我不知道什么是微调器屏幕". AFAIK,对于不需要运行时权限的服务,推荐的模式是:

I do not know what "a spinner screen" is. AFAIK, the recommended pattern for a service needing a runtime permission that it does not have is to:

  1. 引发Notification,以使用户知道该服务在没有此权限的情况下无法完成其工作.

  1. Raise a Notification, to let the user know that the service cannot do its work without this permission.

使Notification触发可以调用requestPermissions()Activity. (可选)该活动可以具有Theme.Translucent.NoTitleBar,因此唯一可见的UI是权限对话框.

Have the Notification trigger an Activity that can call requestPermissions(). Optionally, this activity can have Theme.Translucent.NoTitleBar, so the only visible UI is the permission dialog.

如果onRequestPermissionResult()表示您具有许可,则活动可以告诉服务继续进行(例如,通过调用startService()),然后告诉finish()本身.如果onRequestPermissionResult()指示用户拒绝了该权限,请执行任何有意义的操作(例如,再次显示Notification,正常关闭,并向用户建议用户卸载该应用).

If onRequestPermissionResult() indicates that you have the permission, the activity can tell the service to go ahead (e.g., via a call to startService()), then finish() itself. If onRequestPermissionResult() indicates that the user denied the permission, do whatever makes sense (e.g., show the Notification again, gracefully shut down, suggest to the user that the user uninstall the app).

这篇关于服务可以在Android M中请求许可吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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