0

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?

ibrahim
  • 573
  • 1
  • 6
  • 20

1 Answers1

0

You should not listen to it forever. It will drain the phone's battery and performance.

You might consider creating an alarm to fire the service that finds a specific app from time to time (let's say 1 or 2 hours or what's best for your use case).

When you find what you are looking for, you can cancel the alarm that fires the service.

Edit:

Despite I'm against it and I think it won't be friendly, if you really need to run it at background and do stuff at the moment the user opens the app, do a while(true) loop at your service, but don't forget to put a sleep inside it (minimum 1 second, I would say).

For alarm documentation and usage, check https://developer.android.com/training/scheduling/alarms.html#set. There are nice examples and explains everything you need to know.

Edit 2:

To run a service "forever" in the background, you should return START_STICKY at onStartCommand of the Service. For more info, there are some links:

And don't forget to start the service at system boot:

Community
  • 1
  • 1
Eduardo Herzer
  • 2,033
  • 21
  • 24
  • how lock apps works? they always pop up asking for security code when ever the user enters a secured app? do they use alarms also? thank for ur reply – ibrahim Feb 14 '17 at 11:57
  • and if possible can you provide me with a further explanation of alarms and how to implement them – ibrahim Feb 14 '17 at 12:07
  • @ibrahim I'm not sure how those kind of apps works. But check my updated answer for more details. – Eduardo Herzer Feb 14 '17 at 12:31
  • this actually worked , i put a Thread.sleep(3000) inside my code but as soon as i quit the app it stops running the code (i have placed the above code inside a runnable which is inside a service ) , it gives me "onFatalError, processing error from engine(4)" and "Could not get audio input for session 881" and "Error creating AudioRecord instance" this is so irrelevant since my project contain nothing but the service and the above code , do you have any idea ? – ibrahim Feb 14 '17 at 21:30
  • @ibrahim check my updated answer. You should basically return `START_STICKY` at `onStartCommand` – Eduardo Herzer Feb 15 '17 at 11:30