I am trying to request runtime location permissions but my promise keeps throwing this exception:
"Tried to use permissions API but the host Activity doesn't implement PermissionAwareActivity"
My code look like this:
PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION)
.then((preGranted) => {
console.log('pre-granted', preGranted)
if (!preGranted) {
PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
{ 'title': 'Enable location', 'message': 'Boss said so..' }
)
.then((granted) => {
if (granted) {
console.log('GRANTED AFTER ASKING:', granted)
debugger
}
})
}
})
In my manifest i have:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Has anyone come across that message? What is PermissionAwareActivity im having trouble finding anything about it online.
It is important that GPS is enabled as the permissions library would not enable it even if the location permissions are enabled. To enable GPS, react-native-android-location-enabler can be used.
So to ask permissions, React Native has a prebuilt feature in it which we can import and use it in our code. import { PermissionsAndroid } from 'react-native'; Before asking permissions to the user we have to declare that permissions in AndroidManifest. xml file.
Geolocation is enabled by default when you create a project with react-native init . In order to enable geolocation in the background, you need to include the 'NSLocationAlwaysUsageDescription' key in Info. plist and add location as a background mode in the 'Capabilities' tab in Xcode.
On devices before SDK version 23, the permissions are automatically granted if they appear in the manifest, so check should always result to true and request should always resolve to PermissionsAndroid. RESULTS. GRANTED .
you should implements PermissionAwareActivity in your activity, and define the Callback && PermissionListener .
public class ReactNativeIndexActivity extends AppCompatActivity implements DefaultHardwareBackBtnHandler,PermissionAwareActivity {
private ReactRootView mReactRootView;
private ReactInstanceManager mReactInstanceManager;
private @Nullable Callback mPermissionsCallback;
private @Nullable PermissionListener mPermissionListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
......
}
@Override
protected void onResume() {
super.onResume();
if (mReactInstanceManager != null) {
mReactInstanceManager.onHostResume(this, this);
}
if (mPermissionsCallback != null) {
mPermissionsCallback.invoke();
mPermissionsCallback = null;
}
}
@Override
@TargetApi(Build.VERSION_CODES.M)
public void requestPermissions(String[] permissions, int requestCode, PermissionListener listener) {
mPermissionListener = listener;
requestPermissions(permissions, requestCode);
}
@Override
public void onRequestPermissionsResult(final int requestCode, @NonNull final String[] permissions, @NonNull final int[] grantResults) {
mPermissionsCallback = new Callback() {
@Override
public void invoke(Object... args) {
if (mPermissionListener != null && mPermissionListener.onRequestPermissionsResult(requestCode, permissions, grantResults)) {
mPermissionListener = null;
}
}
};
}
PermissionAwareActivity
is an interface in a third party library.
https://github.com/wix/react-native-navigation
It is not part of the standard Android SDK.
Instructions for requesting runtime permissions using the standard SDK can be found in the documentation.
https://developer.android.com/training/permissions/requesting.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With