Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

android - Asking for permissions while using LocationManager

I've just started with Android programming and so far, I've just messed a little bit around buttons, textviews and some activities, but I would like to begin with services and GPS locations. In order to practise a little bit, I'm building a simple app that just shows the coordinates of the user. I've created my own service class with some methods, but whenever I try to implement the following one, my app crashes:

    @TargetApi(23)
public Location getLocation(){
    try{
        locationManager = (LocationManager)this.ctx.getSystemService(LOCATION_SERVICE);
        gpsActivado = locationManager.isProviderEnabled(locationManager.GPS_PROVIDER);
    }catch(Exception e){
        e.printStackTrace();
    }


    if(gpsActivado && (ctx.checkSelfPermission("ACCESS_FINE_LOCATION") == PackageManager.PERMISSION_GRANTED)){
        locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER
                ,1000*60
                ,2
                ,this);
        return locationManager.getLastKnownLocation(locationManager.GPS_PROVIDER);

    }else{
        return null;
    }

}

With some debugging messages, I know that the problem is related to checking of permissions. Whats wrong with that? Do you now any other method in order to check if I have the required permissions? For some reason, I don't like the one I'm using.

Thanks in advance :)

PS: I'm using API 16

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

With Android Marshmallow, you have to explicitly request permission from the user even if you have specified those in Manifest file. So, you have to request for location permissions this way : First you create a request code for location

public static final int LOCATION_REQUEST_CODE = 1001; //Any number

And then check if already permission is granted or not, if not then the code will request for permission which will show a native popup asking to deny/allow the location permission

if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, LOCATION_REQUEST_CODE);
        } else {
           locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER
            ,1000*60,2,this);
   Location location = locationManager.getLastKnownLocation(locationManager.GPS_PROVIDER);
        }

The above code should be written before requesting any location preferably in onCreate() of the activity. Then based on the action taken by the user on the popup , you'll get a callback where you can perform according to your requirement.

@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case LOCATION_REQUEST_CODE: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED
                        && (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
                        || ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED)) {
                  locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER
            ,1000*60,2,this);
   Location location = locationManager.getLastKnownLocation(locationManager.GPS_PROVIDER);
                }
            }
        }
    }

Also, wherever you are trying to fetch the location, you should check whether the location permission has been granted to your application or not and then fetch the location.

 if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                          locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER
            ,1000*60,2,this);
   Location location = locationManager.getLastKnownLocation(locationManager.GPS_PROVIDER);
                    }

You can request Manifest.permission.ACCESS_FINE_LOCATION or Manifest.permission.ACCESS_COARSE_LOCATION or both. It depends upon your requirement.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...