GpsStatus.Listener只有当GPS是 [英] GpsStatus.Listener works only if GPS is on

查看:121
本文介绍了GpsStatus.Listener只有当GPS是的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用我有一个 GpsStatus.Listener 以接收事件,当用户启用或禁用GPS。一切工作正常,如果 GPS是在开始之前,我的应用程序。在这种情况下,我收到 GPS_EVENT_STARTED 或的 GPS_EVENT_STOPPED 每次我切换上GPS或关闭。

in my App I have a GpsStatus.Listener to receive events when the user enables or disables GPS. Everything works fine if GPS is on before I start the app. In this case I receive a GPS_EVENT_STARTED or a GPS_EVENT_STOPPED everytime I toggle GPS on or off.

现在的问题是,如果 GPS是关闭的,而应用程序正在启动。在这种情况下,我没有收到一个事件,如果我切换GPS或关闭。

The problem is if GPS is off while app is starting. In this case I don't receive an event if I toggle GPS on or off.

有人能解释一下我吗?

下面是我的code:

public class GPSTracker implements android.location.GpsStatus.Listener {

    private final Context context;
    private final LocationListener locListener;
    private LocationManager locationManager;
    private boolean isGPSEnabled = false;

    public GPSTracker(Context context, LocationListener locListener) {
            this.context = context;
            this.locListener = locListener; 
            setupGPS();
    }

    private void setupGPS() {
            locationManager = (LocationManager) getContext().getSystemService(Context.LOCATION_SERVICE);                
            locationManager.addGpsStatusListener(this);
            isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
            if(!isGPSEnabled) {
                    Toast.makeText(getContext(), "GPS disabled", Toast.LENGTH_SHORT).show();
            } else {        
                    locationManager.removeUpdates(locListener);
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME, MIN_DISTANCE, locListener);
            }
    }

    @Override
    public void onGpsStatusChanged(int event) {
            Log.e("onGpsStatusChanged", event+""); 
            switch(event) {
            case GpsStatus.GPS_EVENT_STARTED:
                    Log.e("onGpsStatusChanged", "GPS_EVENT_STARTED");
                    break;
            case GpsStatus.GPS_EVENT_STOPPED:
                    Log.e("onGpsStatusChanged", "GPS_EVENT_STOPPED");
                    break;
            }
    }

所以,我的logcat输出(如GPS是在启动时):

So, my logcat output is (if GPS is on while starting):

  • GpsStatusChanged(24638):1 //应用程序启动
  • GpsStatusChanged(24638):GPS_EVENT_STARTED
  • GpsStatusChanged(24638):4 //寻找修复
  • GpsStatusChanged(24638):4
  • GpsStatusChanged(24638):2 //切换GPS关闭
  • GpsStatusChanged(24638):GPS_EVENT_STOPED

输出,带GPS关闭:没有

Output with GPS off: nothing.

推荐答案

嗯, LocationListener的已提供了:

<一个href="http://developer.android.com/reference/android/location/LocationListener.html#onProviderDisabled%28java.lang.String%29"相对=nofollow> onProviderDisabled() ,<一个href="http://developer.android.com/reference/android/location/LocationListener.html#onProviderEnabled%28java.lang.String%29"相对=nofollow> onProviderEnabled() 和<一href="http://developer.android.com/reference/android/location/LocationListener.html#onStatusChanged%28java.lang.String,%20int,%20android.os.Bundle%29"相对=nofollow> onStatusChanged() 就是出于这样的目的。

onProviderDisabled(),onProviderEnabled() and onStatusChanged() for exactly this purpose.

GpsStatus.Listener 提供有关GPS服务的内部工作信息。它不是要用于告诉状态粉煤的GPS提供商

GpsStatus.Listener delivers info about GPS service's inner workings. It is not to be used for telling the status pf GPS Provider.

LocationListener的提供有关供应商的信息。你注册的那一刻 LocationListener的与位置提供, onProviderEnabled() / onProviderDisabled()相应地调用,在GPS开启或关闭您的应用程序可以随时告诉。

LocationListener delivers info about providers. The moment you register LocationListener with location provider, onProviderEnabled()/onProviderDisabled() is called accordingly, and your app can always tell when GPS is turned on or off.

试试这个:

public class test extends Activity implements LocationListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000,10,this);
    }


    @Override
    public void onLocationChanged(Location location) {
    }

    @Override
    public void onStatusChanged(String s, int i, Bundle bundle) {
    }

    @Override
    public void onProviderEnabled(String s) {
        if(LocationManager.GPS_PROVIDER.equals(s)){
            Toast.makeText(this,"GPS on",Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onProviderDisabled(String s) {
        if(LocationManager.GPS_PROVIDER.equals(s)){
            Toast.makeText(this,"GPS off",Toast.LENGTH_SHORT).show();
        }
    }
}

这篇关于GpsStatus.Listener只有当GPS是的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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