Block/disable recent apps button

49.6k views Asked by At

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

6

There are 6 answers

2
tallpaul On

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" />
1
Pelanes On

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"
0
Dhaval Kriplani On
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!

13
3pic On

@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.

1
Ishan Fernando On
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    if(!hasFocus) {

        Intent closeDialog = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
        sendBroadcast(closeDialog);
    }
}
1
floatingmuseum On

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.