设备在检查gps时启动,并且启用了位置服务

时间:2013-09-17 07:01:23

标签: android gps

启用或禁用检查gps和位置服务时,设备会挂起。下面是我用于cheking的代码:

private void initGpsObserver(final Context context, final Class<?> caller) 
{
     if (null == locationSettingsObserver)
     {
    locationSettingsObserver = new ContentObserver(null) 
        {
            @Override
        public void onChange(boolean selfChange) 
            {
            final LocationManager manager = (LocationManager) context
            .getSystemService(Context.LOCATION_SERVICE);
        if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) 
                {
            isGpsDisabled = true;
            mHandler.sendEmptyMessage(0);
        }
                else 
                {
             if (alert.isShowing()) 
                     {
            alert.dismiss();
              }
              isGpsDisabled = false;
        }

        if (!manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) 
                {
            isLocationServicesDisabled = true;
            mHandler.sendEmptyMessage(0);
        }
                else 
                {
            if (alert.isShowing()) 
                        {
                alert.dismiss();
            }
            isLocationServicesDisabled = false;
        }
    }
};
        context.getApplicationContext()
        .getContentResolver()
        .registerContentObserver(
    Settings.Secure.getUriFor(Settings.Secure.LOCATION_PROVIDERS_ALLOWED),
        true, locationSettingsObserver);
    }
}

如果同时使用GPS_PROVIDER和NETWORK_PROVIDER,则设备或模拟器会挂起。

1 个答案:

答案 0 :(得分:0)

我使用这段代码解决了这个问题。下面这段代码检测是否启用了“位置和安全设置”中的“使用无线网络”选项。

public static boolean isWifiLocationEnabled(Context context) 
{
    ContentResolver cr = context.getContentResolver();
    String enabledProviders = Settings.Secure.getString(cr,
            Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
    if (!TextUtils.isEmpty(enabledProviders)) 
    {
        // not the fastest way to do that :)
        String[] providersList = TextUtils.split(enabledProviders, ",");
        for (String provider : providersList) 
        {
            if (LocationManager.NETWORK_PROVIDER.equals(provider)) 
            {
                return true;
            }
        }
    }
    return false;
}