安卓:当打开GPS侦测/关闭(或没有应用程序是使用它了)应用程序、使用它、GPS

2023-09-07 14:15:06 作者:时间能抢走的都是垃圾

我在Android 2.3.7和我想要当用户开启GPS写一个BroadcastReceiver将调用/关(即:通过点击在选项使用GPS卫星

I'm on Android 2.3.7 and I'm trying to write a BroadcastReceiver that will get called when the user turns GPS on/off (ie: by tapping on the "use GPS satellites" in the options.

什么我迄今所做的是这样的:

What I've done so far is this:

1)创建以下的BroadcastReceiver:

1) Created the following broadcastReceiver:

public class GPSStatusBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context arg0, Intent arg1) {
        Toast.makeText(arg0, "Broadcast received!", Toast.LENGTH_SHORT).show();

    }
}

2)添加到了Android清单,在标签中:

2)Added this to the android manifest, inside the tags:

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

现在与此的问题是,它似乎不可靠。当我打开GPS上/ FF的的onReceive()方法只运行有时(我说1超时3)其他时候它不会被调用。

Now the problem with this is that it appears not to be reliable. When I turn GPS on/ff the onReceive() method only runs sometimes (I'd say 1 time out 3) the other times it doesn't get called.

我想知道是否有检测时,GPS关闭的可靠方法/上。

I want to know if there is a reliable way to detect when the GPS is turned off/on.

如果没有这将是很好时,没有应用程序正在使用GPS anymoer必须至少在通知(即:当GPS图标状态栏,从消失,说明没有人主动定位)。

If there isn't it would be nice to be notified at least when no app is using the GPS anymoer (ie: when the gps icon disappears from statusbar, indicating no one is actively locating).

编辑:澄清:我不希望我的应用程序运行时做到这一点。我只是whatn我的应用程序时,打开GPS来启动/关闭,做它的东西,然后终止,我并不想订阅LocationsUpdates ...

Clarification: I don't want to do this while my app is running. I just whatn my app to be started when gps is turned on/off, do its thing and then terminate, I don't want to subscribe to LocationsUpdates...

推荐答案

答案是LocationManager.PROVIDERS_CHANGED_ACTION\"

公共静态最后弦乐PROVIDERS_CHANGED_ACTION

public static final String PROVIDERS_CHANGED_ACTION

在API级别9 广播意图操作时配置的位置提供改变。用于与isProviderEnabled(字符串)使用。如果你正在与LOCATION_MODE API进行交互,使用MODE_CHANGED_ACTION来代替。

Added in API level 9 Broadcast intent action when the configured location providers change. For use with isProviderEnabled(String). If you're interacting with the LOCATION_MODE API, use MODE_CHANGED_ACTION instead.

恒值:android.location.PROVIDERS_CHANGED

Constant Value: "android.location.PROVIDERS_CHANGED"

我发现从一个答案here

 
精彩推荐
图片推荐