申请权限对于安卓开发很重要,从Android6.0开始,Android系统提供动态申请权限的机制, APP在使用危险权限时,需要用户的授权才可进一步操作。所以这一篇文章介绍如何动态申请权限。非常简单。
一、在AndroidManifest.xml配置所需要的权限,代码如下
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.camera"> <!--需要配置的权限--> <uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <application android: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"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
二、新建一个项目,在MainActivity.java中加入动态申请权限的代码,代码如下
1 package com.example.camera; 2 3 import androidx.appcompat.app.AppCompatActivity; 4 import androidx.core.app.ActivityCompat; 5 import androidx.core.content.ContextCompat; 6 7 import android.Manifest; 8 import android.content.pm.PackageManager; 9 import android.os.Bundle; 10 import android.view.Window; 11 import android.view.WindowManager; 12 13 public class MainActivity extends AppCompatActivity { 14 private static final String TAG = "MainActivity"; 15 16 @Override 17 protected void onCreate(Bundle savedInstanceState) { 18 super.onCreate(savedInstanceState); 19 20 //去除顶部状态栏 21 requestWindowFeature(Window.FEATURE_NO_TITLE); 22 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); 23 setContentView(R.layout.activity_main); 24 // Android 6.0相机动态权限检查 25 if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)== PackageManager.PERMISSION_GRANTED){ 26 }else { 27 ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.CAMERA,Manifest.permission.WRITE_EXTERNAL_STORAGE},1); 28 } 29 30 } 31 }
其中requestPermissions的requestcode为1,作为后面的判断用。
第二种方法,新建一个权限的数组,代码如下:
1 public class MainActivity extends AppCompatActivity { 2 private static final String TAG = "MainActivity"; 3 4 private static final String[] permission = new String[] { 5 Manifest.permission.CAMERA, 6 Manifest.permission.WRITE_EXTERNAL_STORAGE 7 }; 8 9 @Override 10 protected void onCreate(Bundle savedInstanceState) { 11 super.onCreate(savedInstanceState); 12 13 //去除顶部状态栏 14 requestWindowFeature(Window.FEATURE_NO_TITLE); 15 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); 16 setContentView(R.layout.activity_main); 17 // Android 6.0相机动态权限检查 18 if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)== PackageManager.PERMISSION_GRANTED){ 19 }else { 20 ActivityCompat.requestPermissions(this,permission,1); 21 } 22 23 } 24 }