【问题标题】:Detecting GPS on/off switch in Android phones检测 Android 手机中的 GPS 开/关开关
【发布时间】:2011-09-16 03:11:06
【问题描述】:

我想检测用户何时打开或关闭 Android 手机的 GPS 设置。 这意味着当用户打开/关闭 GPS 卫星或通过接入点等进行检测时。

【问题讨论】:

  • +1, 5 被标记为最喜欢的问题,只有 1 个被点赞?!

标签: android gps


【解决方案1】:

我发现最好的方法是附加到

<action android:name="android.location.PROVIDERS_CHANGED" />

意图。

例如:

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

然后在代码中:

public class GpsLocationReceiver extends BroadcastReceiver implements LocationListener 
...

@Override
public void onReceive(Context context, Intent intent)
{
        if (intent.getAction().matches("android.location.PROVIDERS_CHANGED"))
        { 
            // react on GPS provider change action 
        }
}

【讨论】:

  • 不仅适用于 GPS,也适用于网络定位服务。
  • 赞成,但为什么需要&lt;category android:name="android.intent.category.DEFAULT" /&gt;
  • 4 年前就需要它了,我想……谁知道呢。如果效果更好,请尝试不使用。
  • 你不需要实现LocationListener
  • @Radesh 这个问题已经 7 岁了......在 2011 年你不得不
【解决方案2】:

以下是BroadcastReceiver 检测 GPS 位置开/关事件的代码示例:

private BroadcastReceiver locationSwitchStateReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {

            if (LocationManager.PROVIDERS_CHANGED_ACTION.equals(intent.getAction())) {

                LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
                boolean isGpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
                boolean isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

                if (isGpsEnabled || isNetworkEnabled) {
                    //location is enabled
                } else {
                    //location is disabled
                }
            }
        }
    };

您可以动态注册BroadcastReceiver,而不是更改清单文件:

IntentFilter filter = new IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION);
filter.addAction(Intent.ACTION_PROVIDER_CHANGED);
mActivity.registerReceiver(locationSwitchStateReceiver, filter);

不要忘记在您的onPause() 方法中取消注册接收器:

mActivity.unregisterReceiver(locationSwitchStateReceiver);

【讨论】:

  • 它有效。但是为什么 onReceive 会出现多次呢?
  • 可能有不同的操作。调用 onReceive 时,intent.getAction() 的值是多少?
【解决方案3】:

试试这个,

try {
        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

        if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {

            Log.i("About GPS", "GPS is Enabled in your devide");
        } else {
            //showAlert
        }

【讨论】:

  • 我知道如何检测提供程序是打开还是关闭。但我想检测更改,当用户在设置对话框中打开或关闭提供程序时。
【解决方案4】:

执行 android.location.LocationListener,你有两个函数

public void onProviderEnabled(String provider);
public void onProviderDisabled(String provider);

使用它您可以了解所请求的提供程序何时打开或关闭

【讨论】:

  • 这应该是公认的答案。接受的答案已经在使用 LocationListener,所以为什么不使用给定的方法来完成任务。
  • 可以检测开/关状态但检测不到变化(开关)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-18
  • 1970-01-01
相关资源
最近更新 更多