android - Return to app after requesting SYSTEM_ALERT_WINDOW permission -


my app requires permission draw on other apps work correctly, @ start of application check permission and, if needed, ask user grant it, start request via following code:

    intent intent = new intent(settings.action_manage_overlay_permission, uri.parse("package:" + activity.getpackagename()));     activity.startactivityforresult(intent, request_overlay_permission); 

this requires user press button after enabling permission, i've noticed apps able automatically return or go activity after user enables it. how accomplished? there service listening in background periodically checking see if user has enabled something?

it appears there no way observe setting, there no elegant solution this.

what can check setting once per second using handler after send user settings screen.

you can't "go back" programmatically settings screen, option re-launch activity , clear previous ones (otherwise go settings screen on press afterwards).

with example below, within 1 second of enabling setting, re-launch mainactivity.

first define runnable:

handler handler = new handler(); runnable checkoverlaysetting = new runnable() {     @override     @targetapi(23)     public void run() {         if (build.version.sdk_int < build.version_codes.m) {             return;         }         if (settings.candrawoverlays(mainactivity.this)) {             //you have permission, re-launch mainactivity             intent = new intent(mainactivity.this, mainactivity.class);             i.addflags(intent.flag_activity_new_task | intent.flag_activity_clear_task);             startactivity(i);             return;         }         handler.postdelayed(this, 1000);     } }; 

then start runnable when send user settings screen:

intent intent = new intent(settings.action_manage_overlay_permission,                     uri.parse("package:" + getpackagename())); startactivityforresult(intent, action_manage_overlay_permission_request_code); //add here: handler.postdelayed(checkoverlaysetting, 1000); 

note should set timeout or max tries doesn't go on forever if user not enable overlay setting.


Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -