即使在android marshmallow中请求许可后检索位置仍未执行的代码

时间:2016-08-04 13:03:15

标签: android android-6.0-marshmallow android-permissions runtime-permissions android-reactivelocation

我尝试使用this库检索用户的当前位置。

代码在android M下面的版本中成功获取位置,但是当我在使用Android Marshmallow的设备上启动应用程序时,这段代码甚至没有被执行!

我已经在onCreate()方法中编写了这段代码:

ReactiveLocationProvider locationProvider = new ReactiveLocationProvider(getBaseContext());
locationProvider.getLastKnownLocation()
        .subscribe(new Action1<Location>() {
            @Override
            public void call(Location location) {
                currentLatDouble = location.getLatitude();
                currentLngDouble = location.getLongitude();
                Toast.makeText(getBaseContext(), "User's location retrieved using library", Toast.LENGTH_SHORT).show();
            }
        });

在Android marshmallow设备上运行应用程序时,我没有得到这个toast:Toast.makeText(getBaseContext(), "User's location retrieved using library", Toast.LENGTH_SHORT).show();这意味着此代码没有被执行!

在编写此代码之前,我已使用以下代码请求许可:

int hasWriteContactsPermission = ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
        & ContextCompat.checkSelfPermission(MainActivity.this, android.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;
    }
}

此处onRequestPermissionsResult()的代码:

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    switch (requestCode) {
        case REQUEST_RUNTIME_PERMISSION:
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // Permission Granted
                ReactiveLocationProvider locationProvider = new ReactiveLocationProvider(getBaseContext());
                locationProvider.getLastKnownLocation()
                        .subscribe(new Action1<Location>() {
                            @Override
                            public void call(Location location) {
                                currentLatDouble = location.getLatitude();
                                currentLngDouble = location.getLongitude();
                                Toast.makeText(getBaseContext(), "User's location retrieved using library", Toast.LENGTH_SHORT).show();
                            }
                        });
                Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (currentLatDouble != null || currentLngDouble != null ) {
                            retrieveHRequests();
                            retrieveUserCounterA();
                            retrieveUserCounterP();
                        } else {
                            ifLocationAvailable();
                        }
                    }
                }, 2000);
            } else {
                // Permission Denied
                Toast.makeText(MainActivity.this, "Permission denied", Toast.LENGTH_SHORT)
                        .show();
                //helpRequestsLoadingDialog.dismiss();
                progressBarLoadingRequests.setVisibility(View.INVISIBLE);

                ReactiveLocationProvider locationProvider = new ReactiveLocationProvider(getBaseContext());
                locationProvider.getLastKnownLocation()
                        .subscribe(new Action1<Location>() {
                            @Override
                            public void call(Location location) {
                                currentLatDouble = location.getLatitude();
                                currentLngDouble = location.getLongitude();
                                Toast.makeText(getBaseContext(), "User's location retrieved using library", Toast.LENGTH_SHORT).show();
                            }
                        });
                Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (currentLatDouble != null || currentLngDouble != null ) {
                            retrieveHRequestWrapper();
                            retrieveUserInfo();
                        } else {
                            ifLocationAvailable();
                        }
                    }
                }, 2000);
            }
            break;
        default:
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

我已经在onRequestPermissionResult() PERMISSION_GRANTED案例中编写了位置检索代码,但没有发生任何事情,也没有在android marshmallow版本中检索到位置!

请告诉我这里出了什么问题!

0 个答案:

没有答案