android 判断是否获取应用权限

问题描述

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();

}

时间: 2025-01-27 18:40:08

android 判断是否获取应用权限的相关文章

Android判断是否有拍照权限的实例代码

下面一段代码给大家介绍android判断是否有拍照权限,具体代码如下所示: /** * 返回true 表示可以使用 返回false表示不可以使用 */ public boolean cameraIsCanUse() { boolean isCanUse = true; Camera mCamera = null; try { mCamera = Camera.open(); Camera.Parameters mParameters = mCamera.getParameters(); //针对

新 Android 恶意程序获取 Root 权限卸载安全软件

FireEye Labs的安全研究人员发现了一个源自中国的Android恶意程序家族, 它正迅速传播到全世界20多个国家,它的指令控制服务器(CC)域名是aps.kemoge.net,因此被取名为Kemoge.Kemoge将合法应用 重新打包,伪装成合法应用上传到第三方应用商店,通过网站和广告宣传,用户一旦安装之后它会收集设备信息上传到广告服务器,然后用广告轰炸用户. Kemoge一开始只是恼人,但很快它会变得邪恶.它会调用一个多重加密的压缩文件,该压缩文件包含了最多8个root利用可执行工具,

Android应用程序获取ROOT权限的方法(android中如何通过代码检测是否有root权限?)

public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); doSU(); } @Override protected void onResume() { super.onResume();

通讯录-android 如何判断没有获取联系人的权限

问题描述 android 如何判断没有获取联系人的权限 获取通讯录联系人时,如果用户禁止本应用获取联系人权限,我如何得到这个状态并处理呢 解决方案 Android手机root权限的判断与获取 解决方案二: 判断有没有某个权限,貌似API里面是判断不了的. 如果你生命了一个权限. 在使用的时候加上 try{ }catch{ } 如果catch 了,就表示当前没有这个权限.

Android获取ROOT权限的实例代码_Android

获取Android的ROOT权限其实很简单,只要在Runtime下执行命令"su"就可以了. 复制代码 代码如下: // 获取ROOT权限public void get_root(){     if (is_root()){        Toast.makeText(mCtx, "已经具有ROOT权限!", Toast.LENGTH_LONG).show();    }    else{        try{            progress_dialog

android 中APK如何获取root权限?

问题描述 android 中APK如何获取root权限? android APK中如何获取到root权限,从而能切换到执行诸如exec = Runtime.getRuntime().exec(""su -c ""+abspath); 语句?eng版本 具有root权限吗?可是执行时报错:su: uid xxx not allowed to su adb root 和system root有区别吗? user版本如何仅在开发的APK中获取root权限?user版本在我

Android获取ROOT权限的实例代码

获取Android的ROOT权限其实很简单,只要在Runtime下执行命令"su"就可以了. 复制代码 代码如下:// 获取ROOT权限public void get_root(){ if (is_root()){        Toast.makeText(mCtx, "已经具有ROOT权限!", Toast.LENGTH_LONG).show();    }    else{        try{            progress_dialog = Pr

Android系统获取Root权限原理详解

android root权限破解分析 许多机友新购来的Android机器没有破解过Root权限,无法使用一些需要高权限的软件,以及进行一些高权限的操作,其实破解手机Root权限是比较简单及安全的,破解Root权限的原理就是在手机的/system/bin/或/system/xbin/目录下放置一个可执行文件"su",这是一个二进制文件,相当于电脑上的exe文件,仅仅在系统中置入这个"su"文件是不会给手机的软件或硬件造成任何故障. 下面的代码是android系统原版的

android中获取root权限的方法以及原理(转)

一. 概述 本文介绍了android中获取root权限的方法以及原理,让大家对android 玩家中常说的"越狱"有一个更深层次的认识. 二. Root 的介绍 1. Root 的目的 可以让我们拥有掌控手机系统的权限,比如删除一些system/app下面的无用软件,更换开关机铃声和动画,拦截状态栏弹出的广告等. 2. Root的原理介绍 谷歌的android系统管理员用户就叫做root,该帐户拥有整个系统至高无上的权利,它可以访问和修改你手机几乎所有的文件,只有root才具备最高级别