I\'m attempting to request ACCESS_FINE_LOCATION
permissions in order to get the user\'s current location.
My logging indicates that my app does not curr
After stripping out my class completely, and it still not working, I realised that this Activity is being instantiated using a TabHost.
When I stop using the TabHost, the prompt is displayed successfully. I guess TabHosts are not supported by the new permissions prompts - is this a bug?
Same problem as App requests aren't showing up
I ended up creating a PermissionsRequestActivity which handles the permission request and response on behalf of my TabHost, then exits (pass the requested permission information in through the Intent extras Bundle).
It passes back the response to the request as a Broadcast, which is picked up by my TabHost.
Bit of a hack but works OK!
I've faced the same problem on a project which use TabHost. Basis on @Robin solution, I use the EventBus library for send a message from child activity to the TabActity.
EventBus : https://github.com/greenrobot/EventBus
Create an event object :
public class MessageEvent {
private String message;
public MessageEvent(String message){
this.message = message;
}
public String getMessage(){
return this.message;
}
}
In your main Activity :
private EventBus eventBus = EventBus.getDefault();
@Override
protected void onCreate(Bundle savedInstanceState) {
eventBus.register(this);
}
@Override
protected void onDestroy() {
eventBus.unregister(this);
super.onDestroy();
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(MessageEvent event) {
if (event.getMessage().equals("contacts")){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(android.Manifest.permission.WRITE_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainPage.this,new String[]{android.Manifest.permission.WRITE_CONTACTS}, 100 );
}
}
};
Set a different message for the permission your want to request. In your child activity you can than post the adequate message :
EventBus.getDefault().post(new MessageEvent("contacts"));
Be aware of onRequestPermissionsResult callback and the request code ;)! It will only work in the main activity.
Check that you have already added the requested permission in Android's manifest file like before Android M, only then you will get expected behaviour.
Add the permission to your manifest so you can request it via ActivityCompat.requestPermissions:
<uses-permission android:name="android.permission. ACCESS_FINE_LOCATION" />
I will share the code that works for me. In the protected void onCreate(Bundle savedInstanceState) {} method of my activity where I want to see the prompt, I included this code:
/* Check whether the app has the ACCESS_FINE_LOCATION permission and whether the app op that corresponds to
* this permission is allowed. The return value is an int: The permission check result which is either
* PERMISSION_GRANTED or PERMISSION_DENIED or PERMISSION_DENIED_APP_OP.
* Source: https://developer.android.com/reference/android/support/v4/content/PermissionChecker.html
* While testing, the return value is -1 when the "Your location" permission for the App is OFF, and 1 when it is ON.
*/
int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
// The "Your location" permission for the App is OFF.
if (permissionCheck == -1){
/* This message will appear: "Allow [Name of my App] to access this device's location?"
* "[Name of my Activity]._instance" is the activity.
*/
ActivityCompat.requestPermissions([Name of my Activity]._instance, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE_ACCESS_FINE_LOCATION);
}else{
// The "Your location" permission for the App is ON.
if (permissionCheck == 0){
}
}
Before the protected void onCreate(Bundle savedInstanceState) {} method, I created the following constant and method:
public static final int REQUEST_CODE_ACCESS_FINE_LOCATION = 1; // For implementation of permission requests for Android 6.0 with API Level 23.
// Code from "Handle the permissions request response" at https://developer.android.com/training/permissions/requesting.html.
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE_ACCESS_FINE_LOCATION: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// location-related task you need to do.
} 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
}
}