Disable home button in android toddler app?

橙三吉。 提交于 2019-11-27 01:04:33

I think you're right regarding the home screen replacement. Toddler Lock I know doesn't override the home button, because (at least on my LG GW620) while in Toddle Lock holding the home button brings up the ALT-TAB type menu - which then tends to crash the phone.

There is a home screen replacement app available, with source code, on the android dev site:

http://developer.android.com/resources/samples/Home/index.html

EDIT: also, ADW.Launcher:

http://code.google.com/p/adw-launcher-android/

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" />
Venkat
Add in to your Main Activity
@Override 
    public void onAttachedToWindow()
    {  
        this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
        super.onAttachedToWindow();  
    }

And Override Key down event

@Override 
    public boolean onKeyDown(int iKeyCode, KeyEvent event)
    {

        if(iKeyCode == KeyEvent.KEYCODE_BACK || iKeyCode == KeyEvent.KEYCODE_HOME) 
        {
            return true;
        }
       }

Edit: This works in all older version of android. But will not work in ICS and jelly bean and will give you crash in app

Venkat

I replaced the Default Home launcher using the following code:

Intent selector = new Intent("android.intent.action.MAIN");
selector.addCategory("android.intent.category.HOME");
selector.setComponent(new ComponentName("android","com.android.internal.app.ResolverActivity"));
startActivity(selector);

For versions 4.0 and above you can avoid Android security restrictions and set your app as a launcher. Add this to your manifest file:

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

 <activity
    android:launchMode="singleInstance"
    android:name=".MainActivity"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.HOME" />
    </intent-filter>
</activity>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!