Answer the question
In order to leave comments, you need to log in
How to get the name of the running application?
I am writing a service, it needs to get the name of the application that is running on the device, how can this be implemented?
Answer the question
In order to leave comments, you need to log in
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List runningAppProcessInfo = am.getRunningAppProcesses();
for (int i = 0; i < 10 /*runningAppProcessInfo.size()*/; i++) {
Log.i("process"," process number "+i +" name "+runningAppProcessInfo.get(i).processName );
}
If you test this code, you can see that the zero element of runningAppProcessInfo will be the name of the process that belongs to the application that is active at the given time.
Run a bash command ps
(run the ps application), get a list of processes, then get the name of the package, though the presence of a process does not mean that the application is running, and even more so that one of the Activities is open, but if there is no process, then it is definitely not running, there are other ways,
google: android java get all working applications
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question