文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

Android 6.0动态权限申请教程

2022-06-06 01:25

关注

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

示例代码



public class BaseActivity extends AppCompatActivity {
 @Override
 protected void onCreate(@Nullable Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 }
 
 public boolean hasPermission(String... permissions) {
 for (String permission : permissions) {
 if (ActivityCompat.checkSelfPermission(this, permission)
 != PackageManager.PERMISSION_GRANTED) {
 return false;
 }
 }
 return true;
 }
 
 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;
 }
 }
 
 protected void doSDCardPermission() {
 //TODO
 }
 
 protected void doCallPhone() {
 //TODO 
 }
}


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("/file/upload/202206/06/0bg2prurw3h.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写入成功");
 }
 });
 }
}


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);
 }
}


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 写入SD卡的权限申请实例讲解Android 6.0 蓝牙搜索不到设备原因,MIUI权限申请机制方法React-Native之Android(6.0及以上)权限申请详解Android 6.0权限申请详解及权限资料整理Android超清晰6.0权限申请AndPermission


阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-移动开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