-1

We would like to know, if there is a way to block "long pressed" home button function, that opens "appswitch/multitasking" dialog.
Our app crashes, when we try to open multitasking on samsung galaxy s3 by holding the home button.
Is there some way to solve this bug ? So the multitasking dialog opens while the app is running ?
Or could you please provide mě with some code that disables this "multitasking function" ?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Petr Župka
  • 111
  • 2
  • 9
  • Could you please show the output of the LogCat where the error is? – Leon Joosse Oct 24 '14 at 14:02
  • This is not very clever and from user experience you cannot disable this functionality. This is part for android (system are handling this functionality). What you can do, is handling states in your app. Try to show some code and error from LogCat. – Risino Oct 24 '14 at 14:26
  • it just says that the application has stopped. this code sadly does not work public boolean onKeyDown(int keyCode, KeyEvent event) { if(keyCode == KeyEvent.KEYCODE_APP_SWITCH) { System.exit(o); return false; }return super.onKeyDown(keyCode, event); } – Petr Župka Oct 24 '14 at 18:24
  • Possible duplicate of [What is the keycode for multitasking/appswitch for samsung galaxy s3?](https://stackoverflow.com/questions/26554207/what-is-the-keycode-for-multitasking-appswitch-for-samsung-galaxy-s3) – Vadim Kotov Jul 05 '17 at 14:58

3 Answers3

0

If you are not creating your own ROM of Android there is no way to block Home button or even know that Home is pressed. That is done for security reason in Android.

Could you paste your piece of code and logcat error?

Jakub
  • 1
  • 1
0

There is an unknown way to achieve that:

Create a Service and make it run a background thread to catch this event.

ComponentName cm = am.getRunningTasks(1).get(0).topActivity;
String packageName = cm.getPackageName();
if (cm.getClassName().equals("com.android.systemui.recent.RecentsActivity"))
{
    handler.post(new Runnable() {
        @Override
        public void run()
        {
            try
            {
                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);
                context.startActivity(closeRecents);
            }
            catch (Throwable e)
            {
                e.printStackTrace();
            }
        }
    });
}

Basically, this will collapse the "recent tasks" when it pops.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Manitoba
  • 8,522
  • 11
  • 60
  • 122
0

Since Android Lollipop ActivityManager#getRunningTasks() method has been deprecated, yet it is still possible to use it to "block" multitasking. From its documentation: "For backwards compatibility, it will still return a small subset of its data: at least the caller's own tasks, and possibly some other tasks such as home that are known to not be sensitive."

So you can use the caller task as follows:

ComponentName cn = am.getRunningTasks(1).get(0).topActivity; //returns caller's own activity on Lollipop
if (cn != null && cn.getClassName().equals("com.example.MainActivity")) { //use fully qualified name of your running activity
        Intent intent = new Intent(context, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); //get activity keeping its stack
        context.startActivity(intent);
    }
}
Lukáš Kořán
  • 750
  • 7
  • 13