前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Android从零单排系列四十四】《聊一下Android数据权限permission》

【Android从零单排系列四十四】《聊一下Android数据权限permission》

作者头像
再见孙悟空_
发布2023-07-17 20:37:00
6900
发布2023-07-17 20:37:00
举报

前言

小伙伴们,在前面的几篇文章中,我们谈到了Android开发中的几种数据存储方式及数据持久化,本文我们介绍下Android开发中的另一部分内容,权限管理。

一.Android 数据权限基本介绍

在Android中,权限管理是确保应用程序能够安全地访问系统功能和用户敏感信息的重要方面。以下是关于Android权限管理的一些要点:

  1. 权限类型:Android权限分为两种类型:危险权限(Dangerous Permissions)和普通权限(Normal Permissions)。危险权限涉及到用户的敏感信息和设备功能,需要经过用户授权才能使用;而普通权限不需要用户明确授权,系统会自动授予。
  2. 权限申请:对于危险权限,应用需要在清单文件(AndroidManifest.xml)中声明,并且在运行时向用户请求授权。可以使用requestPermissions()方法请求权限,并在回调方法中处理授权结果。
  3. 权限组:Android将一些相关的危险权限分组,用户在授权时只需给予该组的一项权限授予即可。例如,如果应用需要访问摄像头和录音权限,只需申请android.permission-group.CAMERA权限组即可,用户只需授予其中一项。
  4. 权限检查:在代码中,可以使用checkSelfPermission()方法来检查当前是否已经拥有某个权限。如果没有权限,则需要通过用户授权才能继续执行相关操作。
  5. 权限处理:当应用请求权限时,用户可以选择授予或拒绝权限。开发者需要在回调方法中处理用户的授权结果,并根据结果来执行相应的操作。可以使用shouldShowRequestPermissionRationale()方法来判断是否需要向用户解释请求权限的原因。
  6. 运行时权限:自Android 6.0(API级别23)起,引入了运行时权限机制。对于危险权限,即使在清单文件中声明了,应用程序依然需要在运行时动态请求权限。如果应用要兼容6.0及以上的设备,就需要在代码中处理权限请求和授权流程。
  7. 权限回退:即使用户已经授权了某个权限,在应用运行期间也有可能被用户取消授权。开发者需要通过onRequestPermissionsResult()方法处理用户对权限的撤销操作,并相应地调整

二 Android 权限分类

在Android中,权限级别分为三个层次:正常权限(Normal Permissions)、危险权限(Dangerous Permissions)和特殊权限(Special Permissions)。

  1. 正常权限(Normal Permissions):这些权限不会直接涉及用户的隐私或敏感数据,通常不需要用户的明确授权。应用程序在清单文件中声明这些权限后,系统会自动授予它们。例如,访问网络状态、访问网络设置、访问文件系统等。
  2. 危险权限(Dangerous Permissions):这些权限涉及到用户的隐私和敏感数据,并且可能对用户造成潜在风险。应用程序在使用这些权限之前必须向用户请求权限,并得到用户的明确授权才能访问。例如,读取联系人、获取位置信息、拍照等。用户可以在应用安装或运行时选择是否授予这些权限。
  3. 特殊权限(Special Permissions):这些权限是一些特殊功能或设备所需的权限,通常需要用户在系统设置中手动授权。例如,修改系统设置、更改电池优化设置、显示悬浮窗等。这些权限通常对用户的设备和数据具有更高级别的控制,因此用户必须明确授权才能使用它们。

应用程序开发者在使用危险权限时,需要通过运行时权限机制来请求和管理这些权限,以确保用户的隐私和安全。用户可以在应用的权限设置中随时查看和修改已授予的权限。

