Put any application in the background in Android
-
12-12-2019 - |
Question
I know how to put any application in the foreground but I would now put any application in the background. Ie, each application i want. I searched a lot on google but I have not found a solution. Have you any idea how? I tried to display the home but it does not work :
Intent i = new Intent(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
startActivity(i);
Thank you very much in advance.
Here is my all code (its a test code), i launch map app and i want just after, puting it in backgroud:
List<ResolveInfo> pkgAppsList = retreivedAllApplicationsInstalled();
ActivityManager actMngr = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
List<RunningAppProcessInfo> runningAppProcesses = actMngr.getRunningAppProcesses();
for (RunningAppProcessInfo pi : runningAppProcesses) {
String[] testPklist = pi.pkgList;
if(pi.importance == RunningAppProcessInfo.IMPORTANCE_BACKGROUND){
for(String tp : testPklist){
if(tp.equals("com.google.android.apps.maps")){
//launch map application
Intent i = new Intent(Intent.ACTION_MAIN);
PackageManager manager = getPackageManager();
i = manager.getLaunchIntentForPackage("com.google.android.apps.maps");
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);
}
}
}
}
//try to put back all applications on displaying home
Intent back = new Intent(Intent.ACTION_MAIN);
back.addCategory(Intent.CATEGORY_HOME);
startActivity(back);
Solution
If you want your app to run in the background you should use something called a Service. You can find more information on the link. But remember that background process drain battery, so use it with care.
Hope that helps.
OTHER TIPS
I think I understand the problem in my code: I run a activity 'map', which execute and then my code below (to put in background) is never launched.......