转载请注明出处 http://blog.csdn.net/pyfysf/article/details/78204395
Android 6.0(API 级别 23)开始,在应用运行时向其授予权限,而不是在应用安装时授予。这样可以让用户对应用的功能进行更多控制;
系统权限分为两类:正常权限和危险权限:
正常权限不会直接给用户隐私权带来风险。如果你的应用在其清单中列出了正常权限,系统将自动授予该权限。
危险权限会授予应用访问用户隐私数据的权限。如果你列出了危险权限,则用户必须明确批准您的应用使用这些权限。
注:从 Android 6.0(API 级别 23)开始,用户可以随时从任意应用调用权限,即使应用面向较低的 API 级别也可以调用。无论您的应用面向哪个 API 级别,您都应对应用进行测试,以验证它在缺少需要的权限时行为是否正常。
- 正常权限
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
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
- 危险权限
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
因此,如果API 23及以上时,我们申请危险权限时,除了在清单文件配置,还要在代码中进行动态申请
我们可以自己去写代码进行权限代码申请,这种方法可以参考这样一篇博客
利用easypermissions进行权限申请
首先在app项目下的build.gradle中添加
android {
....
}
repositories {
maven {
url "http://maven.aliyun.com/nexus/content/repositories/releases"
}
}
dependencies {
....
compile 'pub.devrel:easypermissions:1.0.1'
}
这里以照相机权限和位置权限作为例子来讲解;
首先在清单文件(AndroidManifest.xml)中声明这两个属性
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
然后在Activity中重写:
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
// Forward results to EasyPermissions
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
}
然后我们就可以直接在Activity中书写请求权限的代码了
//请求码
@AfterPermissionGranted(RC_CAMERA_AND_LOCATION)
private void methodRequiresTwoPermission() {
String[] perms = {Manifest.permission.CAMERA,
Manifest.permission.ACCESS_FINE_LOCATION};
if (EasyPermissions.hasPermissions(this, perms)) {
// Already have permission, do the thing
// ...
Toast.makeText(this, "已经授权", Toast.LENGTH_SHORT).show();
//表明已经授权,可以进行用户授予权限的操作
} else {
// Do not have permissions, request them now
//弹出一个对话框进行提示用户
EasyPermissions.requestPermissions(this, "应用时需要照相机权限",
RC_CAMERA_AND_LOCATION, perms);
}
}
第一次打开app之后,拒绝权限。第二次进入app就会弹出这个对话框
在oncreate方法中调用这个方法就可以了。
完整的MainActivity
public class MainActivity extends AppCompatActivity {
private static final int RC_CAMERA_AND_LOCATION = 100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
methodRequiresTwoPermission();
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
// Forward results to EasyPermissions
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
}
@AfterPermissionGranted(RC_CAMERA_AND_LOCATION)
private void methodRequiresTwoPermission() {
String[] perms = {Manifest.permission.CAMERA,
Manifest.permission.ACCESS_FINE_LOCATION};
if (EasyPermissions.hasPermissions(this, perms)) {
// Already have permission, do the thing
// ...
Toast.makeText(this, "已经授权", Toast.LENGTH_SHORT).show();
} else {
// Do not have permissions, request them now
EasyPermissions.requestPermissions(this, "应用时需要照相机权限",
RC_CAMERA_AND_LOCATION, perms);
}
}
}
easypermissions的github地址,更多详细代码点击这里