14

I'm building a child play application for Android. I need to disable all keys when it is in use. I have set the Application as the Home App and disabled the back key (that takes care of the Home and Back button). In order to clear the recent tasks list I've created a list of Dummy Activites which start and then finish when the application starts. The Dummy Activites look like:

public class Dummy1 extends Activity
{
  public void onCreate(Bundle paramBundle)
  {
    super.onCreate(paramBundle);
   finish();
}
}

And then in my onCreate of the application I perform:

this.pm.setComponentEnabledSetting(new ComponentName(this, Dummy1.class),   PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);

Intent localIntent1 = new Intent(this, Dummy1.class);
localIntent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(localIntent1); 

This takes care of when someone tries to hold the Home key to display the recent tasks as I create 8 of these and they're all empty so the user can't click to change apps.

Now the only button I can't seem to disable is the "Recent Tasks/Apps" button (available mainlyon HTC devices, i.e. One X, One S, etc.). This button seems to still bring up all the recent tasks (even though my dummy tasks were created) and I can't seem to find a "hook" for the event that is fired when this button is pressed?

Note: I know it's do-able since apps like ToddlerLock have done it....I just can't seem to figure it out.

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Mace
  • 141
  • 1
  • 1
  • 4
  • Recent Apps is available on all ICS and above devices assuming they implemented the 'hold to view' part. You can't disable this AFAIK. It might act like Car Dock on motorola phones where it acts like the home until you exit the app (or undock), but I don't know if that is OEM framework specific. – Mgamerz Jan 29 '13 at 02:41
  • The Home key is no problem to overwrite (you just set your app as the home app). My issue is with the Recent Tasks button, I know it's possible since many of the toddler lock apps do it. – Mace Jan 31 '13 at 04:29
  • Hi Mace, did you manage how to do it? I'm trying to add a feature which looks like Lock app. Thank you – Rafael Ruiz Muñoz Oct 12 '15 at 08:20

5 Answers5

23

This will close the RecentActivity dialog. Put it in your activity class.

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);

        if (!hasFocus) {
            windowCloseHandler.postDelayed(windowCloserRunnable, 250);
        }
    }

    private void toggleRecents() {
        Intent closeRecents = new Intent("com.android.systemui.recent.action.TOGGLE_RECENTS");
        closeRecents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
        ComponentName recents = new ComponentName("com.android.systemui", "com.android.systemui.recent.RecentsActivity");
        closeRecents.setComponent(recents);
        this.startActivity(closeRecents);
    }

    private Handler windowCloseHandler = new Handler();
    private Runnable windowCloserRunnable = new Runnable() {
        @Override
        public void run() {
            ActivityManager am = (ActivityManager)getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
            ComponentName cn = am.getRunningTasks(1).get(0).topActivity;

            if (cn != null && cn.getClassName().equals("com.android.systemui.recent.RecentsActivity")) {
                toggleRecents();
            }
        }
    }

You will need to put the following permission in your manifest.

    <uses-permission android:name="android.permission.GET_TASKS" />
mWhitley
  • 453
  • 7
  • 14
  • 1
    This is good, unless a dialog is displayed... then an user can go to another app through the revents button. – Yar May 14 '14 at 18:31
  • 1
    The android Dialog class also has an onWindowFocusChanged public method which can be overridden to the same effect. – mWhitley May 28 '14 at 23:44
7

Have you tried this code?

public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);


    Log.d("Focus debug", "Focus changed !");

if(!hasFocus) {
    Log.d("Focus debug", "Lost focus !");

    Intent closeDialog = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
    sendBroadcast(closeDialog);
}
}

From: http://www.juliencavandoli.com/how-to-disable-recent-apps-dialog-on-long-press-home-button/

halilkaya
  • 467
  • 6
  • 21
2

Actually, you cannot disable task button. When you press it, your activity calls onPause() method and you can move your task to the front in this method. See my answer for related question.

Community
  • 1
  • 1
kagkar
  • 469
  • 5
  • 15
1

To the best of my knowledge, you cannot override the behavior of the home button (like the way you can override the behavior of the BACK button) without the user confirming it. In other words, the defaults of the app that currently handles the home button -- which is whatever the current home launcher is -- would have to be cleared first.

LuxuryMode
  • 33,401
  • 34
  • 117
  • 188
0

You can disable StatusBar popups:

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    try {
        if (!hasFocus) {
            Object service = getSystemService("statusbar");
            Class<?> statusbarManager = Class.forName("android.app.StatusBarManager");
            Method collapse = statusbarManager.getMethod("collapse");
            collapse.setAccessible(true);
            collapse.invoke(service);
        }
    } catch (Exception e) {
        Log.e(TAG, "onWindowFocusChanged - " + e.getCause());
    }
}

Also you should add this permission in AndroidManifest.xml

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

Hope this helps.