【问题标题】:How to trigger broadcast receiver when gps is turn on/off?gps打开/关闭时如何触发广播接收器?
【发布时间】:2014-01-07 13:04:30
【问题描述】:
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 打开/关闭时触发广播接收器。 调查此事并建议在应用中实施的最佳方案。

【问题讨论】:

  • 您是否尝试监听位置模式设置:“仅设备(使用 GPS 确定您的位置)”?

标签: android gps broadcastreceiver location-provider


【解决方案1】:

当用户想要触发任何打开/关闭位置提供的操作时,这很有用

您应该在清单中添加此操作

<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);
        }
    }
}

【讨论】:

  • 你有什么解决屏幕旋转广播接收器的办法??????请帮忙
  • 为什么要在这里实现LocationListener?
  • @Kartihkraj...不,这里不需要实现LocationListener。我现在已经编辑了。
  • @AndroidGuru 这段代码如何告诉你位置模式是否打开/关闭?
  • 广播接收器的Intent过滤器以外的在哪里添加
【解决方案2】:

我想添加 @Deepak Gupta answer。 我想在 GPS 状态更改时添加代码,说明如何在您的 fragmentactivity 中注册动态 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));

这可以用于您可以访问上下文的任何地方,而不仅仅是在活动或片段内部。我希望它有所帮助。

【讨论】:

  • 无需对操作字符串进行硬编码。而是使用...matches(LocationManager.PROVIDERS_CHANGED_ACTION)...new IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION)
  • 谢谢@tir38,我已经用LocationManager.PROVIDERS_CHANGED_ACTION更新了我的答案
  • 什么是registerReceiver? @pRaNaY
  • 这是一个活动方法,用于注册BroadcastReceiver。检查developer.android.com/reference/android/content/Context.html
  • 这还在工作吗?,我似乎无法在 LocationManager.PROVIDERS_CHANGED_ACTION 上获得广播
【解决方案3】:

你可以试试这个代码,正如用户@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";
      }
    }

【讨论】:

  • 您的答案效果很好,但它仅适用于 Android API 19 及更高版本。 19以下的版本有办法吗?
  • 该设置常量在 API 级别 28 中已弃用。
【解决方案4】:

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;
            }
        }

【讨论】:

    【解决方案5】:

    你可以试试这个

    清单

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

    我的接收者

    if (intent.getAction().matches("android.location.GPS_ENABLED_CHANGE"))
        {
            boolean enabled = intent.getBooleanExtra("enabled",false);
    
            Toast.makeText(context, "GPS : " + enabled,
                    Toast.LENGTH_SHORT).show();
        }
    

    【讨论】:

    • Intent 始终为空。请勿包含 enabled 之类的内容。
    猜你喜欢
    • 1970-01-01
    • 2016-03-27
    • 2015-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    相关资源
    最近更新 更多