从服务或模型组件处理Android 6.0的新权限要求的选项? [英] Options for dealing with Android 6.0's new permissions requirements from a service or model component?

查看:78
本文介绍了从服务或模型组件处理Android 6.0的新权限要求的选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑移植一些现有代码,以考虑Android M处理权限的新方法.但是,权限API需要具有与其相关联的活动(例如requestPermissions()方法的第一个参数是活动).

I'm looking into porting some existing code to take Android M's new way of dealing with permissions into consideration. However the permission API needs to have an activity associated with it (for example the requestPermissions() method's first parameter is an activity).

如果服务没有活动,那么需要检查是否已授予权限并请求权限的服务应如何使用此新API?

So how should a service that needs to check if a permissions has been granted and request for permissions use this new API if the service doesn't have an activity?

服务是否有可能创建仅与权限API一起使用的虚拟不可见活动? (如果可能的话,我还是不喜欢这样做的想法.)

Is it possible for the service to create a dummy invisible activity just for use with the permissions API? (if its possible I don't like the thought of doing that anyway though).

或者假设它不是服务,而是需要执行权限检查的模型类,在MVC中,模型对Vs和Cs不应该有任何了解,但现在它必须要知道要进行哪个Activity与权限API一起使用.否则可能需要将大量代码从模型代码迁移到活动代码.

Or suppose its not a service but a model class that needs to perform a permissions check, in MVC a model shouldn't have any knowledge of the Vs and Cs and yet now either it has to in order to know which Activity to use with the permission API. Or potentially lots of code might have to migrate from model code into Activity code.

关于如何将非活动代码迁移到Android 6.0上需要检查/提示权限的任何想法?

Any thoughts on how to migrate non activity based code that needs to check/prompt for permissions over to Android 6.0?

更新:我遗漏了一条重要信息-这是预先安装的代码(我们公司提供了设备制造商位于rom中的代码),并且通常可能在设备启动时运行并在后台运行.因此,通常不会在用户启动应用程序或更高版本时提示用户进行许可的通常情况(因此此时存在活动).

Update: I left out an important piece of information - this is code that is pre-installed (our company provides code that device manufacture's place in rom) and often may be run at device boot time and run in the background. Therefore the usual situation of a user being prompted for permission when they launch the app or later (and there therefore being an activity at that point) does not necessarily apply.

推荐答案

如果服务没有活动,那么需要检查是否已授予权限并请求权限的服务应如何使用此新API?

So how should a service that needs to check if a permissions has been granted and request for permissions use this new API if the service doesn't have an activity?

几乎总是有活动,除了预安装的应用程序和其他应用程序的插件.否则,您的服务将永远无法运行,因为没有任何东西会使用显式的Intent来启动应用程序的组件之一,因此它将保持在停止状态.

There is almost always an activity, except for pre-installed apps and plugins for other apps. Otherwise, your service is unlikely to ever run, as nothing will have used an explicit Intent to start up one of your app's components, so it will remain in the stopped state.

对于约99.9%的具有活动的Android应用,如果该应用的整个操作需要权限,则在首次运行时请求它们.如Snild Dolkow所述,如果用户以后通过设置"撤消了权限,则可以检测到没有活动,然后使用其他UI选项(例如Notification,应用小部件)让用户知道操作已暂停,直到他们授予再次为您提供权限,然后他们将通过您的活动进行操作.

For the ~99.9% of Android apps that have an activity already, if the permissions are needed for the whole operation of the app, request them on first run. As Snild Dolkow notes, if the user later revokes the permission through Settings, you can detect that without an activity, then use other UI options (e.g., Notification, app widget) to let the user know that operation is suspended until they grant you the permissions again, which they would then do through your activity.

服务是否有可能创建仅与权限API一起使用的虚拟不可见活动?

Is it possible for the service to create a dummy invisible activity just for use with the permissions API?

大概您可以使用requestPermissions()进行Theme.NoDisplay活动.但是,从用户的角度来看,除非他们与之交互的其他UI(应用程序小部件?),否则这没有多大意义.突然弹出权限对话框不太可能使您受欢迎.

Presumably you can have a Theme.NoDisplay activity use requestPermissions(). However, from the user's standpoint, it will not make much sense, unless there's some alternative UI (app widget?) that they are interacting with. Popping up a permission dialog out of nowhere is unlikely to make you popular.

更新2019-06-15 :请注意,Android Q禁止在后台弹出活动.请改用通知.

UPDATE 2019-06-15: Note that Android Q bans services popping up activities frmo the background. Please use a notification instead.

在MVC中,模型不应该对Vs和Cs有任何了解,但是现在它必须要知道与权限API一起使用哪个Activity

in MVC a model shouldn't have any knowledge of the Vs and Cs and yet now either it has to in order to know which Activity to use with the permission API

在您请求许可之前,请勿触摸模型,否则,如果许可被撤消,它将正常失败.在其他情况下(磁盘空间不足,没有Internet连接等),您已经必须优雅地失败,因此,应该以几乎相同的方式来处理撤消的许可.

Do not touch the models until you have requested the permission, and gracefully fail if the permission is revoked. You already have to gracefully fail in other circumstances (out of disk space, no Internet connection, etc.), so a revoked permission should be handled in much the same way.

使用这个新的6.0 API似乎是不良设计和紧密耦合的秘诀

using this new 6.0 API seems like an recipe for bad design and tight coupling

欢迎您提出意见.根据我的阅读,Android工程师认为向用户询问权限是用户体验的一部分,因此最好在UI层进行处理.

You are welcome to your opinion. Based on what I have read, the Android engineers believe that asking the user for permissions is part of the user experience and is best handled at the UI layer as a result.

再次: vast 大多数Android应用程序都没有问题,因为它们具有用户界面.没有用户界面且需要dangerous权限的应用程序需要进行大量的返工.

Again: the vast majority of Android apps will not have a problem with this, as they have a user interface. Apps that do not have a user interface and need dangerous permissions are in for some amount of rework.

这是预先安装的代码(我们公司提供的代码表明设备制造商位于rom中),并且通常可以在设备启动时运行

this is code that is pre-installed (our company provides code that device manufacture's place in rom) and often may be run at device boot time

首先,请了解,这与法线相距甚远,由于地球的曲率,您甚至无法从所在位置看到. :-)您真的不能抱怨Google没有优化这种特殊情况.

First, please understand that this is so far from normal that you can't even see normal from where you are due to the curvature of the Earth. :-) You can't really complain that Google did not optimize this particular scenario.

据我了解,即使系统应用程序也应要求运行时权限.例如,相机"应用程序在6.0预览版上运行.话虽这么说,设备上某处必须有一些数据库在跟踪已授予的内容,并且大概有某种方法可以预先填充它.但是,大概用户仍然可以从设置"中将其撤消.但是,制造商可能会采取一些绝技措施(例如,与设置"应用混为一谈),甚至可能排除这种情况.我会在与如何获得它,以便我的应用不能被迫停止?"相同的领域中寻找?设备制造商可以做到的.

As I understand it, even system apps should be asking for runtime permissions. The Camera app did, for example, on the 6.0 preview. That being said, there's gotta be some database on the device somewhere that is tracking what has been granted, and presumably there is some way to pre-populate it. However, the user could still revoke it from Settings, presumably. But, the manufacturer could pull some stunts (e.g., messing with the Settings app) to possibly even preclude that scenario. I'd be looking in the same area as "how do I get it so my app cannot be force-stopped?" that device manufacturers can do.

您的替代选择是摆脱dangerous权限,或者将您的应用从SDK迁移到标准Linux二进制文件中,该二进制文件将作为引导过程的一部分运行,并放入具有以下功能的Linux用户组中:访问所需的内容.

Your alternatives would be to get rid of the dangerous permissions or to migrate your app off the SDK and into a standard Linux binary that would be run as part of the boot process and be put into a Linux user group that has access to the stuff that you need.

这篇关于从服务或模型组件处理Android 6.0的新权限要求的选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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