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