Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
376 views
in Technique[技术] by (71.8m points)

Android - My app can get location information until opening Google Maps although it has related permissions

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.

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.

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

My code block is like the following ,

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);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...