GpsStatus.Listener 仅在 GPS 开启时有效

GpsStatus.Listener works only if GPS is on(GpsStatus.Listener 仅在 GPS 开启时有效)

本文介绍了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.

谁能给我解释一下?

这是我的代码:

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 已经提供了:

onProviderDisabled(),onProviderEnabled()onStatusChanged() 正是为了这个目的.

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

GpsStatus.Listener 提供有关 GPS 服务内部运作的信息.它不能用于告诉 GPS Provider 的状态.

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 开启时有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:GpsStatus.Listener 仅在 GPS 开启时有效

基础教程推荐