Avi
Avi

Reputation: 866

Android - programmatically take a screenshot without having reference to the view / activity

There are many examples of taking a screenshot using a reference to the current activity. However, in my case, I need to take a screenshot of an activity which comes from an external SDK (but still within my app). For obvious reasons I don't have a reference to that activity object within my code. I saw solutions using Instrumentation and UiDevice, but they seem to work only when implementing a testing application using the TestCase framework, while I need it to work in a normal application.

Is there another way?

Upvotes: 3

Views: 194

Answers (1)

Avi
Avi

Reputation: 866

Well, if anyone finds this question useful, here's the answer I found: In your activity or service, call getApplication().registerActivityLifecycleCallbacks(), passing it an object that implements the Application.ActivityLifecycleCallbacks interface. That object will get a callback on every activity's onCreate/onStart etc. with a reference to the Activity object, from which you can get the view and take a screenshot. You can identify that it's an activity from an external SDK by its package name.

NOTE: Application.ActivityLifecycleCallbacks is only available since API level 14 (a.k.a. ICS).

Upvotes: 3

Related Questions