动态查看用户手机定位权限开通

private int SETTING_REQUESTCODE = 10;


    /**
     * 检测GPS是否打开
     *
     * @return
     */
    private boolean checkGPSIsOpen() {
        boolean isOpen;
        LocationManager locationManager = (LocationManager) context
                .getSystemService(Context.LOCATION_SERVICE);
        //NETWORK_PROVIDER,PASSIVE_PROVIDER
        isOpen = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        return isOpen;
    }


    /**
     * 跳转GPS设置
     */
    private void openGPSSettings() {
        if (checkGPSIsOpen()) {
//            initLocation(); //自己写的定位方法
        } else {
            //没有打开则弹出对话框
            new AlertDialog.Builder(context)
                    .setTitle(R.string.notifyTitle)
                    .setMessage(R.string.gpsNotifyMsg)
                    // 拒绝, 退出应用
                    .setNegativeButton(R.string.cancel,
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    getDingWei();
                                }
                            })

                    .setPositiveButton(R.string.setting,
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    //跳转定位权限设置界面
                                    Intent intent = new Intent();
                                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                    if (Build.VERSION.SDK_INT >= 9) {
                                        intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
                                        intent.setData(Uri.fromParts("package", context.getPackageName(), null));
                                    } else if (Build.VERSION.SDK_INT <= 8) {
                                        intent.setAction(Intent.ACTION_VIEW);
                                        intent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails");
                                        intent.putExtra("com.android.settings.ApplicationPkgName", context.getPackageName());
                                    }

                                    startActivityForResult(intent, SETTING_REQUESTCODE);
                                }
                            })

                    .setCancelable(false)
                    .show();

        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == SETTING_REQUESTCODE) {
            //做需要做的事情,比如再次检测是否打开GPS了 或者定位
            openGPSSettings();

        }
    }
<!--开启定位权限;;;弹窗提示语句-->

<string name="notifyTitle">提示</string>
<string name="notifyMsg">当前应用缺少必要权限。\n\n请点击\"设置\"-\"权限\"-打开所需权限。</string>
<string name="gpsNotifyMsg">当前应用需要打开定位功能。\n\n请点击\"设置\"-\"定位服务\"-打开定位功能。</string>
<string name="setting">设置</string>
<string name="cancel">取消</string>

<!--获取经纬度,方法的调用-->
LocationUtil.getLocation(new LocationUtil.OnGetLocationLis() {
    @Override
    public void OnSuccess(double lng, double lat, String address) {

        mLng = lng + "";
        mLat = lat + "";

       
    }


    @Override
    public void OnFail() {

    }
}, true);

<!--获取经纬度,工具类-->
/**
 * 获取地理位置
 * 
 * @author slg
 * 
 */
public class LocationUtil {
   //错误定位返回
   public static final double ERR=0.0;

   // 用户当前所在经度
   private static double LNG = ERR;
   // 用户当前所在维度
   private static double LAT = ERR;
   //当前用户位置信息
   private static String ADDRESS ="";
   /**
    * 获取经纬度
    */
   public static void getLocation(final OnGetLocationLis lis, boolean refresh) {
         if (refresh || LNG == ERR || LAT == ERR) {    
//          if (lis != null) {
//             lis.OnSuccess(LNG, LAT,ADDRESS);
//          }
            final LocationClient locationClient = new LocationClient(BaseApplication.INSTANCE);
            // 设置定位条件
            LocationClientOption option = new LocationClientOption();
            option.setOpenGps(true); //是否打开GPS
            option.setCoorType("bd09ll"); // 设置返回值的坐标类型。
            option.setLocationMode(LocationMode.Hight_Accuracy);
            option.setScanSpan(300); // 设置定时定位的时间间隔。单位毫秒
            option.setIsNeedAddress(true);
            locationClient.setLocOption(option);
            // 注册位置监听器
            locationClient.registerLocationListener(new BDLocationListener() {

               @Override
               public void onReceiveLocation(BDLocation location) {

                  if (location == null) {
                     if(lis!=null){
                        lis.OnFail();
                        lis.OnSuccess(LNG, LAT, ADDRESS);
//                      Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
//                      startActivity(intent);
                     }
                     return;
                  }     
                  double lon =location.getLongitude();
                  double lat = location.getLatitude();
//                if(lon==ERR||lat==ERR){
//                   if(lis!=null){
//                      lis.OnFail();
//                   }
//                }else{
                     LNG = lon;
                     LAT = lat;
                     //int a=location.getCity().length()+location.getProvince().length();
                     //int b=location.getAddrStr().length();
                     ADDRESS =location.getAddrStr();
                     
                     if(lis!=null){
                        lis.OnSuccess(LNG, LAT, ADDRESS);
                     }
                     
//                }

                  if (locationClient != null && locationClient.isStarted()) {
                     locationClient.stop();
                  }

               }
            });
            locationClient.start();
            locationClient.requestLocation();


         } else {
            if (lis != null) {
//             if(LNG==ERR||LAT==ERR){
//                lis.OnFail();
//             }else{
                  lis.OnSuccess(LNG, LAT,ADDRESS);
//             }              
            }

         }

   }

   public interface OnGetLocationLis {
      public void OnSuccess(double lng, double lat, String address);
      public void OnFail();
   }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值