三 Android 权限清单

  • android.permission.ACCESS_CHECKIN_PROPERTIES允许读写访问"properties"表在checkin数据库中,改值可以修改上传( Allows read/write access to the “properties” table in the checkin database, to change values that get uploaded)
  • android.permission.ACCESS_COARSE_LOCATION允许一个程序访问CellID或WiFi热点来获取粗略的位置(Allows an application to access coarse (e.g., Cell-ID, WiFi) location)
  • android.permission.ACCESS_FINE_LOCATION允许一个程序访问精良位置(如GPS) (Allows an application to access fine (e.g., GPS) location)
  • android.permission.ACCESS_LOCATION_EXTRA_COMMANDS允许应用程序访问额外的位置提供命令(Allows an application to access extra location provider commands)
  • android.permission.ACCESS_MOCK_LOCATION允许程序创建模拟位置提供用于测试(Allows an application to create mock location providers for testing)
  • android.permission.ACCESS_NETWORK_STATE允许程序访问有关GSM网络信息(Allows applications to access information about networks)
  • android.permission.ACCESS_SURFACE_FLINGER允许程序使用SurfaceFlinger底层特性(Allows an application to use SurfaceFlinger’s low level features)
  • android.permission.ACCESS_WIFI_STATE允许程序访问Wi-Fi网络状态信息(Allows applications to access information about Wi-Fi networks)
  • android.permission.ADD_SYSTEM_SERVICE允许程序发布系统级服务(Allows an application to publish system-level services).
  • android.permission.BATTERY_STATS允许程序更新手机电池统计信息(Allows an application to update the collected battery statistics)
  • android.permission.BLUETOOTH允许程序连接到已配对的蓝牙设备(Allows applications to connect to paired bluetooth devices)
  • android.permission.BLUETOOTH_ADMIN允许程序发现和配对蓝牙设备(Allows applications to discover and pair bluetooth devices)
  • android.permission.BRICK请求能够禁用设备(非常危险)(Required to be able to disable the device (very dangerous!).)
  • android.permission.BROADCAST_PACKAGE_REMOVED允许程序广播一个提示消息在一个应用程序包已经移除后(Allows an application to broadcast a notification that an application package has been removed)
  • android.permission.BROADCAST_STICKY允许一个程序广播常用intents(Allows an application to broadcast sticky intents)
  • android.permission.CALL_PHONE允许一个程序初始化一个电话拨号不需通过拨号用户界面需要用户确认(Allows an application to initiate a phone call without going through the Dialer user interface for the user to confirm the call being placed.)
  • android.permission.CALL_PRIVILEGED允许一个程序拨打任何号码,包含紧急号码无需通过拨号用户界面需要用户确认(Allows an application to call any phone number, including emergency numbers, without going through the Dialer user interface for the user to confirm the call being placed)
  • android.permission.CAMERA请求访问使用照相设备(Required to be able to access the camera device. )
  • android.permission.CHANGE_COMPONENT_ENABLED_STATE允许一个程序是否改变一个组件或其他的启用或禁用(Allows an application to change whether an application component (other than its own) is enabled or not. )
  • android.permission.CHANGE_CONFIGURATION允许一个程序修改当前设置,如本地化(Allows an application to modify the current configuration, such as locale. )
  • android.permission.CHANGE_NETWORK_STATE允许程序改变网络连接状态(Allows applications to change network connectivity state)
  • android.permission.CHANGE_WIFI_STATE允许程序改变Wi-Fi连接状态(Allows applications to change Wi-Fi connectivity state)
  • android.permission.CLEAR_APP_CACHE允许一个程序清除缓存从所有安装的程序在设备中(Allows an application to clear the caches of all installed applications on the device. )
  • android.permission.CLEAR_APP_USER_DATA允许一个程序清除用户设置(Allows an application to clear user data)
  • android.permission.CONTROL_LOCATION_UPDATES允许启用禁止位置更新提示从无线模块(Allows enabling/disabling location update notifications from the radio. )
  • android.permission.DELETE_CACHE_FILES允许程序删除缓存文件(Allows an application to delete cache files)
  • android.permission.DELETE_PACKAGES允许一个程序删除包(Allows an application to delete packages)
  • android.permission.DEVICE_POWER允许访问底层电源管理(Allows low-level access to power management)
  • android.permission.DIAGNOSTIC允许程序RW诊断资源(Allows applications to RW to diagnostic resources. )
  • android.permission.DISABLE_KEYGUARD允许程序禁用键盘锁(Allows applications to disable the keyguard )
  • android.permission.DUMP允许程序返回状态抓取信息从系统服务(Allows an application to retrieve state dump information from system services.)
  • android.permission.EXPAND_STATUS_BAR允许一个程序扩展收缩在状态栏,Android开发网提示应该是一个类似Windows Mobile中的托盘程序(Allows an application to expand or collapse the status bar. )
  • android.permission.FACTORY_TEST作为一个工厂测试程序,运行在root用户(Run as a manufacturer test application, running as the root user. )
  • android.permission.FLASHLIGHT访问闪光灯,Android开发网提示HTC Dream不包含闪光灯(Allows access to the flashlight )
  • android.permission.FORCE_BACK允许程序强行一个后退操作是否在顶层activities(Allows an application to force a BACK operation on whatever is the top activity. )
  • android.permission.FOTA_UPDATE暂时不了解这是做什么使用的,Android开发网分析可能是一个预留权限.
  • android.permission.GET_ACCOUNTS访问一个帐户列表在Accounts Service中(Allows access to the list of accounts in the Accounts Service)
  • android.permission.GET_PACKAGE_SIZE允许一个程序获取任何package占用空间容量(Allows an application to find out the space used by any package. )
  • android.permission.GET_TASKS允许一个程序获取信息有关当前或最近运行的任务,一个缩略的任务状态,是否活动等等(Allows an application to get information about the currently or recently running tasks: a thumbnail representation of the tasks, what activities are running in it, etc.)
  • android.permission.HARDWARE_TEST允许访问硬件(Allows access to hardware peripherals. )
  • android.permission.INJECT_EVENTS允许一个程序截获用户事件如按键、触摸、轨迹球等等到一个时间流,Android开发网提醒算是hook技术吧(Allows an application to inject user events (keys, touch, trackball) into the event stream and deliver them to ANY window.)
  • android.permission.INSTALL_PACKAGES允许一个程序安装packages(Allows an application to install packages. )
  • android.permission.INTERNAL_SYSTEM_WINDOW允许打开窗口使用系统用户界面(Allows an application to open windows that are for use by parts of the system user interface. )
  • android.permission.INTERNET允许程序打开网络套接字(Allows applications to open network sockets)
  • android.permission.MANAGE_APP_TOKENS允许程序管理(创建、催后、z-order默认向z轴推移)程序引用在窗口管理器中(Allows an application to manage (create, destroy, Z-order) application tokens in the window manager. )
  • android.permission.MASTER_CLEAR目前还没有明确的解释,Android开发网分析可能是清除一切数据,类似硬格机
  • android.permission.MODIFY_AUDIO_SETTINGS允许程序修改全局音频设置(Allows an application to modify global audio settings)
  • android.permission.MODIFY_PHONE_STATE允许修改话机状态,如电源,人机接口等(Allows modification of the telephony state - power on, mmi, etc. )
  • android.permission.MOUNT_UNMOUNT_FILESYSTEMS允许挂载和反挂载文件系统可移动存储(Allows mounting and unmounting file systems for removable storage. )
  • android.permission.PERSISTENT_ACTIVITY允许一个程序设置他的activities显示(Allow an application to make its activities persistent. )
  • android.permission.PROCESS_OUTGOING_CALLS允许程序监视、修改有关播出电话(Allows an application to monitor, modify, or abort outgoing calls)
  • android.permission.READ_CALENDAR允许程序读取用户日历数据(Allows an application to read the user’s calendar data.)
  • android.permission.READ_CONTACTS允许程序读取用户联系人数据(Allows an application to read the user’s contacts data.)
  • android.permission.READ_FRAME_BUFFER允许程序屏幕波或和更多常规的访问帧缓冲数据(Allows an application to take screen shots and more generally get access to the frame buffer data)
  • android.permission.READ_INPUT_STATE允许程序返回当前按键状态(Allows an application to retrieve the current state of keys and switches. )
  • android.permission.READ_LOGS允许程序读取底层系统日志文件(Allows an application to read the low-level system log files. )
  • android.permission.READ_OWNER_DATA允许程序读取所有者数据(Allows an application to read the owner’s data)
  • android.permission.READ_SMS允许程序读取短信息(Allows an application to read SMS messages.)
  • android.permission.READ_SYNC_SETTINGS允许程序读取同步设置(Allows applications to read the sync settings)
  • android.permission.READ_SYNC_STATS允许程序读取同步状态(Allows applications to read the sync stats)
  • android.permission.REBOOT请求能够重新启动设备(Required to be able to reboot the device. )
  • android.permission.RECEIVE_BOOT_COMPLETED允许一个程序接收到 ACTION_BOOT_COMPLETED广播在系统完成启动(Allows an application to receive the ACTION_BOOT_COMPLETED that is broadcast after the system finishes booting. )
  • android.permission.RECEIVE_MMS允许一个程序监控将收到MMS彩信,记录或处理(Allows an application to monitor incoming MMS messages, to record or perform processing on them. )
  • android.permission.RECEIVE_SMS允许程序监控一个将收到短信息,记录或处理(Allows an application to monitor incoming SMS messages, to record or perform processing on them.)
  • android.permission.RECEIVE_WAP_PUSH允许程序监控将收到WAP PUSH信息(Allows an application to monitor incoming WAP push messages. )
  • android.permission.RECORD_AUDIO允许程序录制音频(Allows an application to record audio)
  • android.permission.REORDER_TASKS允许程序改变Z轴排列任务(Allows an application to change the Z-order of tasks)
  • android.permission.RESTART_PACKAGES允许程序重新启动其他程序(Allows an application to restart other applications)
  • android.permission.SEND_SMS允许程序发送SMS短信(Allows an application to send SMS messages)
  • android.permission.SET_ACTIVITY_WATCHER允许程序监控或控制activities已经启动全局系统中Allows an application to watch and control how activities are started globally in the system.
  • android.permission.SET_ALWAYS_FINISH允许程序控制是否活动间接完成在处于后台时Allows an application to control whether activities are immediately finished when put in the background.
  • android.permission.SET_ANIMATION_SCALE修改全局信息比例(Modify the global animation scaling factor.)
  • android.permission.SET_DEBUG_APP配置一个程序用于调试(Configure an application for debugging.)
  • android.permission.SET_ORIENTATION允许底层访问设置屏幕方向和实际旋转(Allows low-level access to setting the orientation (actually rotation) of the screen.)
  • android.permission.SET_PREFERRED_APPLICATIONS允许一个程序修改列表参数PackageManager.addPackageToPreferred() 和PackageManager.removePackageFromPreferred()方法(Allows an application to modify the list of preferred applications with the PackageManager.addPackageToPreferred() and PackageManager.removePackageFromPreferred() methods.)
  • android.permission.SET_PROCESS_FOREGROUND允许程序当前运行程序强行到前台(Allows an application to force any currently running process to be in the foreground.)
  • android.permission.SET_PROCESS_LIMIT允许设置最大的运行进程数量(Allows an application to set the maximum number of (not needed) application processes that can be running. )
  • android.permission.SET_TIME_ZONE允许程序设置时间区域(Allows applications to set the system time zone)
  • android.permission.SET_WALLPAPER允许程序设置壁纸(Allows applications to set the wallpaper )
  • android.permission.SET_WALLPAPER_HINTS允许程序设置壁纸hits(Allows applications to set the wallpaper hints)
  • android.permission.SIGNAL_PERSISTENT_PROCESSES允许程序请求发送信号到所有显示的进程中(Allow an application to request that a signal be sent to all persistent processes)
  • android.permission.STATUS_BAR允许程序打开、关闭或禁用状态栏及图标Allows an application to open, close, or disable the status bar and its icons.
  • android.permission.SUBSCRIBED_FEEDS_READ允许一个程序访问订阅RSS Feed内容提供(Allows an application to allow access the subscribed feeds ContentProvider. )
  • android.permission.SUBSCRIBED_FEEDS_WRITE系统暂时保留改设置,Android开发网认为未来版本会加入该功能。
  • android.permission.SYSTEM_ALERT_WINDOW允许一个程序打开窗口使用TYPE_SYSTEM_ALERT,显示在其他所有程序的顶层(Allows an application to open windows using the type TYPE_SYSTEM_ALERT, shown on top of all other applications. )
  • android.permission.VIBRATE允许访问振动设备(Allows access to the vibrator)
  • android.permission.WAKE_LOCK允许使用PowerManager的WakeLocks保持进程在休眠时从屏幕消失( Allows using PowerManager WakeLocks to keep processor from sleeping or screen from dimming)
  • android.permission.WRITE_APN_SETTINGS允许程序写入API设置(Allows applications to write the apn settings)
  • android.permission.WRITE_CALENDAR允许一个程序写入但不读取用户日历数据(Allows an application to write (but not read) the user’s calendar data. )
  • android.permission.WRITE_CONTACTS允许程序写入但不读取用户联系人数据(Allows an application to write (but not read) the user’s contacts data. )
  • android.permission.WRITE_GSERVICES允许程序修改Google服务地图(Allows an application to modify the Google service map. )
  • android.permission.WRITE_OWNER_DATA允许一个程序写入但不读取所有者数据(Allows an application to write (but not read) the owner’s data.)
  • android.permission.WRITE_SETTINGS允许程序读取或写入系统设置(Allows an application to read or write the system settings. )
  • android.permission.WRITE_SMS允许程序写短信(Allows an application to write SMS messages)
  • android.permission.WRITE_SYNC_SETTINGS允许程序写入同步设置(Allows applications to write the sync settings)

