How to Remove App from Recent Apps in Android Programmatically
In Android, the recent apps feature allows users to quickly switch between recently used applications. However, there may be instances where you want to programmatically remove an app from the recent apps list. This can be useful for various reasons, such as cleaning up the list or preventing users from accessing certain apps. In this article, we will discuss how to remove an app from the recent apps list in Android programmatically.
Understanding the Recent Apps List
The recent apps list is a part of the Android system, and it is accessible through the System UI. The list displays the recently used apps, and users can swipe left or right to navigate through them. To remove an app from this list programmatically, you need to interact with the System UI.
Using the System UI API
To remove an app from the recent apps list programmatically, you can use the System UI API. This API provides a way to interact with the system UI components, including the recent apps list. However, it is important to note that the System UI API is only available on Android 8.0 (API level 26) and above.
Here’s a step-by-step guide on how to remove an app from the recent apps list using the System UI API:
1. Import the necessary classes:
“`java
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
“`
2. Create a new activity or fragment:
“`java
public class RecentAppsActivity extends AppCompatActivity {
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recent_apps);
// Remove an app from the recent apps list
removeAppFromRecentApps();
}
private void removeAppFromRecentApps() {
// Get the System UI controller
Activity activity = this;
SystemUIController controller = SystemUIController.getInstance(activity);
// Get the recent apps list
RecentTasks recentTasks = controller.getRecentTasks();
// Get the task to remove
ComponentName componentName = new ComponentName(this, MainActivity.class);
TaskInfo taskInfo = recentTasks.getTask(componentName);
if (taskInfo != null) {
// Remove the task from the recent apps list
recentTasks.removeTask(taskInfo.id);
Toast.makeText(this, “App removed from recent apps list”, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, “App not found in recent apps list”, Toast.LENGTH_SHORT).show();
}
}
}
“`
3. Add the necessary permissions to your AndroidManifest.xml file:
“`xml
“`
Conclusion
Removing an app from the recent apps list in Android programmatically can be achieved using the System UI API. By following the steps outlined in this article, you can interact with the system UI components and remove an app from the recent apps list. However, it is important to note that this approach is only available on Android 8.0 (API level 26) and above.