16

I know that this question was asked before here Android Disable Recent Task Button like in SureLock, but since the answer there is not working, maybe some can share some light on this forgotten mater.

I also tried:

private void closeRecents() {
     activ.sendBroadcast(new Intent("com.android.systemui.recent.action.CLOSE"));
     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(SYSTEM_UI_PACKAGE_NAME, RECENTS_ACTIVITY);
     closeRecents.setComponent(recents);
     activ.startActivity(closeRecents);
}

but no luck

Community
  • 1
  • 1
Tazz
  • 781
  • 1
  • 8
  • 23
  • 1
    There should be no such possibilities, unless you bake your own Android release ... – Selvin Jun 22 '15 at 11:38
  • yes there is :). Maybe you get more informed about Locker Apps – Tazz Jun 22 '15 at 11:39
  • Then google should fix it ... especially on non rooted devices – Selvin Jun 22 '15 at 11:39
  • 1
    You think i didn't tried to search for it ? I'm stuck on this mater for a week now. I blocked home, back button and status bar. This is the only thing that stops me for locking down the app so it can work as a proper locker app – Tazz Jun 22 '15 at 11:41
  • You don't get it ... if there will be no such API for special purpose(like locker apps) every such possibilities should be threaten as bug/security hole ... and hopefully removed in update/new release of android – Selvin Jun 22 '15 at 11:52
  • have you tried ACTION_CLOSE_SYSTEM_DIALOGS ? – ligi Jun 22 '15 at 11:56
  • yes, it doesn't work :(. I tried the solution provided here: http://stackoverflow.com/questions/24013790/how-to-dismiss-system-dialog-in-android – Tazz Jun 22 '15 at 12:05
  • Why do you want to block recent apps button? you do not want to show app in recent or something else? – Abhinav Singh Maurya Jul 27 '15 at 06:46
  • @Tazz How did you block the home button? Can you share the code please? – user2511882 May 14 '19 at 16:28

6 Answers6

1

This answer helped me. It is not the best as some methods are now deprecated. It works for me (4.4.2), for now, but I too would like to find a more ideal solution.

    @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();
        }
    }
};

With this permission:

<uses-permission android:name="android.permission.GET_TASKS" />
Community
  • 1
  • 1
tallpaul
  • 1,220
  • 2
  • 13
  • 35
0

@Tazz You just cannot. Impossible.

Yes, it is frustrating, OSes are sometimes. I often encounter issues like this in needing advanced designs, here you hit a wall. OS does not provide the feature, it is out of your developer power. It is annying but t-r-u-e, you cannot. Even a bounty wont turn SDK into Magic.

Eventually AND if your app is dedicated to inhouse devices - not publications - you can change code in Android SDK - it is opensource. If you intend to publish on the Play store, it is just useless.

3pic
  • 1,188
  • 8
  • 26
  • Are you sure? I just tried it and it seems to work perfectly on all of my test devices. You can check my answer if you want to. –  Jul 27 '15 at 09:47
  • @AritraRoy "Pressing the recent apps button may turn the screen black for less than a second but will bring your activity back again". That seems to be a beautiful trick. I'd accept that. But it is not 100% okay with the SDK, it depends on which quality Tazz is targetting. – 3pic Jul 27 '15 at 09:50
  • I have tried it on several devices, 4 real devices and 2 emulators, on various Android versions, and it seems to work perfectly on all of them. Hope you will upvote the answer, if it helps. –  Jul 27 '15 at 10:22
  • Congratulations. If you did it, it works. I dont suspect any lie at all, but would you post a video please ? – 3pic Jul 27 '15 at 10:23
  • A video on all these devices? –  Jul 27 '15 at 10:26
  • @AritraRoy At least one. The more the better. Thanks a lot. – 3pic Jul 27 '15 at 10:27
  • I will see it I can do that. But it would be less time consuming for both me and you, if you just try this small code yourself. Thanks. –  Jul 27 '15 at 10:28
  • Up to you. I have upvoted your post, should be kind you post a vine. Cheers – 3pic Jul 27 '15 at 10:31
  • Hello, sry for delay, was gone on a break. The solution Aritra Roy posted is not good for me, i need something without flickering. I found that solution in another stackoverflow post, but it wasn't what i was looking for exactly. I have something similar now, but i will search for a better one. Since other Locker apps did it, there must be a better solution – Tazz Jul 28 '15 at 11:59
  • Hoho "without flickering". It seems I am right. This is not possible. Sorry @Tazz – 3pic Jul 28 '15 at 12:03
  • @Tazz: no alernative in **usages** ? Why do you absolutely want this ? You have to be creative to find another way, cause you just cannot do that. I had such questions, bountied, hoping getting the StGraal, it is loosing time. Dont spend more, get some rest and redesign it. It is certainly not the most important part of your proj. It is impossible, that's it. – 3pic Jul 28 '15 at 12:05
  • @Strukt, well if Go Locker does it, there must be such a solution :D. But yes, for now its not a priority, i just search for solution in my spare time to make an improvement. The users are always peaky – Tazz Jul 28 '15 at 12:36
  • If X does it, it is possible. ;) I said it too. But X raised 500K$ and has a 3 guys team. – 3pic Jul 28 '15 at 12:54
