Question

I'd like to get a running activity of my application and call one of its method (I'm inside of a service). I have found that I need something like this, but i can't resolve my problem. Is it possible?

ActivityManager am = (ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> procInfos = am.getRunningAppProcesses();
for (int j = 0; j < procInfos.size(); j++) {
    if (procInfos.get(j).processName.equals("com.android.pm")) {
        ...
    }
}
Was it helpful?

Solution

Based on your comment:

The problem is that the service is not called from the activity that i want to close. I have activity A that calls Activity B that calls service S, and i want S to close A.

this is the wrong approach. The better thing to do would be to have Activity A register a broadcast receiver that listens for a particular Intent. When the service wants A to finish, it can broadcast a specific Intent. In this way, your service is communicating implicitly with the activity without having to have a reference to it, or know if it is running.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top