0

我正在开发一个应用程序,其中需要 GPS 来检测用户的当前位置并继续进行。所以,我正在使用这个代码:isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);检测 GPS 是否打开,如果它关闭,则提示用户打开它。这段代码可以完美地检测它是否关闭,但是当我打开 GPS 并尝试进一步进行时。上面的代码未能检测到 GPS 已打开,并再次提示用户打开 GPS,即使它已打开。

以下是我在打开 GPS 后尝试进一步进行的方式:

public void btnRetryGpsEvents() {

        if (isGPSEnabled) {
            ReactiveLocationProvider locationProvider = new ReactiveLocationProvider(getBaseContext());
            locationProvider.getLastKnownLocation()
                    .subscribe(new Action1<Location>() {
                        @Override
                        public void call(Location location) {
                            currentLatDouble = location.getLatitude();
                            currentLngDouble = location.getLongitude();

                        }
                    });

            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (currentLatDouble != null || currentLngDouble != null) {
                            retrieveHWrapper();
                            btnRetryGps.setVisibility(View.INVISIBLE);
                            gps_off.setVisibility(View.INVISIBLE);
                            Toast.makeText(getBaseContext(), "Loading h-requests...", Toast.LENGTH_LONG).show();
                        } else {
                            gps_off.setVisibility(View.VISIBLE);
                            progressBarLoadingRequests.setVisibility(View.INVISIBLE);
                            btnRetryGps.setVisibility(View.VISIBLE);
                            btnRetryGps.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    btnRetryGpsEvents();
                                }
                            });
                        }
                }
            }, 2000);
        } else {
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);

            // Setting Dialog Title
            alertDialog.setTitle("GPS settings");

            // Setting Dialog Message
            alertDialog.setMessage("GPS is not enabled. Enable it in settings menu.");

            // On pressing Settings button
            alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                    startActivity(intent);
                }
            });

            // on pressing cancel button
            alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });

            // Showing Alert Message
            alertDialog.show();

            gps_off.setVisibility(View.VISIBLE);
            progressBarLoadingRequests.setVisibility(View.INVISIBLE);
            btnRetryGps.setVisibility(View.VISIBLE);
            btnRetryGps.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    btnRetryGpsEvents();
                }
            });
        }
    }

我已经在 onCreate() 方法中请求了许可:

int hasWriteContactsPermission = ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) & ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) & ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) & ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_SETTINGS);
        if (hasWriteContactsPermission != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.checkSelfPermission(getBaseContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getBaseContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getBaseContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getBaseContext(), Manifest.permission.WRITE_SETTINGS) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
                if (!ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) && !ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) && !ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) && !ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.WRITE_SETTINGS)) {
                    showMessageOKCancel("You need to allow access to few permissions so that the app can work as expected.",
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.WRITE_SETTINGS},
                                            REQUEST_RUNTIME_PERMISSION);
                                }
                            });
                    return;
                }
                ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.WRITE_SETTINGS},
                        REQUEST_RUNTIME_PERMISSION);
                return;
            }
        }

所以,最重要的是,即使打开 GPS 并单击“重试”按钮,在 GPS 开启时应该执行的代码也没有被执行!

这里出了什么问题?

请告诉我。

4

1 回答 1

1

也许您的布尔值仍然返回 false,因为您没有执行重新检查。您可以通过替换重试函数中的条件来做到这一点isGPSEnabled,我假设它仍然返回 true。这样做:

public void btnRetryGpsEvents() {

    if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
        ReactiveLocationProvider locationProvider = new ReactiveLocationProvider(getBaseContext());
        locationProvid//... REST OF YOUR CODE ...//

顺便说一句,您应该知道isProviderEnabled标志只会在打开 GPS 后一段时间返回 true,而不是立即返回。这是因为设备仍然必须连接到 GPS 卫星或网络,才能指示提供商已启用。因此,建议在打开设备的 GPS 后检查并重新检查服务可用性,直到您获得连接。此外,我将其用于初始检查:

LocationManager locationManager;
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
if (!locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
  // TODO Execute code if provider is not available. Also check for network connection
}
于 2016-08-11T06:56:20.347 回答