4.5

所花时间(包括上课):3

打码量(行):300

博客量(篇):1

了解到知识点:学习运行时动态申请权限Hungry模式

 

 import android.Manifest;

import android.content.pm.PackageManager;

import android.os.Bundle;

import android.util.Log;

import androidx.annotation.NonNull;

import androidx.appcompat.app.AppCompatActivity;

import androidx.core.app.ActivityCompat;

import androidx.core.content.ContextCompat;

 

public class MainActivity extends AppCompatActivity {

 

    private static final String TAG = "MainActivity";

    private static final int CAMERA_PERMISSION_REQUEST_CODE = 100;

    private static final int STORAGE_PERMISSION_REQUEST_CODE = 101;

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

 

        // 示例:请求相机和存储权限

        requestCameraPermission();

        requestStoragePermission();

    }

 

    private void requestCameraPermission() {

        // 请求相机权限

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)

                != PackageManager.PERMISSION_GRANTED) {

            ActivityCompat.requestPermissions(this,

                    new String[]{Manifest.permission.CAMERA},

                    CAMERA_PERMISSION_REQUEST_CODE);

        } else {

            // 已授予相机权限,执行相应操作

            Log.d(TAG, "Camera permission granted.");

            openCamera();

        }

    }

 

    private void requestStoragePermission() {

        // 请求存储权限

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)

                != PackageManager.PERMISSION_GRANTED) {

            ActivityCompat.requestPermissions(this,

                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},

                    STORAGE_PERMISSION_REQUEST_CODE);

        } else {

            // 已授予存储权限,执行相应操作

            Log.d(TAG, "Storage permission granted.");

            performStorageOperations();

        }

    }

 

    private void openCamera() {

        // 打开相机,执行相机相关操作

        Log.d(TAG, "Opening camera...");

        // 在这里可以执行需要相机权限的操作

    }

 

    private void performStorageOperations() {

        // 执行存储操作

        Log.d(TAG, "Performing storage operations...");

        // 在这里可以执行需要存储权限的操作

    }

 

    @Override

    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

        switch (requestCode) {

            case CAMERA_PERMISSION_REQUEST_CODE:

                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    // 用户授予了相机权限,执行相应操作

                    Log.d(TAG, "Camera permission granted after request.");

                    openCamera();

                } else {

                    // 用户拒绝了相机权限,可以给出提示或者进行其他处理

                    Log.d(TAG, "Camera permission denied.");

                }

                break;

            case STORAGE_PERMISSION_REQUEST_CODE:

                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    // 用户授予了存储权限,执行相应操作

                    Log.d(TAG, "Storage permission granted after request.");

                    performStorageOperations();

                } else {

                    // 用户拒绝了存储权限,可以给出提示或者进行其他处理

                    Log.d(TAG, "Storage permission denied.");

                }

                break;

            // 可以添加其他权限的处理逻辑

        }

    }

}

posted @ 2024-04-05 19:00  赵千万  阅读(8)  评论(0编辑  收藏  举报