Disable home button in android toddler app?

后端 未结 5 966
夕颜
夕颜 2020-11-29 04:07

I\'ve developed and app that is a slide show of pictures which each play a sound when you tap them. It\'s like a picture book for ages 2-4.

The problem is, since and

5条回答
  •  -上瘾入骨i
    2020-11-29 04:46

    I needed to have toddler lock in a new app, and did not want to use a launcher. Here is what I did, you can see the app at https://play.google.com/store/apps/details?id=com.justforkids.animalsounds

    1. When lock is activated, start a service, and stop it when lock is deactivated
    2. The service checks the top running app, and if it is not my activity, the service launches my activity
    3. There was still an issue that when the user clicks "home", it takes about 6 seconds before my activity is launched again. I assume this is a security feature in Android but not sure. To bypass this, when the service detects that another app is visible, it adds a top view (as an alert window) that covers the home screen for the few seconds it takes the app to re-launch.

    For step 3, here are more details:

    Create the overlay layout, for example file locked_overlay.xml:

    
    
        
    
        
    
        
    
        
    
    
    

    In your service to show or hide the overlay use:

      private View lockedOverlay = null;
    
      private void hideLockedOverlay() {
        if (lockedOverlay != null) {
          WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
          windowManager.removeView(lockedOverlay);
          lockedOverlay = null;
        }
      }
    
      private void showLockedOverlay() {
        if (lockedOverlay != null) {
          return;
        }
    
        WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
        WindowManager.LayoutParams viewLayoutParams = new WindowManager.LayoutParams(
        LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,
        WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, 
        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
            | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
            PixelFormat.TRANSLUCENT);
        viewLayoutParams.gravity = Gravity.TOP | Gravity.LEFT;
    
        LayoutInflater inflater = LayoutInflater.from(this);
        lockedOverlay = inflater.inflate(R.layout.locked_overlay, null);
        windowManager.addView(lockedOverlay, viewLayoutParams);
      }
    

    You will need the permission

    
    

提交回复
热议问题