依赖库:
/**********Easypermissions简化了Android M的运行时权限的申请、结果处理、判断等步骤***********/
compile 'pub.devrel:easypermissions:0.2.0'
/**********Easypermissions简化了Android M的运行时权限的申请、结果处理、判断等步骤***********/
public class RuntimePermissionsActivity extends BaseNewActivity implements EasyPermissions.PermissionCallbacks {
@Override
protected int getLayoutResID() {
return 0;
}
@Override
protected void initViews() {
}
@Override
protected void initData() {
requestPermisions();
}
private static final int REQUEST_CODE_CAMERA = 1;
/**
* 第一步 申请
*/
private void requestPermisions() {
String camera = Manifest.permission.CAMERA;
if (EasyPermissions.hasPermissions(this, camera)) {
} else {
// Ask for one permission
EasyPermissions.requestPermissions(this, "需要请求运行时权限",
REQUEST_CODE_CAMERA, camera);
}
}
/**
* 第二步 申请结果回调
*/
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {//安卓api
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
handleResult(requestCode, permissions, grantResults);
}
/**
* 第三步 申请结果处理
*/
private void handleResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
}
/**
* 第四步 申请成功回调
*/
@Override
public void onPermissionsGranted(int requestCode, List
perms) {//EasyPermissions框架的api
switch (requestCode) {
case REQUEST_CODE_CAMERA:
//相机权限申请成功
break;
}
}
/**
* 第五步 申请失败回调
*/
@Override
public void onPermissionsDenied(int requestCode, List perms) {//EasyPermissions框架的api
switch (requestCode) {
case REQUEST_CODE_CAMERA:
//相机权限申请失败,弹出跳转系统权限设置界面
new AppSettingsDialog.Builder(this, "当前App需要申请相机权限,需要打开设置页面么?")
.setTitle("权限申请")
.setPositiveButton("确认")
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
} /* click listener */)
.setRequestCode(REQUEST_PERMS)
.build()
.show();
break;
}
}
/**
Normal Permissions如下
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_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 Permissions:
group:android.permission-group.CONTACTS
permission:android.permission.WRITE_CONTACTS
permission:android.permission.GET_ACCOUNTS
permission:android.permission.READ_CONTACTS
group:android.permission-group.PHONE
permission:android.permission.READ_CALL_LOG
permission:android.permission.READ_PHONE_STATE
permission:android.permission.CALL_PHONE
permission:android.permission.WRITE_CALL_LOG
permission:android.permission.USE_SIP
permission:android.permission.PROCESS_OUTGOING_CALLS
permission:com.android.voicemail.permission.ADD_VOICEMAIL
group:android.permission-group.CALENDAR
permission:android.permission.READ_CALENDAR
permission:android.permission.WRITE_CALENDAR
group:android.permission-group.CAMERA
permission:android.permission.CAMERA
group:android.permission-group.SENSORS
permission:android.permission.BODY_SENSORS
group:android.permission-group.LOCATION
permission:android.permission.ACCESS_FINE_LOCATION
permission:android.permission.ACCESS_COARSE_LOCATION
group:android.permission-group.STORAGE
permission:android.permission.READ_EXTERNAL_STORAGE
permission:android.permission.WRITE_EXTERNAL_STORAGE
group:android.permission-group.MICROPHONE
permission:android.permission.RECORD_AUDIO
group:android.permission-group.SMS
permission:android.permission.READ_SMS
permission:android.permission.RECEIVE_WAP_PUSH
permission:android.permission.RECEIVE_MMS
permission:android.permission.RECEIVE_SMS
permission:android.permission.SEND_SMS
permission:android.permission.READ_CELL_BROADCASTS
*/
}