How to get the current focus viewer in a service?

How can I get the current focus in my service.

I found several ways to do this, such as Activity.getCurrentFocus () and Windows.getCurrentFocus (). But these methods are used in Activities.

If I want to use them, I have to get the current focus activity. I tried using WindowManager to complete the current task. Thus, he received only the current name of the class Activity. not an activity object.

List<RunningAppProcessInfo> rList =activityManager.getRunningAppProcesses(); RunningTaskInfo rt = rtList.get(0); rt.topActivity.getClassName(); // the class name of the current Activity 

How can I use these methods to get the current focus in Service? Or is there another way to do it?

+6
source share
1 answer

A service is a background job, so it is not tied to any user interface. What you can do is associate your service with an Activity and call some method (via the interface) for this action to return a focused view of this action.

In any case, can you better explain what you want to achieve? usually the service is not used to do something with the user interface, so there may be a better way to do what you want.

0
source

Source: https://habr.com/ru/post/924722/


All Articles