My Goal is very simple. I am Developing An App killer(Hibernator). I Want to make same as "Hibernator.apk", it's Package name is "com.tafayor.hibernator". What it does that It iterate All background apps or Services. and then It Hibernate All one by One. It also ask Accessibility permissions once pr day.
My Requirements are below.
Get all background running apps or Services.(not my App).
Or A funtion to detect an app which is running in background (not my app nor in recent apps) by package name.
Completely Suspend (hibernate) app by package name.
I have tried.
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> runningAppProcessInfo = am.getRunningAppProcesses();
for (int i = 0; i < runningAppProcessInfo.size(); i++) {
if(runningAppProcessInfo.get(i).processName.equals("com.the.app.you.are.looking.for")) {
// Do your stuff here.
}
}
And
public static boolean isForeground(Context ctx, String myPackage){
ActivityManager manager = (ActivityManager) ctx.getSystemService(ACTIVITY_SERVICE);
List< ActivityManager.RunningTaskInfo > runningTaskInfo = manager.getRunningTasks(1);
ComponentName componentInfo = runningTaskInfo.get(0).topActivity;
if(componentInfo.getPackageName().equals(myPackage)) {
return true;
}
return false;
}
But it Shows only my app.
Use UsageStatsManager instead ActivityManager
or AndroidProcesses.getRunningAppProcesses()
. Add <uses-permission android:name="android.permission.PACKAGE_USAGE_STATS"/>
in your Manifest file.
Also give usage access permission to your app . Hope this answer give you a clue , use your desired logic.