Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
419 views
in Technique[技术] by (71.8m points)

replace - Disable home button in android toddler app?

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 android won't let you capture a home button press and essentially disable it, when parents give the phone to their child to play with unattended (brave parent), the child can inadvertenly exit the app and then make calls or otherwise tweak the phone.

There are two other apps that currently have a psuedo fix for this issue. The apps are Toddler Lock and ToddlePhone. I've tried contacting the developers of these apps for some guidance but they haven't been willing to disclose anything, which if fine, but does anyone here have any suggestions?

It looks like both of those other apps are acting like a home screen replacement app. When you enable the "childproof mode" on those apps the user is prompted to chose and app for the action and the choices are "Launcher, LauncherPro, etc." plus the toddler app. You then have to make the toddler app the default and voila, the phone is "locked" and can only be "unlocked" using a key combination or touching the four corners of the screen, etc. when you "unlock" the phone. your normal home screen app default restored. You don't even have to make the toddler app the default the next time you enable the "childproof mode".

I have read that these two apps have problems with Samsung phones and they can cause an an infinite crash-and-restart-loop that requires a factory reset to fix. Obviously this is not the ideal solution to the problem but it looks like the only one availiable at this point.

Does anyone have any ideas on how to implement a "childproof mode"?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#d0000000"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center_horizontal"
        android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="12dp"
        android:text="@string/app_name"
        android:textColor="#fff"
        android:textSize="18sp"
        android:textStyle="bold" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Locked mode is on"
        android:textColor="#fff"
        android:textSize="18sp" />

    </LinearLayout>

</FrameLayout>

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

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...