Android6.0动态申请权限

  • 2017-8-16 08:34
  • PHP二次开发
  • 阅读(138)
  • 评论(0)
  • 来源:互联网
  • 摘要: Permission.javapublic class Permision { private static Context context; public Permision(Context context) { this.context = context; } private static final int REQUEST_EXTERNAL_STORAGE = 1; private sta ...
    Permission.java
    public class Permision {

    private static Context context;

    public Permision(Context context) {
    this.context = context;
    }

    private static final int REQUEST_EXTERNAL_STORAGE = 1;
    private static String[] PERMISSIONS_STORAGE = {
    "android.permission.READ_EXTERNAL_STORAGE",
    "android.permission.WRITE_EXTERNAL_STORAGE"};

    public void verifyStoragePermissions(Activity activity) {

    try {
    //检测是否有写的权限
    int permission = ActivityCompat.checkSelfPermission(activity,
    "android.permission.WRITE_EXTERNAL_STORAGE");
    if (permission != PackageManager.PERMISSION_GRANTED) {
    //已经禁止提示了
    if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
    Toast.makeText(context, "您已禁止SD卡读写权限,需要重新开启。", Toast.LENGTH_SHORT).show();
    } else {
    // 没有写的权限,去申请写的权限,会弹出对话框
    ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE);
    }

    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }

    应用加入判断系统版本
    //判断6.0以上,动态获取权限
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    Permision permission = new Permision(getApplicationContext());
    permission.verifyStoragePermissions(this);
    }


    PHP技术交流QQ群:422137578

    除非注明,文章均为 PHP二次开发 原创,转载请注明本文地址:http://www.php2.cc/article-2635-1.html

    相关阅读

    最新评论

    用户名:  *

    邮 箱:  *

    网 址: 注意加上“http://”哦!