Reputation: 401
I am trying to get last know location for user. To do this, I have already declared the following permissions over at the manifest xml file.
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
But over at the code, it keeps showing following error:
Code should explicitly check to see if permission is available.
I didn't see the point of this but I added the following check anyway.
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
This stops the error from showing but when I run the application, it ends up returning. What permission am I missing? Been awhile since I worked on Android and I don't recall having to handle this additional checks. Manifest permission declarations were sufficient. Please advice what I am missing.
Code:
LocationManager locationManager;
String provider;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
provider = locationManager.getBestProvider(new Criteria(), false);
Location location = null;
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
Log.i("location", "returned"); // This log will occur
return; //return will occur
}
location = locationManager.getLastKnownLocation(provider); //this is the line that requires permission
Log.i("location", "This log will not occur");
}
Upvotes: -1
Views: 501
Reputation: 2845
In Android 6.0, permission granting and access changed. Adding permissions in the manifest no longer guarantees access. The user now has the right to revoke and grant you permissions when he chooses.
You need to request explicitly from the user for some of the permissions. The user can choose to give you the permission and then you continue your work. The user can also choose to deny the request and then you must figure out how to handle the situation.
A permission granted isn't final. The user can still revoke the permission later in the future so you must keep track of your permissions. If a permission is critical to the performance of your application, help the user understand that the permission is imperative for the app to function properly (by using a dialog/popup) when requesting the permission.
For better comprehension read this
What you've done is checking if you have the necessary permission. Instead of breaking out of the method, request for the permission
Upvotes: 3