检查Android中的BroadcastReceiver是否启用了位置服务

时间:2018-05-21 07:08:40

标签: android location broadcastreceiver google-location-services

我需要检查我onReceive()的{​​{1}}方法中是否启用了位置设置,以获取位置更新并使用该位置继续进行。我知道如何使用BroadcastReceiverLocationManager从活动中执行此操作,但如何从SettingsApi执行此操作?

1 个答案:

答案 0 :(得分:0)

您可以按如下方式检查位置服务:

LocationManager lm = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
boolean gps_enabled = false;
boolean network_enabled = false;

try {
    gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch(Exception ex) {}

try {
    network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch(Exception ex) {}
if(gps_enabled || network_enabled ){
    //Do something.
}