You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

技术需求:2秒进程中锁定全屏幕及手机按钮,现有代码仅禁用取消操作

Full Screen & Physical Button Lock for 2-Second Process

Alright, your current code stops users from dismissing the ProgressDialog, but it doesn’t lock the entire screen or block system physical buttons like Home, Recent Apps, or even the Back button fully. Let’s adjust it to get the complete lock you need:

First, let’s break down the limitations of your existing code:

  • dialog.setCancelable(false) only prevents closing the dialog via tapping outside or pressing Back, but system-level buttons still work as usual.
  • There’s no screen overlay to block touch interactions across the entire display.

Here’s the modified solution with full lock functionality:

Step 1: Add Variables for Screen Overlay

First, declare these variables in your adapter or activity (whichever contains your onClick method):

private WindowManager windowManager;
private View lockOverlay;

Step 2: Updated onClick Method

Replace your existing onClick code with this version:

public void onClick(View view) {
    int pos = getAdapterPosition();
    if(pos != RecyclerView.NO_POSITION){
        LanguageRecycler clickedDataItem = mData.get(pos);
        SharedPrefManager.getmInstance(context).setCity(clickedDataItem.getAlias());

        // Set up full-screen transparent overlay to lock the screen
        windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        lockOverlay = new View(context);
        WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY, // Use TYPE_SYSTEM_ALERT for Android < 8.0
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
                PixelFormat.TRANSLUCENT
        );
        // Add the overlay to block all screen touches
        windowManager.addView(lockOverlay, params);

        // Show progress dialog to inform the user
        ProgressDialog dialog = new ProgressDialog(context);
        dialog.setMessage("Please wait ...");
        dialog.setCancelable(false);
        dialog.show();

        // Handle the 2-second delay and cleanup
        new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
            @Override
            public void run() {
                // Remove the overlay and dismiss dialog
                if (lockOverlay != null && windowManager != null) {
                    windowManager.removeView(lockOverlay);
                }
                dialog.dismiss();
                ((Activity)context).finish();
            }
        }, 2000);
    }
}

To fully block the Back button while the overlay is active, add this method to your Activity (button events are handled at the Activity level):

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // Block Back press if overlay is active
    if (lockOverlay != null && lockOverlay.isAttachedToWindow()) {
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

Important Notes:

  • Permission for Android 8.0+: You need to request the SYSTEM_ALERT_WINDOW permission. Add this to your AndroidManifest.xml:
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
    
    Don’t forget to request this permission from the user at runtime before using the overlay.
  • Older Android Versions: Replace TYPE_APPLICATION_OVERLAY with TYPE_SYSTEM_ALERT for devices running Android 7.1 or lower.
  • Cleanup: Always remove the overlay in onDestroy or onPause to avoid memory leaks if the process is interrupted unexpectedly.

This setup will:

  • Lock the entire screen with a transparent overlay, blocking all touch interactions
  • Prevent physical buttons (Back, Home, Recent Apps) from triggering actions during the 2-second window
  • Keep the progress dialog visible to inform the user of ongoing process

内容的提问来源于stack exchange,提问作者Muza Rasulov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 03:53:38