打开/关闭gps时如何触发广播接收器?

时间:2013-12-19 04:42:58

标签: android gps broadcastreceiver location-provider

public class BootReceiver extends BroadcastReceiver  {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().matches("android.location.PROVIDERS_CHANGED")) {
            Toast.makeText(context, "in android.location.PROVIDERS_CHANGED",
                Toast.LENGTH_SHORT).show();
            Intent pushIntent = new Intent(context, LocalService.class);
            context.startService(pushIntent);
        } else {
            Toast.makeText(context, "not in android.location.PROVIDERS_CHANGED",
                Toast.LENGTH_SHORT).show();
            Intent pushIntent = new Intent(context, LocalService.class);
            context.startService(pushIntent);
        }
    }

    @Override
    public void onLocationChanged(Location arg0) {

    }
}

在我的应用程序中,我需要在打开/关闭gps时触发广播接收器。  调查此事并建议在应用程序中实施最佳方案。

5 个答案:

答案 0 :(得分:69)

当用户想要在打开/关闭位置提供

时触发任何操作时,此功能非常有用

您应该在清单

中添加此操作
<action android:name="android.location.PROVIDERS_CHANGED" />

添加此操作后,您可以触发广播接收器

<receiver android:name=".GpsLocationReceiver">
    <intent-filter>
        <action android:name="android.location.PROVIDERS_CHANGED" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</receiver>

在您的BroadcastReceiver课程中,您必须在该课程中实施LocationListener,如下所示。

public class GpsLocationReceiver extends BroadcastReceiver {        
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().matches("android.location.PROVIDERS_CHANGED")) {
            Toast.makeText(context, "in android.location.PROVIDERS_CHANGED",
            Toast.LENGTH_SHORT).show();
            Intent pushIntent = new Intent(context, LocalService.class);
            context.startService(pushIntent);
        }
    }
}

答案 1 :(得分:27)

我想添加 @Deepak Gupta answer。 我希望在GPS状态发生变化时添加如何在片段活动中注册动态brodacsast接收器的代码。

在您的活动或片段中注册您的广播接收器,如下所示。

private BroadcastReceiver gpsReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().matches(LocationManager.PROVIDERS_CHANGED_ACTION)) {
            //Do your stuff on GPS status change
        }
    }
};

片段

getContext().registerReceiver(gpsReceiver, new IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION));

活动

registerReceiver(gpsReceiver, new IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION));

可以在任何可以访问Context的地方使用它,而不仅仅是在一个活动或片段中。我希望它有所帮助。

答案 2 :(得分:13)

您可以尝试使用此代码,用户@DanielNugent指出:

    ContentResolver contentResolver = getContext().getContentResolver();
    // Find out what the settings say about which providers are enabled
    int mode = Settings.Secure.getInt(
            contentResolver, Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_OFF);

    if (mode != Settings.Secure.LOCATION_MODE_OFF) {
      if (mode == Settings.Secure.LOCATION_MODE_HIGH_ACCURACY) {
        locationMode = "High accuracy. Uses GPS, Wi-Fi, and mobile networks to determine location";
      } else if (mode == Settings.Secure.LOCATION_MODE_SENSORS_ONLY) {
          locationMode = "Device only. Uses GPS to determine location";
      } else if (mode == Settings.Secure.LOCATION_MODE_BATTERY_SAVING) {
          locationMode = "Battery saving. Uses Wi-Fi and mobile networks to determine location";
      }
    }

答案 3 :(得分:0)

在Android中引入Android定位模式后的工作解决方案

我们可以使用定位模式进行更准确的检查

private boolean isGpsEnabled(Context context) {
            ContentResolver contentResolver = getContext().getContentResolver();
            // Find out what the settings say about which providers are enabled
            //  String locationMode = "Settings.Secure.LOCATION_MODE_OFF";
            int mode = Settings.Secure.getInt(
                    contentResolver, Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_OFF);
            if (mode != Settings.Secure.LOCATION_MODE_OFF) {
                return true;
               /* if (mode == Settings.Secure.LOCATION_MODE_HIGH_ACCURACY) {
                    locationMode = "High accuracy. Uses GPS, Wi-Fi, and mobile networks to determine location";
                } else if (mode == Settings.Secure.LOCATION_MODE_SENSORS_ONLY) {
                    locationMode = "Device only. Uses GPS to determine location";
                } else if (mode == Settings.Secure.LOCATION_MODE_BATTERY_SAVING) {
                    locationMode = "Battery saving. Uses Wi-Fi and mobile networks to determine location";
                }*/
            } else {
                return false;
            }
        }

答案 4 :(得分:-2)

你可以试试这个

<强>清单

<receiver android:name=".MyReceiver">
        <intent-filter>
            <action android:name="android.location.GPS_ENABLED_CHANGE" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>

<强> MyReceiver

if (intent.getAction().matches("android.location.GPS_ENABLED_CHANGE"))
    {
        boolean enabled = intent.getBooleanExtra("enabled",false);

        Toast.makeText(context, "GPS : " + enabled,
                Toast.LENGTH_SHORT).show();
    }