欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android6.0以上添加原生动态权限

程序员文章站 2024-03-16 23:05:28
...

首先AndroidMenifest.xml里面需要添加权限申请:

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

build.gradle里面添加依赖:

implementation 'androidx.appcompat:appcompat:1.1.0'

Activity里面添加动态权限申请代码:

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;

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

import static android.content.pm.PackageManager.PERMISSION_DENIED;
import static com.example.chinaweather.WeatherApp.TAG;

public class MainActivity extends AppCompatActivity {
    private static final int REQUEST_CODE = 1001;
    private static final int REQUEST_CODE_SYSTEM_ALERT = 1002;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        if(checkAppPemisstionDenied()){
            Log.d(TAG,"requestPermissions ");
            ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.INTERNET,
                    Manifest.permission.SYSTEM_ALERT_WINDOW,Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE},REQUEST_CODE);
        }else {
            Log.d(TAG,"no need to requestPermissions ");
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        Log.d(TAG,"onRequestPermissionsResult "+permissions[0]+" "+grantResults[0]);
        Log.d(TAG,"onRequestPermissionsResult "+permissions[1]+" "+grantResults[1]);
        if(!hasSystemAlertPermission()){
            Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                    Uri.parse("package:" + getPackageName()));
            startActivityForResult(intent, REQUEST_CODE_SYSTEM_ALERT);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(data==null){
            Log.d(TAG,"onActivityResult SYSTEM_ALERT requestCode "+requestCode+" resultCode "+resultCode);
            return;
        }
        Log.d(TAG,"onActivityResult SYSTEM_ALERT requestCode "+requestCode+" resultCode "+resultCode+" intent "+data.toString());

    }

    private boolean checkAppPemisstionDenied() {
        return ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.INTERNET)==PERMISSION_DENIED ||
                ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.SYSTEM_ALERT_WINDOW)==PERMISSION_DENIED||
                ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE)==PERMISSION_DENIED||
        ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)==PERMISSION_DENIED;
    }

    private boolean hasSystemAlertPermission(){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            return Settings.canDrawOverlays(this);
        }
        return true;
    }
}

 

相关标签: Android6.0动态权限