四 Android 动态申请权限DEMO

在清单文件(AndroidManifest.xml)中声明所需的危险权限。例如,如果应用程序需要访问相机和存储权限,可以添加以下权限声明:

代码语言:javascript
复制
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

在需要使用权限的地方,检查权限状态并请求权限。使用以下代码进行权限检查:

代码语言:javascript
复制
// 检查相机权限是否已授予
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
    // 如果权限未被授予,向用户发起权限请求
    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, CAMERA_PERMISSION_REQUEST_CODE);
} else {
    // 权限已授予,执行相关操作
    openCamera();
}

处理权限请求的回调结果。当用户对权限请求做出响应后,会调用活动(Activity)的onRequestPermissionsResult()方法。在该方法中,您可以检查每个权限的授权状态并采取相应的操作:

代码语言:javascript
复制
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    if (requestCode == CAMERA_PERMISSION_REQUEST_CODE) {
        // 检查相机权限是否被授予
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            // 相机权限已授予,执行相关操作
            openCamera();
        } else {
            // 相机权限被拒绝,处理未授予权限的情况
            Toast.makeText(this, "未授予相机权限", Toast.LENGTH_SHORT).show();
        }
    }
}

五 Android 主流权限框架

1.soulPermission

一句话搞定权限,亲测确实效果不错

