收到通知(通过广播接收器),当GPS(或位置服务)的启动/停止 [英] Receive a notification (via BroadcastReceiver) when GPS (or location services) is started/stopped

查看:76
本文介绍了收到通知(通过广播接收器),当GPS(或位置服务)的启动/停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过的方法通过使用侦​​听<一个一个BroadcastReceiver href=\"http://developer.android.com/reference/android/location/LocationManager.html#PROVIDERS_CHANGED_ACTION\"相对=nofollow> PROVIDERS_CHANGED 行动,但只告诉我当位置设置开启/关闭。我想,当正在使用的位置服务,以获取由任何应用程序的位置来知道。我并不需要知道哪些应用程序是干什么的,我不喜欢的位置本身。我只是想知道,每当某个应用程序试图获取一个位置,当它停止这样做。这可能吗?

I tried an approach by using a BroadcastReceiver that listens for PROVIDERS_CHANGED action, but that only tells me when the location settings are turned on/off. I would like to know when the location services are being used to acquire a location by any application. I don't need to know which application is doing it, and I don't care for the location itself. I just want to know whenever some application tries to acquire a location, and when it stops doing that. Is this possible?

推荐答案

我不知道,如果你真的需要通过广播接收到通知,但如果不是绝对必需的,那么你可以使用的 GpsStatus.Listener

I don't know if you really need to be notified via broadcast receiver, but if it is not absolutely required then you can use GpsStatus.Listener :

下面是如何使用它:

mLocationManager.addGpsStatusListener(new GpsStatus.Listener(){

    @Override
    public void onGpsStatusChanged(int event) {
        if(event==GpsStatus.GPS_EVENT_STARTED){
            Log.d(TAG,"Gps Started");
        }else if(event==GpsStatus.GPS_EVENT_STOPPED){
            Log.d(TAG,"Gps Stopped");
        }
    }
});

如果你真的需要接收通过广播接收器这个信息:只是包装这个code的服务,并从那里发送事件

And if you really need to receive this info via BroadcastReceiver : just wrap this code in a Service and send the event from there.

这篇关于收到通知(通过广播接收器),当GPS(或位置服务)的启动/停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