Android 6.0动态权限申请教程
转载请标明出处:
http://www.cnblogs.com/why168888/p/6580396.html本文出自:【Edwin博客园】
PermissionManage
介绍
- 如果设备运行的是 Android 6.0(API 级别 23)或更高版本,并且应用的 targetSdkVersion 是 23 或更高版本,则应用在运行时向用户请求权限。
- 如果设备运行的是 Android 5.1(API 级别 22)或更低版本,并且应用的 targetSdkVersion 是 22 或更低版本,则系统会在用户安装应用时要求用户授予权限。
参考资料
- https://developer.android.google.cn/guide/topics/security/permissions.html
- https://github.com/lovedise/PermissionGen
- https://github.com/tbruyelle/RxPermissions
- 以下是需要单独申请的权限,共分为9组,每组只要有一个权限申请成功了,就默认整组权限都可以使用了
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
- 以下是普通权限,只需要在AndroidManifest.xml中申请即可
android.permission.ACCESS_LOCATION_EXTRA_COMMANDS
android.permission.ACCESS_NETWORK_STATE
android.permission.ACCESS_NOTIFICATION_POLICY
android.permission.ACCESS_WIFI_STATE
android.permission.ACCESS_WIMAX_STATE
android.permission.BLUETOOTH
android.permission.BLUETOOTH_ADMIN
android.permission.BROADCAST_STICKY
android.permission.CHANGE_NETWORK_STATE
android.permission.CHANGE_WIFI_MULTICAST_STATE
android.permission.CHANGE_WIFI_STATE
android.permission.CHANGE_WIMAX_STATE
android.permission.DISABLE_KEYGUARD
android.permission.EXPAND_STATUS_BAR
android.permission.FLASHLIGHT
android.permission.GET_ACCOUNTS
android.permission.GET_PACKAGE_SIZE
android.permission.INTERNET
android.permission.KILL_BACKGROUND_PROCESSES
android.permission.MODIFY_AUDIO_SETTINGS
android.permission.NFC
android.permission.READ_SYNC_SETTINGS
android.permission.READ_SYNC_STATS
android.permission.RECEIVE_BOOT_COMPLETED
android.permission.REORDER_TASKS
android.permission.REQUEST_INSTALL_PACKAGES
android.permission.SET_TIME_ZONE
android.permission.SET_WALLPAPER
android.permission.SET_WALLPAPER_HINTS
android.permission.SUBSCRIBED_FEEDS_READ
android.permission.TRANSMIT_IR
android.permission.USE_FINGERPRINT
android.permission.VIBRATE
android.permission.WAKE_LOCK
android.permission.WRITE_SYNC_SETTINGS
com.android.alarm.permission.SET_ALARM
com.android.launcher.permission.INSTALL_SHORTCUT
com.android.launcher.permission.UNINSTALL_SHORTCUT
示例代码
/**
* BaseActivity
*
* @author Edwin.Wu
* @version 2017/3/17$ 14:12$
* @since JDK1.8
*/
public class BaseActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
/**
* 为子类提供一个权限检查方法
*
* @param permissions
* @return
*/
public boolean hasPermission(String... permissions) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(this, permission)
!= PackageManager.PERMISSION_GRANTED) {
return false;
}
}
return true;
}
/**
* 权限请求申请
*
* @param requestCode 请求码
* @param permissions 权限
*/
public void requestPermission(int requestCode, String... permissions) {
ActivityCompat.requestPermissions(this, permissions, requestCode);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case PermissionsConstans.WRITE_STORAGE_CODE:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
doSDCardPermission();
} else {
//TODO 提示用户权限未授予
Toast.makeText(BaseActivity.this, "WRITE_EXTERNAL_STORAGE 权限未开启", Toast.LENGTH_SHORT).show();
}
break;
case PermissionsConstans.CALL_PHONE_CODE:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
doCallPhone();
} else {
//TODO 提示用户权限未授予
Toast.makeText(BaseActivity.this, "ACTION_CALL 权限未开启", Toast.LENGTH_SHORT).show();
}
break;
default:
break;
}
}
/**
* 默认的写SD权限处理
*/
protected void doSDCardPermission() {
//TODO
}
/**
* 默认的打电话处理
*/
protected void doCallPhone() {
//TODO
}
}
/**
* BaseActivity
*
* @author Edwin.Wu
* @version 2017/3/17 11:59
* @since JDK1.8
*/
public class PermissionActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_permission);
}
public void callPhone(View view) {
if (hasPermission(android.Manifest.permission.CALL_PHONE)) {
doCallPhone();
} else {
requestPermission(PermissionsConstans.CALL_PHONE_CODE, android.Manifest.permission.CALL_PHONE);
}
}
public void sdCardPermission(View view) {
if (hasPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
doSDCardPermission();
} else {
requestPermission(PermissionsConstans.WRITE_STORAGE_CODE, android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
}
@Override
protected void doCallPhone() {
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + "10000"));
startActivity(intent);
}
@Override
protected void doSDCardPermission() {
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request
.Builder()
.url("http://img.mmjpg.com/2015/350/3.jpg")
.get()
.build();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
ResponseBody body = response.body();
InputStream is = body.byteStream();
int len = 0;
byte[] bytes = new byte[2048];
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + System.currentTimeMillis() + ".jpg";
File file = new File(path);
FileOutputStream fos = new FileOutputStream(file);
while ((len = is.read(bytes)) != -1) {
fos.write(bytes, 0, len);
fos.flush();
}
Toast.makeText(PermissionActivity.this, "SDCard写入成功", Toast.LENGTH_SHORT).show();
Log.e("Edwin", "SDCard写入成功");
}
});
}
}
/**
* PermissionGen框架
*
* @author Edwin.Wu
* @version 2017/3/17 23:19
* @since JDK1.8
*/
public class PermissionGenActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_permission_gen);
}
public void openCamera(View view) {
PermissionGen.needPermission(this, 200, Manifest.permission.CAMERA);
}
public void openGroup(View view) {
PermissionGen
.with(this)
.addRequestCode(100)
.permissions(
Manifest.permission.READ_CONTACTS,
Manifest.permission.RECEIVE_SMS,
Manifest.permission.CAMERA,
Manifest.permission.WRITE_CONTACTS)
.request();
}
@PermissionSuccess(requestCode = 100)
public void openGroupSuccess() {
Toast.makeText(this, "Group Permission Success", Toast.LENGTH_SHORT).show();
startActivity(new Intent(this, ContactActivity.class));
}
@PermissionFail(requestCode = 100)
private void openGroupFail() {
Toast.makeText(this, "Group permission is not granted", Toast.LENGTH_SHORT).show();
}
@PermissionSuccess(requestCode = 200)
public void openCameraSuccess() {
Toast.makeText(this, "Camera permission Success", Toast.LENGTH_SHORT).show();
}
@PermissionFail(requestCode = 200)
public void openCameraFail() {
Toast.makeText(this, "Camera permission is not granted", Toast.LENGTH_SHORT).show();
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
PermissionGen.onRequestPermissionsResult(this, requestCode, permissions, grantResults);
}
}
/**
* RxPermission框架
*
* @author Edwin.Wu
* @version 2017/3/19 14:17
* @since JDK1.8
*/
public class RxPermissionActivity extends AppCompatActivity {
private static final String TAG = "RxPermissionsSample";
private Camera camera;
private SurfaceView surfaceView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RxPermissions rxPermissions = new RxPermissions(this);
rxPermissions.setLogging(true);
setContentView(R.layout.activity_rx_permission);
surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
RxView.clicks(findViewById(R.id.enableCamera))
// Ask for permissions when button is clicked
.compose(rxPermissions.ensureEach(Manifest.permission.CAMERA))
.subscribe(new Action1<Permission>() {
@Override
public void call(Permission permission) {
Log.i(TAG, "Permission result " + permission);
if (permission.granted) {
releaseCamera();
camera = Camera.open(0);
try {
camera.setPreviewDisplay(surfaceView.getHolder());
camera.startPreview();
} catch (IOException e) {
Log.e(TAG, "Error while trying to display the camera preview", e);
}
} else if (permission.shouldShowRequestPermissionRationale) {
// Denied permission without ask never again
Toast.makeText(RxPermissionActivity.this, "Denied permission without ask never again", Toast.LENGTH_SHORT).show();
} else {
// Denied permission with ask never again
// Need to go to the settings
Toast.makeText(RxPermissionActivity.this, "Permission denied, can't enable the camera", Toast.LENGTH_SHORT).show();
}
}
},
new Action1<Throwable>() {
@Override
public void call(Throwable t) {
Log.e(TAG, "onError", t);
}
},
new Action0() {
@Override
public void call() {
Log.i(TAG, "OnComplete");
}
});
}
@Override
protected void onStop() {
super.onStop();
releaseCamera();
}
private void releaseCamera() {
if (camera != null) {
camera.release();
camera = null;
}
}
}
Android 6.0动态权限申请教程的更多相关文章
- Android 6.0动态权限申请
转载(Android 6.0 动态权限申请简单简洁优雅的处理方式): https://blog.csdn.net/lin_dianwei/article/details/79025324
- Android 6.0 动态权限申请注意事项
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/uana_777/article/details/54136255 Part One 权限区分 And ...
- Android 6.0 动态权限申请
1. 概述 Android 6.0 (API 23) 之前应用的权限在安装时全部授予,运行时应用不再需要询问用户.在 Android 6.0 或更高版本对权限进行了分类,对某些涉及到用户隐私的权限可在 ...
- android 6.0 动态权限
Android 6.0 动态权限: 除了要在AndroidManifest.xml中申请外,还需使用时,请求用户允许授权. 以下是需要单独申请的权限,共分为9组,每组只要有一个权限申请成功了,就默认整 ...
- Android 6.0 - 动态权限管理的解决方案(转)
转自:http://www.cnblogs.com/dubo-/p/6018262.html Android 6.0 - 动态权限管理的解决方案 转载请标注 Android 6.0版本(Api 2 ...
- Android6.0动态权限申请
goggle在Android6.0要求部分权限需要动态申请,直接下载AndroidManifest.xml中无效 6.0权限的基本知识,以下是需要单独申请的权限,共分为9组, 每组只要有一个权限申请成 ...
- Android6.0动态权限申请步骤以及需要注意的一些坑
因为工作需要,简单研究了一下Android6.0权限申请,在Google提供的sample的基础上,写了一个简单的demo.算是自己的笔记吧,可能会比较混乱,主要是方便以后查看.后期有别的问题,随时更 ...
- android 6.0+ 动态权限 拒绝不再询问后跳转设置应用详情页面
android 6.0+ 的权限 需要动态申请 这里的权限针对的是 敏感权限: SMS(短信) SEND_SMS RECEIVE_SMS READ_SMS RECEIVE_WAP_PUSH RECEI ...
- Android 6.0动态权限(转)
转自:http://blog.csdn.net/uana_777/article/details/54136255 Part One 权限区分 Android 6.0 为了保护用户隐私,将一些权限的申 ...
随机推荐
- Mongo db 与mysql 语法比较
mongodb与mysql命令对比 传统的关系数据库一般由数据库(database).表(table).记录(record)三个层次概念组成,MongoDB是由数据库(database).集合(col ...
- Gin实战:Gin+Mysql简单的Restful风格的API
我们已经了解了Golang的Gin框架.对于Webservice服务,restful风格几乎一统天下.Gin也天然的支持restful.下面就使用gin写一个简单的服务,麻雀虽小,五脏俱全.我们先以一 ...
- 第9章 scrapy-redis分布式爬虫
9-1 分布式爬虫要点 1.分布式的优点 充分利用多机器的宽带加速爬取 充分利用多机的IP加速爬取速度 问:为什么scrapy不支持分布式? 答:在scrapy中scheduler是运行在队列的,而队 ...
- IntelliJ使用指南—— 导入Eclipse的Web项目
通常一个团队中可能有人用eclipse,有人用intelliJ,那么经常会出现需要导入别人用eclipse建好的web项目.而IntelliJ提供了多种项目类型的导入方式,其中就有eclipse. 在 ...
- 503 Service Unavailable
转自:https://jingyan.baidu.com/article/6b1823099a258eba58e15902.html 第一 服务是不是被关闭了. 第二 原因IIS设置最大并发连接数 网 ...
- php 函数的嵌套
/*一定要小心变量作用域*/ function insert_dynamic() { function bar() { echo "I don't exist until insert_dy ...
- UVA 455(最小周期)
最小周期可以用%枚举 #include <iostream> #include <string> #include <cstring> #include <c ...
- 谈缓存和Redis
自从上次分享<Redis到底该如何利用?>已经有1年多了,这1年经历了不少.从码了我们网站的第一行开始到现在,我们的缓存模块也不断在升级,这之中确实略有心得,最近也有朋友探讨缓存,觉得可以 ...
- jQuery中判断input的checked属性
<input type="checkbox" id="ipt1" checked> <input type="checkbox&qu ...
- 转 VS Code 快捷键大全,没有更全
VS Code折腾记 - (2) 快捷键大全,没有更全 前言 VSCode的快捷键继承了一些IDE风格,有VS的身影,也有Emacs的身影..简言之,内置快捷键玩熟了,效率提高不是一点两点. VsCo ...