IntegrationWithExsistingApps Android Update

Summary:
Added permission code to handle the user permission accepted or denied cases which causes potential app crashing if not handled. Only a small piece of code added to the guide to resolve an issue I was facing with permission.

The app that I was testing following the guide available, it was crashing showing permission denied error on the android monitor, and there was no step for how to resolve the issue. So I tried out a solution so thought would share it on the official Guide to help people who are stuck with the same issue.
Closes https://github.com/facebook/react-native/pull/11687

Differential Revision: D4377882

Pulled By: ericvicenti

fbshipit-source-id: a7db104c7c1b7e1fb2590c83118715840c17927b
This commit is contained in:
Eric Vicenti 2017-01-03 11:19:44 -08:00 committed by Facebook Github Bot
parent 05aef26893
commit 257794762e
1 changed files with 29 additions and 2 deletions

View File

@ -726,9 +726,36 @@ public boolean onKeyUp(int keyCode, KeyEvent event) {
}
```
That's it, your activity is ready to run some JavaScript code.
Now your activity is ready to run some JavaScript code.
> If your app is targeting the Android `api level 23` or greater, make sure you have, for the development build, the `overlay permission` enabled. You can check it with `Settings.canDrawOverlays(this);`. This is required because, if your app produces an error in the react native component, the error view is displayed above all the other windows. Due to the new permissions system, introduced in the api level 23, the user needs to approve it.
### Configure permissions for development error overlay
If your app is targeting the Android `API level 23` or greater, make sure you have the `overlay` permission enabled for the development build. You can check it with `Settings.canDrawOverlays(this);`. This is required in dev builds because react native development errors must be displayed above all the other windows. Due to the new permissions system introduced in the API level 23, the user needs to approve it. This can be acheived by adding the following code to the Activity file in the onCreate() method. OVERLAY_PERMISSION_REQ_CODE is a field of the class which would be responsible for passing the result back to the Activity.
```java
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!Settings.canDrawOverlays(this)) {
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, OVERLAY_PERMISSION_REQ_CODE);
}
}
```
Finally, the `onActivityResult()` method (as shown in the code below) has to be overridden to handle the permission Accepted or Denied cases for consistent UX.
```java
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == OVERLAY_PERMISSION_REQ_CODE) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!Settings.canDrawOverlays(this)) {
// SYSTEM_ALERT_WINDOW permission not granted...
}
}
}
}
```
## Run your app