0

I am working on an app in which I have to get all the views of the top most activity. This activity may or may not be of my app. I am able to get all the views from a ViewGroup(root view). Now I just want to get the root view of top most activity to do my work but I was not able to find any answers which gives me an idea to get root view of an activity from service.

I had used these methods:

View rootView = Window.DecorView.RootView;

and

Context ctx = this;
ViewGroup rootView = ((Activity)ctx).getWindow().getDecorView().findViewById(android.R.id.content);

But nothing works inside service class. It reports that getWindow() and findViewById() did not work inside android service class. So I just want to know is there any other way to do the same? Thanks In Advance...:-)

Darush
  • 11,403
  • 9
  • 62
  • 60
ankushalg
  • 523
  • 3
  • 23
  • Have a look at [this](https://stackoverflow.com/questions/23586031/calling-activity-class-method-from-service-class) ..might it help you. – Harshad Prajapati Dec 06 '17 at 10:23
  • Thank You @HarshadPrajapati . It really help me in understanding about android services...:-) But Unfortunately my problem didn't solved – ankushalg Dec 06 '17 at 13:56

1 Answers1

1

I am working on an app in which I have to get all the views of the top most activity. This activity may or may not be of my app.

Fortunately, this is not possible, for blindingly obvious privacy and security reasons.

It's also not possible from a simple programming standpoint, as the views in whatever you think "the top most activity" is are from another process. You cannot have an object from another process in yours, by definition.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thank you for your this much fast reply. – ankushalg Dec 06 '17 at 13:48
  • Thank You for your immediate reply. I just have a Question in my mind that is it cann't be done by something else like any accessibility service??? – ankushalg Dec 06 '17 at 13:52
  • @Ankush: An accessibility service has limited ability to get at some bits of information in another app's UI. It does not get the complete view hierarchy, and [Google is cracking down on apps using accessibility services for things other than accessibility](https://commonsware.com/blog/2017/11/20/accessibilityservice-bans.html). – CommonsWare Dec 06 '17 at 14:00