本文介绍了检查GPS是在奇巧(4.4)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面奇巧,我得到,如果GPS是与供应商名单

 字符串商= Secure.getString(context.getContentResolver(),Secure.LOCATION_PROVIDERS_ALLOWED);
如果(TextUtils.isEmpty(供应商)){
    返回false;
}
返回providers.contains(LocationManager.GPS_PROVIDER);
 

但在奇巧,Secure.LOCATION_PROVIDERS_ALLOWED是德precated。

Javaodc说:使用Secure.LOCATION_MODE,定位模式是初级讲座......

  Secure.LOCATION_MODE_OFF
Secure.LOCATION_MODE_SENSORS_ONLY
Secure.LOCATION_MODE_BATTERY_SAVING
Secure.LOCATION_MODE_HIGH_ACCURACY
 

但我不知道到底是低于code是正确的。 (我没有奇巧的设备)

  INT locationMode = Secure.getInt(co​​ntext.getContentResolver(),Secure.LOCATION_MODE);
布尔isGpsOn = locationMode = Secure.LOCATION_MODE_OFF!;
 

如果不正确,正确的答案code吧。

修改

我知道LocationManager。但它需要ACCESS_FINE_LOCATION许可。我并不需要访问的位置。

  LocationManager经理=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
布尔isGpsOn = manager.isProviderEnabled(LocationManager.GPS_PROVIDER)
 

解决方案

自答

  @TargetApi(Build.VERSION_ codeS.KITKAT)
@燮pressWarnings(德precation)
公共静态布尔isGpsEnabled(上下文的背景下){

    如果(PackageUtil.checkPermission(上下文,Manifest.permission.ACCESS_FINE_LOCATION)){
        LocationManager LM =(LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
        返回lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
    }

    如果(Build.VERSION.SDK_INT< Build.VERSION_ codeS.KITKAT){
        串提供商= Secure.getString(context.getContentResolver(),
                Secure.LOCATION_PROVIDERS_ALLOWED);
        如果(TextUtils.isEmpty(供应商)){
            返回false;
        }
        返回providers.contains(LocationManager.GPS_PROVIDER);
    } 其他 {
        最终诠释locationMode;
        尝试 {
            locationMode = Secure.getInt(co​​ntext.getContentResolver(),
                    Secure.LOCATION_MODE);
        }赶上(SettingNotFoundException E){
            e.printStackTrace();
            返回false;
        }
        开关(locationMode){

        案例Secure.LOCATION_MODE_HIGH_ACCURACY:
        案例Secure.LOCATION_MODE_SENSORS_ONLY:
            返回true;
        案例Secure.LOCATION_MODE_BATTERY_SAVING:
        案例Secure.LOCATION_MODE_OFF:
        默认:
            返回false;
        }
    }
}
 

和添加这种方法在你的packageutils类:

 公共静态类PackageUtil {

    静态布尔的checkPermission(上下文的背景下,字符串accessFineLocation){

        INT RES = context.checkCallingOrSelfPermission(accessFineLocation);
        返程(RES == PackageManager.PERMISSION_GRANTED);

    }

}
 

Below kitkat, I get if gps is on with providers list

String providers = Secure.getString(context.getContentResolver(), Secure.LOCATION_PROVIDERS_ALLOWED);
if (TextUtils.isEmpty(providers)) {
    return false;
}
return providers.contains(LocationManager.GPS_PROVIDER);

But in kitkat, Secure.LOCATION_PROVIDERS_ALLOWED is deprecated.

Javaodc say "use Secure.LOCATION_MODE", Location modes are belows...

Secure.LOCATION_MODE_OFF
Secure.LOCATION_MODE_SENSORS_ONLY
Secure.LOCATION_MODE_BATTERY_SAVING
Secure.LOCATION_MODE_HIGH_ACCURACY

But i do not know exactly below code is correct. (I do not have kitkat devices)

int locationMode = Secure.getInt(context.getContentResolver(), Secure.LOCATION_MODE);
boolean isGpsOn = locationMode != Secure.LOCATION_MODE_OFF;

If incorrect, Answer correct code please.

EDIT

I know LocationManager. But It require ACCESS_FINE_LOCATION permission. I do not need to "Access" location.

LocationManager manager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );
boolean isGpsOn = manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) 
解决方案

Self Answer

@TargetApi(Build.VERSION_CODES.KITKAT)
@SuppressWarnings("deprecation")
public static boolean isGpsEnabled(Context context) {

    if (PackageUtil.checkPermission(context, Manifest.permission.ACCESS_FINE_LOCATION)) {
        LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        return lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
    }

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
        String providers = Secure.getString(context.getContentResolver(),
                Secure.LOCATION_PROVIDERS_ALLOWED);
        if (TextUtils.isEmpty(providers)) {
            return false;
        }
        return providers.contains(LocationManager.GPS_PROVIDER);
    } else {
        final int locationMode;
        try {
            locationMode = Secure.getInt(context.getContentResolver(),
                    Secure.LOCATION_MODE);
        } catch (SettingNotFoundException e) {
            e.printStackTrace();
            return false;
        }
        switch (locationMode) {

        case Secure.LOCATION_MODE_HIGH_ACCURACY:
        case Secure.LOCATION_MODE_SENSORS_ONLY:
            return true;
        case Secure.LOCATION_MODE_BATTERY_SAVING:
        case Secure.LOCATION_MODE_OFF:
        default:
            return false;
        }
    }
}

And Add this method in your packageutils class :

public static class PackageUtil {

    static boolean checkPermission(Context context, String accessFineLocation) {

        int res = context.checkCallingOrSelfPermission(accessFineLocation);
        return (res == PackageManager.PERMISSION_GRANTED);

    }

}

这篇关于检查GPS是在奇巧(4.4)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 04:01