Android 6.0动态权限及跳转GPS设置界面的方法
时间:2020-10-13 10:06:53|栏目:Android代码|点击: 次
1.动态权限申请
模糊的位置信息android.permission.ACCESS_COARSE_LOCATION权限为例
在AndroidManifest文件中加入权限
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
然后java代码中动态申请
//动态申请权限的测试方法 public void test() { // 要申请的权限 数组 可以同时申请多个权限 String[] permissions = {Manifest.permission.ACCESS_COARSE_LOCATION}; if (Build.VERSION.SDK_INT >= 23) { //如果超过6.0才需要动态权限,否则不需要动态权限 //如果同时申请多个权限,可以for循环遍历 int check = ContextCompat.checkSelfPermission(this,permissions[0]); // 权限是否已经 授权 GRANTED---授权 DINIED---拒绝 if (check == PackageManager.PERMISSION_GRANTED) { //写入你需要权限才能使用的方法 run(); } else { //手动去请求用户打开权限(可以在数组中添加多个权限) 1 为请求码 一般设置为final静态变量 requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 1); } } else { //写入你需要权限才能使用的方法 run(); } }
其中run()为你自己需要权限才能执行的方法
然后重写申请权限的回掉方法
@Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); //回调,判断用户到底点击是还是否。 //如果同时申请多个权限,可以for循环遍历 if (requestCode == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { //写入你需要权限才能使用的方法 run(); } else { // 没有获取 到权限,从新请求,或者关闭app Toast.makeText(this,"需要获得XXX权限",Toast.LENGTH_SHORT).show(); } }
2.跳转GPS设置界面
相关字符串
<string name="notifyTitle">提示</string> <string name="notifyMsg">当前应用缺少必要权限。\n\n请点击\"设置\"-\"权限\"-打开所需权限。</string> <string name="gpsNotifyMsg">当前应用需要打开定位功能。\n\n请点击\"设置\"-\"定位服务\"-打开定位功能。</string> <string name="setting">设置</string> <string name="cancel">取消</string>
java代码
private int GPS_REQUEST_CODE = 10; /** * 检测GPS是否打开 * * @return */ private boolean checkGPSIsOpen() { boolean isOpen; LocationManager locationManager = (LocationManager) this .getSystemService(Context.LOCATION_SERVICE); isOpen = locationManager.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER); return isOpen; } /** * 跳转GPS设置 */ private void openGPSSettings() { if (checkGPSIsOpen()) { initLocation(); //自己写的定位方法 } else { //没有打开则弹出对话框 new AlertDialog.Builder(this) .setTitle(R.string.notifyTitle) .setMessage(R.string.gpsNotifyMsg) // 拒绝, 退出应用 .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { finish(); } }) .setPositiveButton(R.string.setting, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //跳转GPS设置界面 Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); startActivityForResult(intent, GPS_REQUEST_CODE); } }) .setCancelable(false) .show(); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == GPS_REQUEST_CODE) { //做需要做的事情,比如再次检测是否打开GPS了 或者定位 openGPSSettings(); } }
最后在需要的地方调用 openGPSSettings()方法。
效果(是在相关权限是已经申请好的情况下的效果)
3.下面是我在高德地图Demo中看见的检测权限的Activity
public class CheckPermissionsActivity extends Activity implements ActivityCompat.OnRequestPermissionsResultCallback { /** * 需要进行检测的权限数组 */ protected String[] needPermissions = { Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.READ_PHONE_STATE }; private static final int PERMISSON_REQUESTCODE = 0; private static final int SETTING_REQUESTCODE = 1; /** * 判断是否需要检测,防止不停的弹框 */ private boolean isNeedCheck = true; @Override protected void onResume() { super.onResume(); if (isNeedCheck) { checkPermissions(needPermissions); } } /** * @param permissions * @since 2.5.0 */ private void checkPermissions(String... permissions) { List<String> needRequestPermissonList = findDeniedPermissions(permissions); if (null != needRequestPermissonList && needRequestPermissonList.size() > 0) { ActivityCompat.requestPermissions(this, needRequestPermissonList.toArray( new String[needRequestPermissonList.size()]), PERMISSON_REQUESTCODE); } } /** * 获取权限集中需要申请权限的列表 * * @param permissions * @return * @since 2.5.0 */ private List<String> findDeniedPermissions(String[] permissions) { List<String> needRequestPermissonList = new ArrayList<String>(); for (String perm : permissions) { if (ContextCompat.checkSelfPermission(this, perm) != PackageManager.PERMISSION_GRANTED || ActivityCompat.shouldShowRequestPermissionRationale( this, perm)) { needRequestPermissonList.add(perm); } } return needRequestPermissonList; } /** * 检测是否说有的权限都已经授权 * * @param grantResults * @return * @since 2.5.0 */ private boolean verifyPermissions(int[] grantResults) { for (int result : grantResults) { if (result != PackageManager.PERMISSION_GRANTED) { return false; } } return true; } @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] paramArrayOfInt) { if (requestCode == PERMISSON_REQUESTCODE) { if (!verifyPermissions(paramArrayOfInt)) { showMissingPermissionDialog(); isNeedCheck = false; } } } /** * 显示提示信息 * * @since 2.5.0 */ private void showMissingPermissionDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle(R.string.notifyTitle); builder.setMessage(R.string.notifyMsg); // 拒绝, 退出应用 builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { finish(); } }); builder.setPositiveButton(R.string.setting, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { startAppSettings(); } }); builder.setCancelable(false); builder.show(); } /** * 启动应用的设置 * * @since 2.5.0 */ private void startAppSettings() { Intent intent = new Intent( Settings.ACTION_APPLICATION_DETAILS_SETTINGS); intent.setData(Uri.parse("package:" + getPackageName())); startActivityForResult(intent, SETTING_REQUESTCODE); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { this.finish(); return true; } return super.onKeyDown(keyCode, event); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == SETTING_REQUESTCODE) { checkPermissions(needPermissions); } } }
继承于CheckPermissionsActivity即可
不允许就跳转系统设置界面,若没设置再次检测权限并申请,直到允许为止。可和GPS设置搭配使用
上一篇:Android编程开发之EditText实现输入QQ表情图像的方法
栏 目:Android代码
下一篇:Android UI控件RatingBar实现自定义星星评分效果
本文标题:Android 6.0动态权限及跳转GPS设置界面的方法
本文地址:http://www.codeinn.net/misctech/10579.html