使Android成为“使用权限”可选的 [英] Make Android "uses-permission" optional

查看:127
本文介绍了使Android成为“使用权限”可选的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这些:

 < uses-permission android:name =android.permission.ACCESS_COARSE_LOCATION/> 
< uses-permission android:name =android.permission.ACCESS_FINE_LOCATION/>
< uses-feature android:name =android.hardware.location.gpsandroid:required =false/>

我可以自己检查GPS:

  locationManager =(LocationManager)
context.getSystemService(Context.LOCATION_SERVICE);

fSupportsGPS =(locationManager.getAllProviders()。
包含(LocationManager.GPS_PROVIDER));

但我仍然有些怀疑。我希望我的应用可以在没有wifi / gps /网络的设备上运行。原因是我使用GPS的唯一目的是在Google地图上显示用户位置。 (与其他应用程序相比,这是一个小功能,所以我不想对它有任何要求。)

解决方案

<使用-feature ...> 告诉Android,该应用必须需要该功能,并且无法使用它,所以Android不会让用户安装应用程序(实际上,它甚至不会显示在Play商店/ Market Place中。)



要让没有这些功能的用户安装您的应用程序应用程序中,您只需为适当的功能添加 android:required =false选项即可。您已经在清单中使用此XML完成了这项工作:

 < uses-feature android:name =android.hardware.cameraandroid:required =false /> 






检测用户是否拥有featu无论是否(现在不是强制他们拥有它),您可以使用Android API。你已经完成了这个Java代码:

  locationManager =(LocationManager)context.getSystemService Context.LOCATION_SERVICE); 
fSupportsGPS =(locationManager.getAllProviders()。contains(LocationManager.GPS_PROVIDER));






现在,你不能做的一件事就是让使用Android 1.6及更早版本的用户无法使用这些功能来安装应用程序,即使这些功能被标记为可选。这是b'co​​z,该版本的Android无法识别 android:required 功能,如这个答案






另外要记住的是,一些标签会触发一些隐含的权限要求。

为了解决这个问题,从文档引用:


如果您不希望Google Play根据特定的隐含
功能进行过滤,则可以禁用该行为。为此,请在元素中明确声明特征
,并包含
android:required =false属性。例如,要禁用从CAMERA权限导出的过滤
,您将声明该功能为
,如下所示。 pre-class =lang-xml prettyprint-override> < uses-feature android:name =android.hardware.cameraandroid:required =false/>

根据 this page < uses-permission android:name =android.permission.ACCESS_COARSE_LOCATION/> 意味着网络的需求。要禁用该功能,请将其添加到您的清单中:

 < uses-feature android:name =android.hardware.location。网络android:required =false/> 


I have these:

  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />  
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
  <uses-feature android:name="android.hardware.location.gps" android:required="false" />      

And I can check for GPS myself doing this:

locationManager = (LocationManager) 
    context.getSystemService(Context.LOCATION_SERVICE);

fSupportsGPS = (locationManager.getAllProviders().
    contains(LocationManager.GPS_PROVIDER));

But I still have some doubts. I want my app to run on devices with no wifi/gps/network available. The reason being the only thing I use GPS for is showing user locaion on Google maps. (And that is a minute feature compared to rest of app, so I don't want to have any requirements for it.)

解决方案

The <uses-feature ...> tells Android that the app must require the feature and won't work w/o it, so Android doesn't let users install the app (in fact, it won't even show up in the Play Store/Market Place.

To let users who don't have those features install your app, you just have to add the android:required="false" option for the appropriate features. That you have already done in the manifest using this XML:

<uses-feature android:name="android.hardware.camera" android:required="false"/>


To detect if the user has the feature or not (as it is now not compulsory they have it), you can use the Android APIs. This you have already done with this Java code:

locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);   
  fSupportsGPS = (locationManager.getAllProviders().contains(LocationManager.GPS_PROVIDER));


Now, one thing that you cannot do is let users using Android 1.6 and earlier w/o the features install the apps, even if the features are marked as optional. This is b'coz that version of Android doesn't recognize the android:required feature as is explained in this answer.


Another thing to keep in mind is that some <uses-permission ...> tags will trigger some implicit requirements for the permission.

To address this issue, quoting from the docs:

If you don't want Google Play to filter based on a specific implied feature, you can disable that behavior. To do so, declare the feature explicitly in a element and include an android:required="false" attribute. For example, to disable filtering derived from the CAMERA permission, you would declare the feature as shown below.

<uses-feature android:name="android.hardware.camera" android:required="false" />

According to this page, the <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> implies the requirement of a network. To disable that, add this to your manifest:

<uses-feature android:name="android.hardware.location.network" android:required="false" />

这篇关于使Android成为“使用权限”可选的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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