<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.CALL_PHONE" />
//对应的类 下面代码放在MainActivity类里,根据情况而定
public final static int REQ_PERMISSION_CODE = 0x1000;
// 申请动态权限 checkPermission();
/** 动态权限申请 */ private boolean checkPermission() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { List<String> permissions = new ArrayList<>(); if (PackageManager.PERMISSION_GRANTED != ActivityCompat.checkSelfPermission(LoginMainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) { permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE); } if (PackageManager.PERMISSION_GRANTED != ActivityCompat.checkSelfPermission(LoginMainActivity.this, Manifest.permission.CAMERA)) { permissions.add(Manifest.permission.CAMERA); } if (PackageManager.PERMISSION_GRANTED != ActivityCompat.checkSelfPermission(LoginMainActivity.this, Manifest.permission.RECORD_AUDIO)) { permissions.add(Manifest.permission.RECORD_AUDIO); } if (PackageManager.PERMISSION_GRANTED != ActivityCompat.checkSelfPermission(LoginMainActivity.this, Manifest.permission.READ_PHONE_STATE)) { permissions.add(Manifest.permission.READ_PHONE_STATE); } if (PackageManager.PERMISSION_GRANTED != ActivityCompat.checkSelfPermission(LoginMainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE)) { permissions.add(Manifest.permission.READ_EXTERNAL_STORAGE); } if (PackageManager.PERMISSION_GRANTED != ActivityCompat.checkSelfPermission(LoginMainActivity.this, Manifest.permission.CALL_PHONE)) { permissions.add(Manifest.permission.CALL_PHONE); } if (permissions.size() != 0) { ActivityCompat.requestPermissions(this,permissions.toArray(new String[0]), REQ_PERMISSION_CODE); return false; } } return true; } @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); switch (requestCode) { case REQ_PERMISSION_CODE: for (int ret : grantResults) { if (PackageManager.PERMISSION_GRANTED != ret) { //DlgMgr.showMsg(AppLauncher.this, "用户没有允许需要的权限,使用可能会受到限制!"); Toast.makeText(LoginMainActivity.this,"用户没有允许需要的权限,使用可能会受到限制!" ,Toast.LENGTH_LONG).show(); } } break; default: break; } }