Android-尽管我的应用具有相关权限,但我可以在打开Goog​​le Maps之前获取位置信息 [英] Android - My app can get location information until opening Google Maps although it has related permissions

查看:86
本文介绍了Android-尽管我的应用具有相关权限,但我可以在打开Goog​​le Maps之前获取位置信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android开发的新手.所以我的问题可能太基本了.非常遗憾.现在,我正在维护以前编写的代码.

I am new in Android development. So my question may be too basic. Sorry for this. Now I am maintaining a previously written code.

此应用程序使用电话的位置服务.在清单文件中将其写为:

This application use the location services of the phone. In the manifest file it is written:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

但是,尽管用户允许使用定位服务,但在大多数情况下,按应用程序无法直接获取位置.

But although users give permisson for using location services, in most cases by application can not get location directly.

例如,当我的应用程序无法获取位置信息时,打开Goog​​le Maps应用程序后,我的应用程序始终可以获取位置信息.然后,我在位置上没有问题.但是首先,我必须使用Google Maps进行触发.

For example while my app can not get location information, after opening Google Maps application, my application can always get the location information. Then I have no problem with location.But firstly I have to trigger with Google Maps.

这可能导致什么?为什么我的应用在打开Goog​​le地图后可以获取位置?清单文件中是否需要其他权限?

What could cause this? Why my app can get location after opening Google Maps? Do I need another permission in my manifest file?

我的代码块如下所示,

private static final int LOCATION_SERVICES_RESOLUTION_REQUEST = 9001;

private void initMainActivity() {

        int fineAccessGranted = ContextCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_FINE_LOCATION);
        int coarseAccessGranted = ContextCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_COARSE_LOCATION);
        int externalWrite = ContextCompat.checkSelfPermission(this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE);
        int externalRead = ContextCompat.checkSelfPermission(this,
                Manifest.permission.READ_EXTERNAL_STORAGE);
        int camera = ContextCompat.checkSelfPermission(this,
                Manifest.permission.CAMERA);

        ArrayList<String> permissionsNeeded = new ArrayList<String>();

        if (fineAccessGranted == PackageManager.PERMISSION_DENIED)
            permissionsNeeded.add(Manifest.permission.ACCESS_FINE_LOCATION);
        if (coarseAccessGranted == PackageManager.PERMISSION_DENIED)
            permissionsNeeded.add(Manifest.permission.ACCESS_COARSE_LOCATION);
        if (externalWrite == PackageManager.PERMISSION_DENIED)
            permissionsNeeded.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
        if (externalRead == PackageManager.PERMISSION_DENIED)
            permissionsNeeded.add(Manifest.permission.READ_EXTERNAL_STORAGE);
        if (camera == PackageManager.PERMISSION_DENIED)
            permissionsNeeded.add(Manifest.permission.CAMERA);

        if (permissionsNeeded.size() == 0) {
            this.initMainActivityAfterPermissions();
        } else {
            ActivityCompat.requestPermissions(this, permissionsNeeded.toArray(new String[0]), LOCATION_SERVICES_RESOLUTION_REQUEST);
        }
    }


public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        // super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case LOCATION_SERVICES_RESOLUTION_REQUEST: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    this.initMainActivityAfterPermissions();

                } else {

                }
                return;
            }

获取位置的代码片段;

Code fragment that gets the location;

result =  LocationServices.FusedLocationApi.getLastLocation(instance.mGoogleApiClient);

推荐答案

基本上,这意味着您尝试通过mFusedLocationClient.getLastLocation()检索位置,并且当前设备上没有位置.因此,您需要请求位置更新,例如mFusedLocationClient.requestLocationUpdates. https://developer.android.com/training/location/receive-location-updates?hl = es https://github .com/googlesamples/android-play-location

Basically it means that you try to retrieve location via mFusedLocationClient.getLastLocation() and there is no location on device at this time. So you need to request location updates , like mFusedLocationClient.requestLocationUpdates. This links will be useful https://developer.android.com/training/location/receive-location-updates?hl=es and https://github.com/googlesamples/android-play-location

这篇关于Android-尽管我的应用具有相关权限,但我可以在打开Goog​​le Maps之前获取位置信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