Showing posts with label background. Show all posts
Showing posts with label background. Show all posts

Oct 10, 2013

[android] How to bring app in background to foreground

First, you should get the list of active apps like this.
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> tasklist =am.getRunningTasks(10);
// Number of tasks you want to get

Then, you will have the active app list in  tasklist.  
moveTaskToFront () function will bring the app to foreground.
if(!tasks.isEmpty()) 
{
      int nSize = 
tasklist.size();
      for(int i = 0; i >= nSize;  i++) 
     {
            RunningTaskInfo taskinfo = tasklist.get(i);
           if(taskinfo.topActivity.getPackageName().equals("name of the package")) 
         {
                 
am.moveTaskToFront(taskinfo.id, 0);
         }
     }
}

Finally, add permissions to the manifest.

<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.REORDER_TASKS" />