github GitHub - soulqw/SoulPermission: a permission check or request for android

转载的博客地址:SoulPermission-Android一句话权限适配的更优解决方案_再见孙悟空的专栏-CSDN博客_soulpermission

2.RxPermissions

基于RxJava完成权限申请的框架

github https://github.com/tbruyelle/RxPermissions

3.easyPermission

Easypermissions简化了Android M的运行时权限的申请、结果处理

github https://github.com/googlesamples/easypermissions

4.AndPermission

AndPermission 算是一个不错的 权限库了,链式调用,还可以使用 content 上下文去请求权限

github https://github.com/yanzhenjie/AndPermission

5.Android-UsesPermission

Github:https://github.com/xiangyuecn/Android-UsesPermission

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-07-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 一.Android 数据权限基本介绍
  • 二 Android 权限分类
  • 三 Android 权限清单
  • 四 Android 动态申请权限DEMO
  • 五 Android 主流权限框架
相关产品与服务
短信
腾讯云短信(Short Message Service,SMS)可为广大企业级用户提供稳定可靠,安全合规的短信触达服务。用户可快速接入,调用 API / SDK 或者通过控制台即可发送,支持发送验证码、通知类短信和营销短信。国内验证短信秒级触达,99%到达率;国际/港澳台短信覆盖全球200+国家/地区,全球多服务站点,稳定可靠。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档