如何同时显示权限对话框与Toast并规避屏幕覆盖问题?
Got it, let's tackle this Screen Overlay problem while keeping both your Toast message and permission request intact. The core issue here is that the Toast and system permission dialog are trying to render at nearly the same time, which triggers overlay detection on certain Samsung devices running API 23+. Here are a few reliable, practical fixes you can implement:
1. Delay Permission Request with a Fixed Timeout
The simplest approach is to give the Toast enough time to display and disappear before triggering the permission check. Since Toast.LENGTH_SHORT typically lasts around 2 seconds, adding a 2000ms delay works well for most cases:
startButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(MainActivity.this, "Please, login to start.", Toast.LENGTH_SHORT).show(); // Delay permission check to let Toast finish new Handler(Looper.getMainLooper()).postDelayed(new Runnable() { @Override public void run() { checkPermissionAndStartLogin(); } }, 2000); // Matches Toast.LENGTH_SHORT's standard duration } });
You can tweak the delay slightly (e.g., 1800ms) if you want the permission dialog to appear a bit sooner without causing conflicts.
2. Trigger Permission Check After Toast is Dismissed (More Accurate)
Instead of relying on a fixed delay, you can listen for when the Toast's view is detached from the window—this ensures the Toast has fully disappeared before launching the permission request. This is more reliable across devices that might have slightly different Toast durations:
startButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast toast = Toast.makeText(MainActivity.this, "Please, login to start.", Toast.LENGTH_SHORT); // Listen for when Toast is dismissed toast.getView().addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() { @Override public void onViewAttachedToWindow(View v) { // No action needed here—just confirming Toast is visible } @Override public void onViewDetachedFromWindow(View v) { // Toast is gone, now start permission flow checkPermissionAndStartLogin(); } }); toast.show(); } });
3. Optimize Flow for Already Granted Permissions
If the user has already granted the required permission, there's no need to delay anything—you can skip the permission request and jump straight to the login flow. This improves user experience by avoiding unnecessary waits:
startButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(MainActivity.this, "Please, login to start.", Toast.LENGTH_SHORT).show(); String requiredPermission = Manifest.permission.YOUR_TARGET_PERMISSION; // Replace with your permission if (ContextCompat.checkSelfPermission(MainActivity.this, requiredPermission) != PackageManager.PERMISSION_GRANTED) { // Permission not granted—delay request new Handler(Looper.getMainLooper()).postDelayed(() -> checkPermissionAndStartLogin(), 2000); } else { // Permission already granted—start login immediately startLoginProcess(); // Replace with your actual login method } } });
Recommendation
I’d suggest going with the second method (listening for Toast detachment) since it adapts to device-specific Toast behavior and eliminates the guesswork of fixed delays. It’s the most robust solution for avoiding Screen Overlay conflicts while keeping both your Toast and permission functionality intact.
内容的提问来源于stack exchange,提问作者Yamashiro Rion

