6.0以上获取位置信息,需要动态申请,这里讨论当位置权限申请同意了,但是位置信息关闭了(其实就是6.0以前的gps开关)高精度定位也会获取失败!(网上有的检测方法在某些手机上会检测失败,下列的方式亲测有效)
位置信息开关
package com.guoshikeji.xiaoxiangDriver.utils;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.provider.Settings;
import android.text.TextUtils;
/**
* Created by tyl
* 2019/5/22/022
* Describe:
*/
public class GpsUtil {
/**
* 判断GPS是否开启,GPS或者AGPS开启一个就认为是开启的
* @param context
* @return true 表示开启
*/
public static final boolean isOPen(final Context context) {
if (context==null){
return true;
}
int locationMode = 0;
String locationProviders;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
try {
locationMode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE);
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
return false;
}
return locationMode != Settings.Secure.LOCATION_MODE_OFF;
} else {
locationProviders = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
return !TextUtils.isEmpty(locationProviders);
}
}
/**
* 跳转设置 打开GPS
* @param context
*/
public static final void openGPS(Context context) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
context.startActivity(intent);
}
}
网友评论