Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"gps" location provider requires ACCESS_FINE_LOCATION permission for android 6.0

Tags:

android

In the android manifest file I have added the below permissions for gps to access the location

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

I am fecthing Location Updates as below using Location Manager Class

 private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters  private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute  locationManager.requestLocationUpdates(                     LocationManager.GPS_PROVIDER,                     MIN_TIME_BW_UPDATES,                     MIN_DISTANCE_CHANGE_FOR_UPDATES,                      this                 ); 

But the above line is giving an exception stating

"gps" location provider requires ACCESS_FINE_LOCATION permission 

This thing only happens with the targetSDKVersion 23(i.e with android 6.0) and i have also tested in android 5.1 which works fine.

Please Help!!.Thanks in Advance.

like image 280
bhanu.cs Avatar asked Nov 23 '15 07:11

bhanu.cs


People also ask

What permissions are needed for location Android?

Android offers two location permissions: ACCESS_COARSE_LOCATION and ACCESS_FINE_LOCATION .

How do I give runtime permission to location in Android?

Android App Development for Beginners This example demonstrates how do I request Location permission in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

What is Android location provider?

LocationProvider is the super class for location providers. A location provider provides the information of the geographical location of the device. This information is stored in the Location class. For accessing the location based services Android provide different classes that are available in android.


2 Answers

Since 6.0 some permissions are considered as "dangerous" (FINE_LOCATION is one of them).

To protect the user, they have to be authorized at runtime, so the user know if it's related to his action.

To do this :

 ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1); 

It will show a DialogBox in which user will choose wether he autorize your app to use location or not.

Then get the user answer by using this function :

public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {     switch (requestCode) {         case 1: {             // If request is cancelled, the result arrays are empty.             if (grantResults.length > 0             && grantResults[0] == PackageManager.PERMISSION_GRANTED) {              } else {                 // permission denied, boo! Disable the                 // functionality that depends on this permission.             }         return;         }             // other 'case' lines to check for other             // permissions this app might request     } } 

If the user accept it once, then your app will remember it and you won't need to send this DialogBox anymore. Note that the user could disable it later if he decided to. Then before requesting the location, you would have to test if the permission is still granted :

public boolean checkLocationPermission() {     String permission = "android.permission.ACCESS_FINE_LOCATION";     int res = this.checkCallingOrSelfPermission(permission);     return (res == PackageManager.PERMISSION_GRANTED); } 

It's all explained on Android documentation : http://developer.android.com/training/permissions/requesting.html

like image 132
Sebastien FERRAND Avatar answered Oct 14 '22 08:10

Sebastien FERRAND


Try this code

private void marshmallowGPSPremissionCheck() {         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M                 && getActivity().checkSelfPermission(                 Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED                 && getActivity().checkSelfPermission(                 Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {             requestPermissions(                     new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,                             Manifest.permission.ACCESS_FINE_LOCATION},                     MY_PERMISSION_LOCATION);         } else {             //   gps functions.         }     } 

add this also..................

@Override     public void onRequestPermissionsResult(int requestCode,                                            String[] permissions, int[] grantResults) {         if (requestCode == MY_PERMISSION_LOCATION                 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {             //  gps functionality         }     } 
like image 43
Nivedh Avatar answered Oct 14 '22 08:10

Nivedh