如果应用请求权限,如何获取?

早上好

我正在寻找一种方法来了解应用程序是否在给定包名称的情况下请求特定权限以及是否授予该权限(通过设置)。我一直在寻找类似的东西:

if(//com.package.name requests Manifest.permission.camera and that permission is granted){//do something}

我已经阅读了这个线程http://stackoverflow.com/questions/31535088/android-m-programmatically-revoke-permissions但它没有找到一种方法来知道应用程序是否真的在请求某个权限以及它是否被授予.

提前致谢


白猪掌柜的
浏览 97回答 1
1回答

红颜莎娜

在您的活动中使用以下代码:我创建StringBuffer appNameAndPermissions = new StringBuffer();以附加所有应用程序和权限信息。它工作正常。我已经测试过了。如果您有任何问题,请告诉我。StringBuffer appNameAndPermissions = new StringBuffer();PackageManager pm = getPackageManager();List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);for (ApplicationInfo applicationInfo : packages) {&nbsp; &nbsp; Log.d("test", "App: " + applicationInfo.name + " Package: " + applicationInfo.packageName);try {&nbsp; &nbsp; PackageInfo packageInfo = pm.getPackageInfo(applicationInfo.packageName, PackageManager.GET_PERMISSIONS);&nbsp; &nbsp; appNameAndPermissions.append(packageInfo.packageName+"*******:\n");&nbsp; &nbsp; //Get Permissions&nbsp; &nbsp; String[] requestedPermissions = packageInfo.requestedPermissions;&nbsp; &nbsp; if(requestedPermissions != null) {&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < requestedPermissions.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.d("test", requestedPermissions[i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; appNameAndPermissions.append(requestedPermissions[i]+"\n");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; appNameAndPermissions.append("\n");} catch (NameNotFoundException e) {&nbsp; &nbsp; e.printStackTrace();}在您的 AndroidManifest.xml 文件中使用以下权限:<uses-permission android:name="android.permission.GET_TASKS"/>来源和更多答案在这里
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java