0

You can create a black list,and add "com.android.systemui" in it.then start a service,open infinite loop to check the TopPackageName,if that blacklist contains it,start you own activity. if is not clearly,sorry for my bad english.

and the code:

public class MyService extends Service{
private HashSet<String> blackPackages = new HashSet<String>();

@Override
public void onCreate() {
    m_black_packages.add("com.android.systemui");

    while(true){
        if(blackPackages.contains(getTopPackage(MyApplication.context)){
        //start you own activity
        }
    }
}

public String GetTopPackage(Context context) {
    int version=android.os.Build.VERSION.SDK_INT;
    if(version>=21)
    {
        long ts = System.currentTimeMillis();
        UsageStatsManager mUsageStatsManager = (UsageStatsManager) context.getSystemService("usagestats");
        List<UsageStats> usageStats = mUsageStatsManager.queryUsageStats(
                UsageStatsManager.INTERVAL_BEST, ts - 86400000,     
                ts);
        if (usageStats == null || usageStats.size() == 0) {
            return getTopPackageLegacy(context);
        }
        Collections.sort(usageStats, mRecentComp);
        return usageStats.get(0).getPackageName();
    }
    else
    {
        return getTopPackageLegacy(context);
    }
}

public String getTopPackageLegacy(Context context) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    @SuppressWarnings("deprecation")
    List<RunningTaskInfo> tasks = am.getRunningTasks(1);
    if (tasks.size() == 0) {
        return null;
    }
    RunningTaskInfo task = tasks.get(0);
    ComponentName cn = task.topActivity;
    String package_name = cn.getPackageName();
    return package_name;
}

private static class RecentUseComparator implements Comparator<UsageStats> {

    @Override
    public int compare(UsageStats lhs, UsageStats rhs) {
        return (lhs.getLastTimeUsed() > rhs.getLastTimeUsed()) ? -1
                : (lhs.getLastTimeUsed() == rhs.getLastTimeUsed()) ? 0 : 1;
    }
}

}

this is not original code,but something like that.

floatingmuseum
  • 958
  • 8
  • 12
0

An easy trick that worked for me (in a Samsung Galaxy S6) is to launch again the Activity when onStop() is called, because your Activity will call onStop() when user tap on recent apps button.

@Override
public void onStop() {
    startActivity(new Intent(context, YourActivity.class));
}

It is recommended to mark your Activity as singleInstance to avoid multiple app instances:

android:launchMode="singleInstance"
Pelanes
  • 3,451
  • 1
  • 34
  • 32
0
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    if(!hasFocus) {

        Intent closeDialog = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
        sendBroadcast(closeDialog);
    }
}
Ishan Fernando
  • 2,758
  • 1
  • 31
  • 38
  • This work alright, but all this does close the pop-up as soon as it shows. This can be bypassed by vigorous tapping. – Michael T Nov 13 '20 at 13:10
0
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);
    }
}

This is better probably but still haven't found anything close for disabling the button!