0

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

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 在同一个类中使用,则设备或模拟器挂起。

4

1 回答 1

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;
} 
于 2013-10-01T06:46:21.123 回答