其他分享
首页 > 其他分享> > android studio设置动态权限

android studio设置动态权限

作者:互联网

1.关键代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.exampleglideok">
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_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>

MainActivivty:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //判断是否有读取权限
        havePermission();
        initView();
    }
//判断权限
    private void havePermission() {
        if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED||
                ActivityCompat.checkSelfPermission(MainActivity.this,Manifest.permission.READ_EXTERNAL_STORAGE)
                        !=PackageManager.PERMISSION_GRANTED){
            //申请一个或多个权限,并提供用于回调的获取码
            ActivityCompat.requestPermissions(MainActivity.this,new String[]{
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.READ_EXTERNAL_STORAGE
            },100);
        }
    }
    private static final String TAG = "MainActivity";
    /**
     * 权限请求结果
     * @param requestCode
     * @param permissions
     * @param grantResults
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        //TODO:根据请求结果来做事
        Log.d(TAG, "onRequestPermissionsResult: requestcode"+requestCode
                +"\n"+permissions.toString()
                +"\n"+grantResults[0]
        );
        if (grantResults[0]!=0){
            Toast.makeText(this, "要开启权限才能用软件哦,不然无法让您更好的体验", Toast.LENGTH_SHORT).show();
            this.finish();
        }
    }

2.代码讲解与步骤:

1.在AndroidManifest.xml中设置权限

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

2.检查是否有权限

if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED||
                ActivityCompat.checkSelfPermission(MainActivity.this,Manifest.permission.READ_EXTERNAL_STORAGE)
                        !=PackageManager.PERMISSION_GRANTED){
            //申请一个或多个权限,并提供用于回调的获取码
            ActivityCompat.requestPermissions(MainActivity.this,new String[]{
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.READ_EXTERNAL_STORAGE
            },100);
        }

3.申请权限返回结果(可以根据返回权限结果来进行操作)

/**
     * 权限请求结果
     * @param requestCode
     * @param permissions
     * @param grantResults
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        //TODO:根据请求结果来做事
        Log.d(TAG, "onRequestPermissionsResult: requestcode"+requestCode
                +"\n"+permissions.toString()
                +"\n"+grantResults[0]
        );
        if (grantResults[0]!=0){
            Toast.makeText(this, "要开启权限才能用软件哦,不然无法让您更好的体验", Toast.LENGTH_SHORT).show();
            this.finish();
        }
    }

标签:permission,grantResults,requestCode,studio,EXTERNAL,android,权限,permissions
来源: https://blog.csdn.net/sunweihao2019/article/details/111482758