android 权限 permission

编程入门 行业动态 更新时间:2024-10-14 02:21:14

android <a href=https://www.elefans.com/category/jswz/34/1771295.html style=权限 permission"/>

android 权限 permission

android 权限 permission

Declares a security permission that can be used to limit access to specific components or features of this or other applications. See the Permissions section in the introduction, and the Security and Permissions document for more information on how permissions work.

声明一个用来限制此应用或其他应用访问特殊组件或功能的安全权限。请参阅简介中的Permissions部分以及安全与权限文档,了解更多权限如何工作的信息。

权限等级:

从Android 6.0 (API级别23)开始,用户可以在运行时统一或拒绝某些应用权限。但是,无论您的应用支持哪个Android版本,您都必须使用清单中元素声明所有版本权限请求。授予应用权限后,该应用便能使用受保护的功能。否则,该应用在尝试访问这些功能时会失败。

请求应用权限

向清单添加权限

对于所有Android版本,要声明应用需要某项权限,请在应用清单中添加元素,作为顶级元素的子项。例如网络访问权限:

    <manifest xmlns:android=""package="com.example.snazzyapp"><uses-permission android:name="android.permission.INTERNET"/><!-- other permissions go here --><application ...>...</application></manifest>

检查权限

应用需要一项危险权限,那么每次执行需要该权限的操作时必须检查自己是否具有该权限。Android6.0开始用于可以随时取消权限,因此每次使用功能时都需要请求权限

权限检查:调用ContextCompat.checkSelfPermission()方法。例如是否可以向日历写入数据:

if (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.WRITE_CALENDAR)!= PackageManager.PERMISSION_GRANTED) {// Permission is not granted}

请求权限

当您的应用从checkSelfPermission收到PERMISSION_DENIED时需要提示用户授予该权限。Android提供了几种方法来请求权限

使用requestPermission()方法来请求权限,例如检查读取用户联系人权限没有权限则请求:

// Here, thisActivity is the current activityif (ContextCompat.checkSelfPermission(thisActivity,Manifest.permission.READ_CONTACTS)!= PackageManager.PERMISSION_GRANTED) {// Permission is not granted// Should we show an explanation?if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,Manifest.permission.READ_CONTACTS)) {// Show an explanation to the user *asynchronously* -- don't block// this thread waiting for the user's response! After the user// sees the explanation, try again to request the permission.} else {// No explanation needed; request the permissionActivityCompat.requestPermissions(thisActivity,new String[]{Manifest.permission.READ_CONTACTS},MY_PERMISSIONS_REQUEST_READ_CONTACTS);// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an// app-defined int constant. The callback method gets the// result of the request.}} else {// Permission has already been granted}

处理权限请求响应

当用户响应您应用的权限请求时,系统会调用应用的onRequestPermissionResult()方法,在调用过程中向其传递用户响应。您的应用必须替换该方法以查明是否授予响应权限。在回调过程中传递的请求代码与传递给requestPermissions()的请求代码相同。例如,如果应用请求READ_CONTACTS访问权限,则它可能采用以下回调方法:

    @Overridepublic void onRequestPermissionsResult(int requestCode,String[] permissions, int[] grantResults) {switch (requestCode) {case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {// If request is cancelled, the result arrays are empty.if (grantResults.length > 0&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {// permission was granted, yay! Do the// contacts-related task you need to do.} else {// permission denied, boo! Disable the// functionality that depends on this permission.}return;}// other 'case' lines to check for other// permissions this app might request.}}

应用权限最佳做法

  1. 遵循以下原则:
  2. 仅使用应用正常工作所需的权限
  3. 注意库所需的权限
  4. 公开透明
  5. 让系统以显式方式询问

自己的实现

写一个请求权限的接口

public interface RequestPermission1 {void requestPermission(Activity activity);
}

继承实现

/*** Description:* Data:2018/10/14-15:03* Author: satsuki*/
public class WriteExternalStorage implements RequestPermission1 {@Overridepublic void requestPermission(Activity activity) {//检查这个权限是否已经获取int checkWriteExternalStoragePermission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);if(checkWriteExternalStoragePermission!= PackageManager.PERMISSION_GRANTED){//如果没有权限则获取权限 requestCode在后面回调中会用到ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},3);Log.e("没有权限,","请求权限");}}
}

在activity中重写onRequestPermissionResult方法处理权限请求响应

@Override
public void onRequestPermissionsResult(int requestCode,String permissions[], int[] grantResults) {switch (requestCode) {case 3:if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {} else {Toast.makeText(this, "权限被拒绝了", Toast.LENGTH_LONG).show();}break;default:break;}
}

权限详情参照:=zh-cn

android 9.0 只允许https安全加密的http请求如果想要使用明文http请求做如下处理:

在res目录下创建xml目录并添加network_security_config.xml文件内容如下;

<?xml version="1.0" encoding="utf-8"?>
<network-security-config><base-config cleartextTrafficPermitted="true"><trust-anchors><certificates src="system" overridePins="true" /><certificates src="user" overridePins="true" /></trust-anchors></base-config>
</network-security-config>

然后在AndroidManifest.xml的标签中添加:

android:networkSecurityConfig="@xml/network_security_config"
<!--具体如下: -->
<applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"android:theme="@style/AppTheme"android:networkSecurityConfig="@xml/network_security_config">

更多推荐

android 权限 permission

本文发布于:2024-03-05 19:43:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1713229.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:权限   android   permission

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!