0

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

Adarsh
  • 105
  • 3
  • 14

2 Answers2

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
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" />