欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 6.0动态权限申请教程

程序员文章站 2023-12-02 15:30:52
permissionmanage 项目地址:https://github.com/why168/androidprojects/tree/master/permissio...

permissionmanage

项目地址:https://github.com/why168/androidprojects/tree/master/permissionmanage

介绍

如果设备运行的是 android 6.0(api 级别 23)或更高版本,并且应用的 targetsdkversion 是 23 或更高版本,则应用在运行时向用户请求权限。

如果设备运行的是 android 5.1(api 级别 22)或更低版本,并且应用的 targetsdkversion 是 22 或更低版本,则系统会在用户安装应用时要求用户授予权限。

参考资料

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

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