Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.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
Java GPsStatus在GPS_事件_启动和GPS_事件_停止之间切换_Java_Android_Google Maps_Gps - Fatal编程技术网

Java GPsStatus在GPS_事件_启动和GPS_事件_停止之间切换

Java GPsStatus在GPS_事件_启动和GPS_事件_停止之间切换,java,android,google-maps,gps,Java,Android,Google Maps,Gps,我在检测GPS开关打开/关闭时遇到一些问题 我可以检测到GPS开关打开,但祝酒词总是在我的屏幕上。 我启用了“GPS” “GPS禁用” “启用GPS” “GPS禁用” “启用GPS” “GPS禁用” “启用GPS” 这是我的代码: final LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); locationManager.ad

我在检测GPS开关打开/关闭时遇到一些问题

我可以检测到GPS开关打开,但祝酒词总是在我的屏幕上。 我启用了“GPS”

“GPS禁用”

“启用GPS”

“GPS禁用”

“启用GPS”

“GPS禁用”

“启用GPS”

这是我的代码:

final LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        locationManager.addGpsStatusListener(new GpsStatus.Listener() {
            @Override
            public void onGpsStatusChanged(int event) {

                switch (event) {
                    case GpsStatus.GPS_EVENT_STARTED:
                            Toast.makeText(HomeActivity.this, "GPS enabled", Toast.LENGTH_LONG).show();
                            currentLocation = getLocation();
                            if (currentLocation != null) setUpMapIfNeeded();
                            break;
                    case GpsStatus.GPS_EVENT_STOPPED:
                            Toast.makeText(HomeActivity.this, "GPS disabled", Toast.LENGTH_LONG).show();
                        break;
                    default:
                }

            }
        });

我只是在活动中注册了一个接收者,运气要好得多。广播接收器为我工作得完美无缺

private static final String ACTION_GPS = "android.location.PROVIDERS_CHANGED";
private BroadcastReceiver yourReceiver;

@Override
public void onStop() {
    super.onStop();
    // Do not forget to unregister the receiver!!!
    if (yourReceiver != null) {
        unregisterReceiver(yourReceiver);
        yourReceiver = null;
    }

@Override
public void onResume() {
    super.onResume();
    registerReceiverGPS();



private void registerReceiverGPS() {
    if (yourReceiver == null) {
        // INTENT FILTER FOR GPS MONITORING
        final IntentFilter theFilter = new IntentFilter();
        theFilter.addAction(ACTION_GPS);
        yourReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (intent != null) {
                    String s = intent.getAction();
                    if (s != null) {
                        if (s.equals(ACTION_GPS)) {
                            checkGPS();
                        }
                    }
                }
            }
        };
        registerReceiver(yourReceiver, theFilter);
    }
}


 private void checkGPS() {
    Context context = this.getApplicationContext();
    if (context != null) {
        Button btnGPS = (Button) findViewById(R.id.btnGPS);
        if (btnGPS != null) {
            LocationManager locationManager = (LocationManager) context
                    .getSystemService(Context.LOCATION_SERVICE);
            boolean b = locationManager
                    .isProviderEnabled(LocationManager.GPS_PROVIDER);
            locationManager = null;
            if (b) {
                if (btnGPS.getVisibility() != View.GONE) {
                    btnGPS.setVisibility(View.GONE);
                }
            } else {
                if (btnGPS.getVisibility() != View.VISIBLE) {
                    btnGPS.setVisibility(View.VISIBLE);
                }
            }
        }
    }
}

我使用GPSstatuslistener获取卫星的数量。我创建一个广播接收器来检查GPS是否启用或禁用。它起作用了。Thx:)