Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 侦听器仅在GPS打开时工作_Android_Gps_Listener - Fatal编程技术网

Android 侦听器仅在GPS打开时工作

Android 侦听器仅在GPS打开时工作,android,gps,listener,Android,Gps,Listener,在我的应用程序中,当用户启用或禁用GPS时,我可以接收事件。如果在我启动应用程序之前打开GPS,一切正常。在这种情况下,每次打开或关闭GPS时,我都会收到一个或一个 问题是当应用程序启动时,GPS是否关闭。在这种情况下,如果我打开或关闭GPS,我不会收到事件 有人能给我解释一下吗 这是我的密码: public class GPSTracker implements android.location.GpsStatus.Listener { private final Context c

在我的应用程序中,当用户启用或禁用GPS时,我可以接收事件。如果在我启动应用程序之前打开GPS,一切正常。在这种情况下,每次打开或关闭GPS时,我都会收到一个或一个

问题是当应用程序启动时,GPS是否关闭。在这种情况下,如果我打开或关闭GPS,我不会收到事件

有人能给我解释一下吗

这是我的密码:

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在启动时打开):

  • GpsStatusChanged(24638):1//应用程序启动
  • GpsStatusChanged(24638):GPS\u事件\u已启动
  • GpsStatusChanged(24638):4//搜索修复程序
  • GpsStatusChanged(24638):4
  • GpsStatusChanged(24638):2//关闭GPS
  • GpsStatusChanged(24638):GPS事件停止

GPS关闭时的输出:无。

好的,
LocationListener
已提供:

,正是为了这个目的

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

LocationListener
提供有关提供商的信息。当您向位置提供程序注册
LocationListener
时,会相应地调用
onProviderEnabled()
/
onProviderDisabled()
,并且您的应用程序始终可以知道GPS何时打开或关闭

试试这个:

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;-)@Namenlos和
GpsStatus.Listener
不是检测提供程序设置更改的合适工具。这就是我要解释的。“GpsStatus.Listener提供有关GPS服务内部工作的信息。它不是用来告诉GPS提供商的状态。”-谢谢,这是我想听到的。课程概述让我有点困惑。