0

I need to open overlay setting window for my app. Of course, my manifest file use SYSTEM_ALERT_WINDOW permission already.

public class MainActivity extends ReactActivity {
private static final int CODE_DRAW_OVER_OTHER_APP_PERMISSION = 2084;

@Override
protected String getMainComponentName() {
    return "MyToolbox";
}

@Override
protected ReactActivityDelegate createReactActivityDelegate() {
    return new ReactActivityDelegate(this, getMainComponentName()) {
        @Override
        protected ReactRootView createRootView() {
            return new RNGestureHandlerEnabledRootView(MainActivity.this);
        }
    };
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requireDrawOverlayPermission();
}

//Ask draw overlay permission
void requireDrawOverlayPermission() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !Settings.canDrawOverlays(this)) {
        Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package: " + getPackageName()));
        startActivityForResult(intent, CODE_DRAW_OVER_OTHER_APP_PERMISSION);
    } else {
        //TODO: Permission granted
    }
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CODE_DRAW_OVER_OTHER_APP_PERMISSION) {
        if (Settings.canDrawOverlays(this)) {
            //TODO: Permission granted
        } else {
            Toast.makeText(this, "Draw over the app is not available", Toast.LENGTH_SHORT).show();
        }
    } else {
        super.onActivityResult(requestCode, resultCode, data);
    }
}

}

The first debug run from my Android Studio, my app got crashed. And next time, it's not crash, but it doesn't show overlay setting window for me. I can see a screen is flashed but close immediately. What wrong in my code? Thank you!

Ngo Kim Huynh
  • 69
  • 3
  • 16

1 Answers1

0

Finally, I figured it out afterall. That settings window need package name to know which app require this permission. And the uri can't parse my package name because I put a space between "package:" and "getPackageName()". I removed it as below, and everything works fine!

Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName()));
Ngo Kim Huynh
  • 69
  • 3
  • 16
  • Ngo Kim Huynh Let me ask you, is it possible for a react-native UI to be run as system_alert_window? I'm researching this but finding it pretty hard to find this info. I want to know if I'll have to do the app with react but fall back to native for an overlay. – Breno Salgado Apr 25 '19 at 04:23
  • 1
    @BrenoSalgado I am understanding that you want to open system_alert_window by react code, without native code, right? I did the same thing as you, and got no answer. Then I have to go back with native code. So sad :( – Ngo Kim Huynh May 14 '19 at 03:43
  • more specifically if I could do the alert_window UI work w/ react native, but I expected it wouldn't be possible :) thanks for the info! – Breno Salgado May 14 '19 at 17:00