高德地图中安卓6.0权限处理方案

Posted Gradle官方文件

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了高德地图中安卓6.0权限处理方案相关的知识,希望对你有一定的参考价值。

package com.amap.location.demo;

import java.util.ArrayList;
import java.util.List;

import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.view.KeyEvent;

/**
 * 继承了Activity,实现Android6.0的运行时权限检测
 * 需要进行运行时权限检测的Activity可以继承这个类
 * 
 * @创建时间:2016年5月27日 下午3:01:31 
 * @项目名称: AMapLocationDemo
 * @author hongming.wang
 * @文件名称:PermissionsChecker.java
 * @类型名称:PermissionsChecker
 * @since 2.5.0
 */
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 boolean isNeedCheck = true;

    @Override
    protected void onResume() 
        super.onResume();
        if(isNeedCheck)
            checkPermissions(needPermissions);
        
    

    /**
     * 
     * @param needRequestPermissonList
     * @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()));
        startActivity(intent);
    

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) 
        if(keyCode == KeyEvent.KEYCODE_BACK)
            this.finish();
            return true;
        
        return super.onKeyDown(keyCode, event);
    

以上是关于高德地图中安卓6.0权限处理方案的主要内容,如果未能解决你的问题,请参考以下文章

安卓开发 百度地图怎么自动设置缩放比例

android使用高德定位sdk获取的城市名称怎样让其他的界面进行调用

用高德地图定位不准确,是软件问题还是手机问题?

安卓开发—导入高德地图sdk后工程中无法找到相应的包

在安卓开发中如何在自己设置的经纬度显示到高德地图上中心点

安卓软件嵌入高德地图如何实现上下挪动?我开发一个安卓软件,现在嵌入的高德地图可以左右移动,但是无法