问题描述
- android 判断是否获取应用权限
-
在android 项目中 怎么用代码获取用户是否开启或者拒绝了某一个应用的权限,比如。拍照权限、定位权限、
解决方案
android在开发中有时候要判断应用中是否有某项权限,或者想获取到某个应用的权限清单,可以使用以下方法
PackageManager pm = getPackageManager();
boolean permission = (PackageManager.PERMISSION_GRANTED ==
pm.checkPermission("android.perm......
答案就在这里:android判断应用是否有某个权限
解决方案二:
ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED
解决方案三:
如果是应用层不好判断
但可以通过其他方式知道,用书旗小说为例
安装书旗小说后,运行,在adb 中运行
id com.shuqi.controller
会出现
uid=0(root) gid=0(root) groups=1004(input),1007(log),1011(adb),1015(sdcard_rw),1
028(sdcard_r),3001(net_bt_admin),3002(net_bt),3003(inet),3006(net_bw_stats) cont
ext=u:r:su:s0
这是运行书旗小说的实际权限
源码systemcoreincludeprivateandroid_filesystem_config.h 中定义了
#define AID_ROOT 0 /* traditional unix root user */
#define AID_SYSTEM 1000 /* system server */
#define AID_RADIO 1001 /* telephony subsystem, RIL /
#define AID_BLUETOOTH 1002 / bluetooth subsystem /
#define AID_GRAPHICS 1003 / graphics devices /
#define AID_INPUT 1004 / input devices /
#define AID_AUDIO 1005 / audio devices /
#define AID_CAMERA 1006 / camera devices /
#define AID_LOG 1007 / log devices /
#define AID_COMPASS 1008 / compass device /
#define AID_MOUNT 1009 / mountd socket /
#define AID_WIFI 1010 / wifi subsystem /
#define AID_ADB 1011 / android debug bridge (adbd) /
#define AID_INSTALL 1012 / group for installing packages /
#define AID_MEDIA 1013 / mediaserver process /
#define AID_DHCP 1014 / dhcp client /
#define AID_SDCARD_RW 1015 / external storage write access /
#define AID_VPN 1016 / vpn system /
#define AID_KEYSTORE 1017 / keystore subsystem /
#define AID_USB 1018 / USB devices /
#define AID_DRM 1019 / DRM server /
#define AID_MDNSR 1020 / MulticastDNSResponder (service discovery) /
#define AID_GPS 1021 / GPS daemon /
#define AID_UNUSED1 1022 / deprecated, DO NOT USE /
#define AID_MEDIA_RW 1023 / internal media storage write access /
#define AID_MTP 1024 / MTP USB driver access /
#define AID_UNUSED2 1025 / deprecated, DO NOT USE /
#define AID_DRMRPC 1026 / group for drm rpc /
#define AID_NFC 1027 / nfc subsystem /
#define AID_SDCARD_R 1028 / external storage read access /
#define AID_CLAT 1029 / clat part of nat464 /
#define AID_LOOP_RADIO 1030 / loop radio devices /
#define AID_MEDIA_DRM 1031 / MediaDrm plugins /
#define AID_PACKAGE_INFO 1032 / access to installed package details /
#define AID_SDCARD_PICS 1033 / external storage photos access /
#define AID_SDCARD_AV 1034 / external storage audio/video access /
#define AID_SDCARD_ALL 1035 / access all users external storage /
#define AID_LOGD 1036 / log daemon /
#define AID_SHARED_RELRO 1037 / creator of shared GNU RELRO files */
#define AID_SHELL 2000 /* adb and debug shell user /
#define AID_CACHE 2001 / cache access /
#define AID_DIAG 2002 / access to diagnostic resources */
/* The 3000 series are intended for use as supplemental group id's only.
- They indicate special Android capabilities that the kernel is aware of. /
#define AID_NET_BT_ADMIN 3001 / bluetooth: create any socket /
#define AID_NET_BT 3002 / bluetooth: create sco, rfcomm or l2cap sockets /
#define AID_INET 3003 / can create AF_INET and AF_INET6 sockets /
#define AID_NET_RAW 3004 / can create raw INET sockets /
#define AID_NET_ADMIN 3005 / can configure interfaces and routing tables. /
#define AID_NET_BW_STATS 3006 / read bandwidth statistics /
#define AID_NET_BW_ACCT 3007 / change bandwidth statistics accounting /
#define AID_NET_BT_STACK 3008 / bluetooth: access config files /
#define AID_QCOM_DIAG 3009 / can read/write /dev/diag /
#define AID_IMS 3010 / can read/write /dev/socket/imsrtp /
#define AID_SENSORS 3011 / access to /dev/socket/sensor_ctl_socket & QCCI/QCSI /
#define AID_RFS 3012 / Remote Filesystem for peripheral processors /
#define AID_RFS_SHARED 3013 / Shared files for Remote Filesystem for peripheral processors */
#define AID_EVERYBODY 9997 /* shared between all apps in the same profile */
#define AID_MISC 9998 /* access to misc storage */
#define AID_NOBODY 9999
#define AID_APP 10000 /* first app user */
#define AID_ISOLATED_START 99000 /* start of uids for fully isolated sandboxed processes /
#define AID_ISOLATED_END 99999 / end of uids for fully isolated sandboxed processes */
#define AID_USER 100000 /* offset for uid ranges for each user */
#define AID_SHARED_GID_START 50000 /* start of gids for apps in each user to share /
#define AID_SHARED_GID_END 59999 / start of gids for apps in each user to share */
#if !defined(EXCLUDE_FS_CONFIG_STRUCTURES)
struct android_id_info {
const char *name;
unsigned aid;
};
static const struct android_id_info android_ids[] = {
{ "root", AID_ROOT, },
{ "system", AID_SYSTEM, },
{ "radio", AID_RADIO, },
{ "bluetooth", AID_BLUETOOTH, },
{ "graphics", AID_GRAPHICS, },
{ "input", AID_INPUT, },
{ "audio", AID_AUDIO, },
{ "camera", AID_CAMERA, },
{ "log", AID_LOG, },
{ "compass", AID_COMPASS, },
{ "mount", AID_MOUNT, },
{ "wifi", AID_WIFI, },
{ "adb", AID_ADB, },
{ "install", AID_INSTALL, },
{ "media", AID_MEDIA, },
{ "dhcp", AID_DHCP, },
{ "sdcard_rw", AID_SDCARD_RW, },
{ "vpn", AID_VPN, },
{ "keystore", AID_KEYSTORE, },
{ "usb", AID_USB, },
{ "drm", AID_DRM, },
{ "mdnsr", AID_MDNSR, },
{ "gps", AID_GPS, },
// AID_UNUSED1
{ "media_rw", AID_MEDIA_RW, },
{ "mtp", AID_MTP, },
// AID_UNUSED2
{ "drmrpc", AID_DRMRPC, },
{ "nfc", AID_NFC, },
{ "sdcard_r", AID_SDCARD_R, },
{ "clat", AID_CLAT, },
{ "loop_radio", AID_LOOP_RADIO, },
{ "mediadrm", AID_MEDIA_DRM, },
{ "package_info", AID_PACKAGE_INFO, },
{ "sdcard_pics", AID_SDCARD_PICS, },
{ "sdcard_av", AID_SDCARD_AV, },
{ "sdcard_all", AID_SDCARD_ALL, },
{ "logd", AID_LOGD, },
{ "shared_relro", AID_SHARED_RELRO, },
{ "shell", AID_SHELL, },
{ "cache", AID_CACHE, },
{ "diag", AID_DIAG, },
{ "net_bt_admin", AID_NET_BT_ADMIN, },
{ "net_bt", AID_NET_BT, },
{ "inet", AID_INET, },
{ "net_raw", AID_NET_RAW, },
{ "net_admin", AID_NET_ADMIN, },
{ "net_bw_stats", AID_NET_BW_STATS, },
{ "qcom_diag", AID_QCOM_DIAG, },
{ "ims", AID_IMS, },
{ "net_bw_acct", AID_NET_BW_ACCT, },
{ "net_bt_stack", AID_NET_BT_STACK, },
{ "qcom_diag", AID_QCOM_DIAG, },
{ "sensors", AID_SENSORS, },
{ "rfs", AID_RFS, },
{ "rfs_shared", AID_RFS_SHARED, },
{ "everybody", AID_EVERYBODY, },
{ "misc", AID_MISC, },
{ "nobody", AID_NOBODY, },
};
platform.xml中定义
<permission name="android.permission.BLUETOOTH" >
<group gid="net_bt" />
</permission>
<permission name="android.permission.BLUETOOTH_STACK" >
<group gid="net_bt_stack" />
</permission>
<permission name="android.permission.NET_TUNNELING" >
<group gid="vpn" />
</permission>
<permission name="android.permission.INTERNET" >
<group gid="inet" />
</permission>
<permission name="android.permission.READ_LOGS" >
<group gid="log" />
</permission>
<permission name="android.permission.READ_EXTERNAL_STORAGE" >
<group gid="sdcard_r" />
</permission>
<permission name="android.permission.WRITE_EXTERNAL_STORAGE" >
<group gid="sdcard_r" />
<group gid="sdcard_rw" />
<group gid="media_rw" />
</permission>
<permission name="android.permission.ACCESS_ALL_EXTERNAL_STORAGE" >
<group gid="sdcard_r" />
<group gid="sdcard_rw" />
<group gid="sdcard_all" />
</permission>
<permission name="android.permission.WRITE_MEDIA_STORAGE" >
<group gid="media_rw" />
</permission>
<permission name="android.permission.ACCESS_MTP" >
<group gid="mtp" />
</permission>
<permission name="android.permission.NET_ADMIN" >
<group gid="net_admin" />
</permission>
<!-- The group that /cache belongs to, linked to the permission
set on the applications that can access /cache -->
<permission name="android.permission.ACCESS_CACHE_FILESYSTEM" >
<group gid="cache" />
</permission>
<!-- RW permissions to any system resources owned by group 'diag'.
This is for carrier and manufacture diagnostics tools that must be
installable from the framework. Be careful. -->
<permission name="android.permission.DIAGNOSTIC" >
<group gid="input" />
<group gid="diag" />
</permission>
<!-- Group that can read detailed network usage statistics -->
<permission name="android.permission.READ_NETWORK_USAGE_HISTORY">
<group gid="net_bw_stats" />
</permission>
<!-- Group that can modify how network statistics are accounted -->
<permission name="android.permission.MODIFY_NETWORK_ACCOUNTING">
<group gid="net_bw_acct" />
</permission>
<permission name="android.permission.LOOP_RADIO" >
<group gid="loop_radio" />
</permission>
<!-- Hotword training apps sometimes need a GID to talk with low-level
hardware; give them audio for now until full HAL support is added. -->
<permission name="android.permission.MANAGE_VOICE_KEYPHRASES">
<group gid="audio" />
</permission>
<permission name="android.permission.ACCESS_FM_RADIO" >
<group gid="media" />
</permission>
<!-- ================================================================== -->
<!-- ================================================================== -->
<!-- ================================================================== -->
<!-- The following tags are assigning high-level permissions to specific
user IDs. These are used to allow specific core system users to
perform the given operations with the higher-level framework. For
example, we give a wide variety of permissions to the shell user
since that is the user the adb shell runs under and developers and
others should have a fairly open environment in which to
interact with the system. -->
<assign-permission name="android.permission.MODIFY_AUDIO_SETTINGS" uid="media" />
<assign-permission name="android.permission.ACCESS_SURFACE_FLINGER" uid="media" />
<assign-permission name="android.permission.WAKE_LOCK" uid="media" />
<assign-permission name="android.permission.UPDATE_DEVICE_STATS" uid="media" />
<assign-permission name="android.permission.UPDATE_APP_OPS_STATS" uid="media" />
<assign-permission name="android.permission.ACCESS_SURFACE_FLINGER" uid="graphics" />
这样就将permission和gid联系起来了
从应用中查看申请的permission,和实际运行过程中对应的gids就可以看出来,那些权限禁止了
解决方案四:
看清楚问题再回答好吗?用户拒绝开启了定位权限,这个状态获取,然后去做处理。同问
解决方案五:
判断应用是否具有某个权限
PackageManager pm = getPackageManager();
boolean permission = (PackageManager.PERMISSION_GRANTED ==
pm.checkPermission("android.permission.RECORD_AUDIO", "packageName"));
if (permission) {
showToast("有这个权限");
}else {
showToast("木有这个权限");
}
获取某个应用的权限清单
try {
PackageInfo pack = pm.getPackageInfo("packageName",PackageManager.GET_PERMISSIONS);
String[] permissionStrings = pack.requestedPermissions;
showToast("权限清单--->" + permissionStrings.toString());
} catch (NameNotFoundException e) {
e.printStackTrace();
}