簡體   English   中英

gps打開/關閉時如何觸發廣播接收器?

[英]How to trigger broadcast receiver when gps is turn on/off?

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 打開/關閉時觸發廣播接收器。 調查此事並建議在應用程序中實施的最佳方案。

當用戶想要觸發打開/關閉位置提供的任何操作時,這很有用

您應該在清單中添加此操作

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

我想添加@Deepak Gupta答案 我想添加有關如何在 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));

這可以在您有權訪問上下文的任何地方使用,而不僅僅是在活動或片段內部。 我希望它有幫助。

你可以試試這個代碼,正如用戶@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 中引入 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;
            }
        }

你可以試試這個

艙單

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM