[英]Return to app after requesting SYSTEM_ALERT_WINDOW permission
My app requires permission to draw over other apps to work correctly, so at the start of the application I check for permission and, if needed, ask the user to grant it, then start the request via the following code:我的应用程序需要获得其他应用程序的权限才能正常工作,因此在应用程序开始时我检查权限,如果需要,请用户授予权限,然后通过以下代码启动请求:
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + activity.getPackageName()));
activity.startActivityForResult(intent, REQUEST_OVERLAY_PERMISSION);
This requires that the user press the back button after enabling permission, but I've noticed that some apps are able to automatically return or go to another activity after the user enables it.这要求用户在启用权限后按下后退按钮,但我注意到某些应用程序能够在用户启用后自动返回或转到另一个活动。 How is this accomplished?这是如何实现的? Is there a service listening in the background that is periodically checking to see if the user has enabled something?是否有在后台侦听的服务定期检查用户是否启用了某些功能?
It appears that there is no way to observe this setting, so there is really no elegant solution for this.似乎没有办法观察到这个设置,所以对此确实没有优雅的解决方案。
What you can do is just check the setting once per second using a Handler after you send the user to the Settings screen.您可以做的只是在您将用户发送到设置屏幕后使用处理程序每秒检查一次设置。
You can't truly "go back" programmatically from the Settings screen, so the only option is to re-launch the Activity and clear the previous ones (otherwise it will go back to the Settings screen on back press afterwards).您无法真正从“设置”屏幕以编程方式“返回”,因此唯一的选择是重新启动活动并清除以前的活动(否则它会在之后按下后返回“设置”屏幕)。
With the example below, within one second of enabling the setting, it will re-launch MainActivity.在下面的示例中,在启用设置的一秒钟内,它将重新启动 MainActivity。
First define the Runnable:首先定义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 the permission, re-launch MainActivity
Intent i = 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 the Runnable when you send the user to the Settings screen:然后在您将用户发送到设置屏幕时启动 Runnable:
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, ACTION_MANAGE_OVERLAY_PERMISSION_REQUEST_CODE);
//Add this here:
handler.postDelayed(checkOverlaySetting, 1000);
Note that you should set a timeout or max tries so that it doesn't go on forever if the user does not enable the overlay setting.请注意,您应该设置超时或最大尝试次数,这样如果用户未启用覆盖设置,它就不会永远持续下去。
Another way to go back is to keep track of the request code and then finishActivity
另一种返回方式是跟踪请求代码然后finishActivity
int REQ_CODE = 0;
public void requestPermission(){
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getContext().getPackageName()));
startActivityForResult(intent, REQ_CODE);
}
public void onPermissionChanged(){
runOnUiThread(new Runnable() {
@Override
public void run() {
finishActivity(REQ_CODE);
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.