Beginning in Android 6.0 (API level 23), we can ask permissions at run time. However, according to the docs, all permissions still need to be defined in AndroidManifest.xml so in APIs lower than 23, these permissions will be granted prior to installing the app.
I want to request ACCESS_FINE_LOCATION
permission only at runtime - since this is a sensitive permission, requesting it prior to installation without any context will cause a decrease in downloads.
I am targeting my app to API levels 11+, so I wonder whether it is possible not to ask for the ACCESS_FINE_LOCATION
permission in older APIs (i.e. not list it in AndroidManifest for APIs older than 23), and only request it for APIs 23+.
Update
For clarification, I want to know whether it is possible to do the following:
AndroidManifest.xml
IF API_LEVEL>=23: {
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
}
ELSE:
{
}
- You can't request a Permission which is not declared in Manifest.
- You can't change your Manifest at runtime.
What you can do is to make 1 flavor targeting API 23+ with permission declared and another flavor without the permission targeting API 23-.
I think you can do this using:
<uses-permission-sdk-23 android:name="string" android:maxSdkVersion="integer" />
You can read more about it in the official documentation: uses-permission-sdk-23
no that's not possible in AndroidManifest.xml
you can ask the permission before using the functions based on the that permission.
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
//Ask for permission
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
requestCode);
}else{
//already permitted . Do your work here..
}
now override onRequestPermissionsResult() Method to see if user permitted the request or not
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission is granted, Now you can execute function
} else {
// permission denied, Don't execute your function to avoid any crashes
}
return;
}
you don't have to check for SDK version here
for more information click here
Happy Coding :)