Android 权限动态请求
Posted 长江很多号
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 权限动态请求相关的知识,希望对你有一定的参考价值。
在android M版本以后,权限就没办法Manifest声明就万事了,还需要动态请求。
网上已经有不少例子,不过这里还是做一个简单的activity,把动态请求的方法,完整的实现一下,基本可以复制粘贴使用。
首先,manifest还是声明一下,兼容老版本。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
然后,写一个WelcomeActivity,把权限拿到,再进入下一步。
import static android.Manifest.permission.READ_EXTERNAL_STORAGE;
import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import com.google.android.material.snackbar.Snackbar;
public class WelcomeActivity extends AppCompatActivity implements View.OnClickListener
private static final int PERMISSION_REQUEST_CODE = 200;
private View view;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
Button check_permission = (Button) findViewById(R.id.check_permission);
Button request_permission = (Button) findViewById(R.id.request_permission);
check_permission.setOnClickListener(this);
request_permission.setOnClickListener(this);
@Override
public void onClick(View v)
view = v;
int id = v.getId();
switch (id)
case R.id.check_permission:
if (checkPermission())
Snackbar.make(view, "Permission already granted.", Snackbar.LENGTH_LONG).show();
else
Snackbar.make(view, "Please request permission.", Snackbar.LENGTH_LONG).show();
break;
case R.id.request_permission:
if (!checkPermission())
requestPermission();
else
Snackbar.make(view, "Permission already granted.", Snackbar.LENGTH_LONG).show();
goNext();
break;
private boolean checkPermission()
int result = ContextCompat.checkSelfPermission(getApplicationContext(), READ_EXTERNAL_STORAGE);
int result1 = ContextCompat.checkSelfPermission(getApplicationContext(), WRITE_EXTERNAL_STORAGE);
return result == PackageManager.PERMISSION_GRANTED && result1 == PackageManager.PERMISSION_GRANTED;
private void requestPermission()
ActivityCompat.requestPermissions(this, new String[]READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE, PERMISSION_REQUEST_CODE);
private void goNext()
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults)
switch (requestCode)
case PERMISSION_REQUEST_CODE:
if (grantResults.length > 0)
boolean readAccepted = grantResults[0] == PackageManager.PERMISSION_GRANTED;
boolean writeAccepted = grantResults[1] == PackageManager.PERMISSION_GRANTED;
if (readAccepted && writeAccepted)
Snackbar.make(view, "Permission Granted", Snackbar.LENGTH_LONG).show();
goNext();
else
Snackbar.make(view, "Permission Denied", Snackbar.LENGTH_LONG).show();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
if (shouldShowRequestPermissionRationale(WRITE_EXTERNAL_STORAGE))
showMessageOKCancel("You need to allow access to both the permissions",
new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
requestPermissions(new String[]READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE,
PERMISSION_REQUEST_CODE);
);
return;
break;
private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener)
new AlertDialog.Builder(this)
.setMessage(message)
.setPositiveButton("OK", okListener)
.setNegativeButton("Cancel", null)
.create()
.show();
上面的例子,提到了存储权限,这里就顺带说一个注意事项。
如果你的gradle的targetSDK > 29,则需要在manifest的application标签,加
android:requestLegacyExternalStorage = "true"
原因是,从Android Q开始,出于数据隐私问题,Android 希望禁止应用程序操作非沙箱内的数据(app私有存储目录,例如QQ的外部存储目录,/sdcard/Android/data/com.tencent.qq/,为沙箱数据,可以随意读写)。 但是为了过度,Android提供了requestLegacyExternalStorage机制,来帮助应用使用原来的机制继续读写存储卡。
以上是关于Android 权限动态请求的主要内容,如果未能解决你的问题,请参考以下文章