在Android 6.0以前,我们安装App时会列出安装的App的访问权限,而且只有安装时才会出现 一次,一旦我们同意了并安装App,这个App就可以在用户不知道的情况下访问权限内的所有东西,所以Google在Android6.0时增加了权限运行时询问用户授权权限,Google将权限分为Normal Permission和Dangerous Permission
Normal Permission
- ACCESS_LOCATION_EXTRA_COMMANDS
- ACCESS_NETWORK_STATE
- ACCESS_NOTIFICATION_POLICY
- ACCESS_WIFI_STATE
- BLUETOOTH
- BLUETOOTH_ADMIN
- BROADCAST_STICKY
- CHANGE_NETWORK_STATE
- CHANGE_WIFI_MULTICAST_STATE
- CHANGE_WIFI_STATE
- DISABLE_KEYGUARD
- EXPAND_STATUS_BAR
- GET_PACKAGE_SIZE
- INSTALL_SHORTCUT
- INTERNET
- KILL_BACKGROUND_PROCESSES
- MODIFY_AUDIO_SETTINGS
- NFC
- READ_SYNC_SETTINGS
- READ_SYNC_STATS
- RECEIVE_BOOT_COMPLETED
- REORDER_TASKS
- REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
- REQUEST_INSTALL_PACKAGES
- SET_ALARM
- SET_TIME_ZONE
- SET_WALLPAPER
- SET_WALLPAPER_HINTS
- TRANSMIT_IR
- UNINSTALL_SHORTCUT
- USE_FINGERPRINT
- VIBRATE
- WAKE_LOCK
- WRITE_SYNC_SETTINGS
Dangerous Permission
Dangerous Permission 是以分组的形式给出
- CALENDAR(日历)
READ_CALENDAR
WRITE_CALENDAR- CAMERA(相机)
CAMERA
CONTACTS(联系人)
READ_CONTACTS
WRITE_CONTACTS
GET_ACCOUNTS- LOCATION(位置)
ACCESS_FINE_LOCATION
ACCESS_COARSE_LOCATION- MICROPHONE(麦克风)
RECORD_AUDIO- PHONE(手机)
READ_PHONE_STATE
CALL_PHONE
READ_CALL_LOG
WRITE_CALL_LOG
ADD_VOICEMAIL
USE_SIP
PROCESS_OUTGOING_CALLS- SENSORS(传感器)
BODY_SENSORS- SMS(短信)
SEND_SMS
RECEIVE_SMS
READ_SMS
RECEIVE_WAP_PUSH
RECEIVE_MMS- STORAGE(存储卡)
READ_EXTERNAL_STORAGE
WRITE_EXTERNAL_STORAGE
举个栗子
public class MainActivity extends AppCompatActivity {
private static final int PERMOSSION_REQUEST_PHONE = 1;
private Button btn_call;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_call = findViewById(R.id.button);
btn_call.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
call();
}
});
}
public void call() {
//检查App是否有permission.CALL_PHONE 的权限
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
//如果没有permission.CALL_PHONE 的权限,就申请权限
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission_group.PHONE}, PERMOSSION_REQUEST_PHONE);
} else {
callPhone();
}
}
public void callPhone() {
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + "10086"));
try {
startActivity(intent);
} catch (SecurityException e) {
e.printStackTrace();
}
}
//申请权限的回调
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (PERMOSSION_REQUEST_PHONE == requestCode) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
callPhone();
} else {
if (!ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CALL_PHONE)) {
AlertDialog alertDialog = new AlertDialog.Builder(this)
.setMessage("该功能需要访问电话权限,不开启将无法正常使用!")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).create();
alertDialog.show();
}
Toast.makeText(this, "权限被拒绝", Toast.LENGTH_SHORT).show();
}
return;
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
call方法首先判断当前App是否有permission.CALL_PHONE权限,如果有直接调用callPhone方法打电话,如果没有该权限,则弹出提示框进行申请权限,onRequestPermissionsResult就是申请权限的回调,如果用户允许,则调用callPhone方法;如果用户拒绝。就弹出Toast实现权限被拒绝,需要注意的是:如果我们选择允许下一次就不会弹出申请权限对话框了;如果我们拒绝,则下一次还会演出弹出提示框,只不过会多出一个“不再询问”选择按钮,如果我们选择该选项,下一次就不会弹出对话框了,而是直接调用onRequestPermissionsResult方法,所以需要ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CALL_PHONE),来解析权限的情况;
最后建议使用已经封装好的库PermissionDispatcher
使用教程官方讲的很详细也很简单:
//必需的注解,它用来注册一个Activity或Fragment,使用方法他们可以处理权限
@RuntimePermissions
public class MainActivity extends AppCompatActivity {
private static final int PERMOSSION_REQUEST_PHONE = 1;
private Button btn_call;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_call = findViewById(R.id.button);
btn_call.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
call();
}
});
}
public void call() {
callPhone();
}
@NeedsPermission(android.Manifest.permission.CALL_PHONE)
//在需要权限的地方注释
public void callPhone() {
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + "10086"));
try {
startActivity(intent);
} catch (SecurityException e) {
e.printStackTrace();
}
}
//提示用户为何开启此权限
@OnShowRationale(android.Manifest.permission.CALL_PHONE)
public void showWhy(final PermissionRequest request) {
android.app.AlertDialog alertDialog = new android.app.AlertDialog.Builder(this)
.setMessage("该功能需要访问电话权限,不开启将无法正常使用!")
.setPositiveButton("知道了", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
request.proceed();//再次请求
}
}).create();
alertDialog.show();
}
//用户选择拒绝的提示
@OnPermissionDenied(android.Manifest.permission.CALL_PHONE)
public void showDenied() {
Toast.makeText(this, "用户选择拒绝的提示", Toast.LENGTH_SHORT).show();
}
@OnNeverAskAgain(android.Manifest.permission.CALL_PHONE)
void showNotAsk() {
android.app.AlertDialog alertDialog = new android.app.AlertDialog.Builder(this)
.setMessage("该功能需要访问电话权限,不开启将无法正常使用!")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).create();
alertDialog.show();
}
}
最后生成XXXPermissionDispatcher,所有工作都交给它完成就好了;
其实第三方封装权限处理的库,大家有兴趣可以分享;
网友评论