Android

Android 权限申请的简单实现

2023-03-01  本文已影响0人  笔头还没烂
  1. 先注册。先在 module 下的 src/main/AndroidManifest.xml 注册要申请的权限。举个例子,比如我们要申请写入外部存储的权限 android.permission.WRITE_EXTERNAL_STORAGE,AndroidManifest.xml 权限注册的代码如下所示:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyApplication"
        tools:targetApi="31">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <meta-data
                android:name="android.app.lib_name"
                android:value="" />
        </activity>
    </application>
</manifest>
  1. 申请权限以及一些细节的处理(比如如果用户拒绝应用获取该权限、下次是否还需继续询问等,如果只需询问一次,则按以下代码处理即可)申请权限的代码如下所示:
package com.example.myapplication;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.core.content.PackageManagerCompat;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private static final int MY_PERMISSIONS_READ_SMS_REQ_CODE = 1;
    private static final String TAG = "Hello";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        requestPermission();
    }

    public void requestPermission() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_SMS) != PackageManager.PERMISSION_GRANTED) {
            //检查到未授权该权限信息
            //Should we show an explanation?
            if (ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.READ_SMS)) {
                //上一次用户拒绝了开启此权限!
                Log.e(TAG, "requestPermission: 上一次用户拒绝了开启此权限");
                //权限申请完成,回调
            }else {
                Log.e(TAG, "requestPermission: 开始申请权限");
                ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_SMS},MY_PERMISSIONS_READ_SMS_REQ_CODE);
            }
        }else {
            Log.e(TAG, "requestPermission: 用户已经同意过授权");
            //权限申请完成,回调
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case MY_PERMISSIONS_READ_SMS_REQ_CODE: {
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Toast.makeText(this,"申请成功",Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(this,"申请失败",Toast.LENGTH_SHORT).show();
                }
                //权限申请完成,回调
                return;
            }
        }
    }
}

以上,感谢阅读!

上一篇下一篇

猜你喜欢

热点阅读