so i got this code that gets other applications package name
ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> runningAppProcessInfos = activityManager.getRunningAppProcesses();
for (int i = 0; i < runningAppProcessInfos.size(); i++) {
ActivityManager.RunningAppProcessInfo runningAppProcessInfo = runningAppProcessInfos.get(i);
if (runningAppProcessInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
packagename= runningAppProcessInfo.pkgList[0];
}
}
return packagename;
and i have placed it inside a Service's onStartCommand method using a thread , and from my main activity i started the service with an intent.Now the problem is that the code will only be executed once and that when the app is launched
i need it to be always running in the background until it finds a specific app's package name (facebook or any other app for example) how can i do that?