In my app I want to bring the background app to foreground by knowing its package name. I know it can be done using moveTaskToFront()
method but I don't know how to implement it.I am currently testing it on Lollipop android version.Can anyone know the solution..
Asked
Active
Viewed 3,499 times
2 Answers
2
ActivityManager am = (ActivityManager) getSystemService(Activity.ACTIVITY_SERVICE);
List<RunningTaskInfo> rt = am.getRunningTasks(Integer.MAX_VALUE);
for (int i = 0; i < rt.size(); i++)
{
// bring to front
if (rt.get(i).baseActivity.toShortString().indexOf("yourproject") > -1) {
am.moveTaskToFront(rt.get(i).id, ActivityManager.MOVE_TASK_WITH_HOME);
}
}
Inside your manifest add :
<!--User Permissions-->
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.REORDER_TASKS" />

Nir Duan
- 6,164
- 4
- 24
- 38
-
If you are only interested in top activity of **your own app**: you can use `getAppTasks()`. Also look on this answer: http://stackoverflow.com/a/26451064/6028746 – Nir Duan Jul 19 '16 at 08:40
-
No,I want to move the background apps to foreground using its package name – Adarsh Jul 19 '16 at 08:58
-
Starting Android L getRunningTasks() can't give us the task information. – Alexey Ozerov Nov 27 '20 at 02:53
0
I racked my brains for a few days trying to do the same without success until I tried Intent
. Once you know the name of the package, and you know that it is in the background Intent.FLAG_ACTIVITY_REORDER_TO_FRONT
.
The code will look more or less like this:
PackageManager pck = getPackageManager();
Intent i = pck.getLaunchIntentForPackage("com.airdatatech.alibrowser");
if (pck != null && i != null) {
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);
}
You will need to add the permission to the Manifest: <uses-permission android:name="android.permission.REORDER_TASKS" />

Andre Carioca
- 1
- 1