为什么'locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);'无法检测到GPS棉花糖中是否打开GPS?

时间:2016-08-11 06:33:59

标签: android gps android-6.0-marshmallow locationmanager android-reactivelocation

我正在开发一款应用程序,其中需要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开启时应该执行的代码也没有被执行!

这里出了什么问题?

请告诉我。

1 个答案:

答案 0 :(得分: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
}