Android 6.0动态权限申请教程

PermissionManage

项目地址:https://github.com/why168/AndroidProjects/tree/master/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); } } /** * RxPermissionGen框架 * * @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; } } }

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持脚本之家!

时间: 2024-11-10 12:19:01

Android 6.0动态权限申请教程的相关文章

Android 7.0动态权限大总结

应公司项目需求,做了下Android 7.0适配.对于我们程序员,适配7.0主要就是对手机本地文件的Uri做转换处理.注意红色字体,意思也就是说对于http开头的等等,非手机存储中的文件就不用管了.Uri.parse("package") 这样的也不用管!!!!! 7.0的适配,就是对手机存储中的私有文件路径的保护,当系统发现你通过intent带走了一个uri,地址是本地的文件,就会限制的.其他的原理普及请搜索其他文章吧,此处略. 下面贴一下使用步骤和我的工具类. 一.需要修改当前mo

赛门铁克警示安卓威胁变种现可攻击Android 6.0新权限模式

赛门铁克安全团队发现,移动恶意软件开发者对威胁攻击进行更新,现可攻击安卓最新运行系统Marshmallow 中的授权模式.在安装移动应用时,安卓系统的授权模式会向所安装的应用授予权限,但并不是直接接受所有的安装要求.然而,Android.Bankosy和Android.Cepsohord等恶意软件如今已适应了这种授权模式,它们会设法获得所需的权限,从而进行恶意活动. 在运行时申请授权 在运行Android 6.0 Marshmallow系统的设备上,移动应用只会在需要时申请所需的权限,并告知用户

Android 6.0 permission 权限适配

1.获取危险权限列表,每个分组申请其中一个权限即可: bogon:~ hujason$ adb shell pm list permissions -d -f  -g Dangerous Permissions: + group:android.permission-group.LGT_CONTENT   package:android   label:LGT Contents   description:LGT Contents + group:com.sec.enterprise.permi

Android 5.0动态设计细节

  错过了历数Honeycomb (3.0).Ice Cream Sandwich (4.0) 和 Jelly Bean (4.1 – 4.3)Android成长的尴尬"青春期",我第一次接触到的还是KitKat 4.4.当时感觉整个系统都非常不错,与iOS 7的操作相对比也没有让人觉得特别适应不了.但是,在应用本身或多或少有些问题,让我仍然无法完全割舍Apple的生态系统,投奔安卓. 而本月发布的安卓5.0棒棒糖,可以说是转折点.5.0为安卓带来了很多优异的功能,而且在很多方面超出了

Android 6.0 Permission权限与安全机制

Marshmallow版本权限修改 android的权限系统一直是首要的安全概念,因为这些权限只在安装的时候被询问一次.一旦安装了,app可以在用户毫不知晓的情况下访问权限内的所有东西,而且一般用户安装的时候很少会去仔细看权限列表,更不会去深入了解这些权限可能带来的相关危害.

Android 6.0权限申请详解及权限资料整理_Android

在android 6.0开始,部分的权限需要我们动态申请,也就是说当我们的打开app的时候系统不会主动像您申请app所需要的部分权限,需要客户在使用app的时候主动的去申请. 一.权限的申请两步骤: 1.权限申请: /** * @param permissions需要申请的权限 * @param requestCode申请回调code */ public static void requestPermissions(final @NonNull Activity activity,final @

Android6.0动态申请权限所遇到的问题小结_Android

白天在做SDK23版本的适配,遇到了不少坑,现在抽空记下来,以此为戒. 首先要知道哪些坑,就得先了解一些定义和基本使用方式. 那么先介绍一下动态申请的权限分组情况. 下面的权限组是由谷歌官方定义的,目的是在申请权限时,只要用户允许同一权限组的任意一条权限,那么该组的其他权限也就默认是允许的.不过据高人介绍,在使用时最好是用到哪个权限就具体的请求该权限,因为保不齐哪天谷歌一高兴就把权限组换了甚至删了 group:android.permission-group.CONTACTS permissio

Android 6.0 fork Zygote时的存储权限管理

Android 6.0 存储权限管理 官方说明 先翻译一段Android的官方文档,原文在:https://source.android.com/devices/storage/ Android 6.0开始支持运行时权限管理的功能.运行时权限管量中当然也包括对READ_EXTERNAL_STORAGE和WRITE_EXTERNAL_STORAGE这两个权限的动态管理.系统需要提供在不杀掉或重启已经运行的应用的情况下去动态授权的机制.目前系统是通过维护三个View来实现的: /mnt/runtim

详解Android数据存储之Android 6.0运行时权限下文件存储的思考_Android

前言: 在我们做App开发的过程中基本上都会用到文件存储,所以文件存储对于我们来说是相当熟悉了,不过自从Android 6.0发布之后,基于运行时权限机制访问外置sdcard是需要动态申请权限,所以以往直接sdcard根目录上直接新建了一个xxx/cache/目录来做文件存储就会不是那么容易控制了,所以有必要重新认识一下Android文件存储的相关知识了. 背景: 有关外置sdcard的读写权限 <uses-permission android:name="android.permissi