Effective Screenshot Capture in Android Black‑Box Testing Using ActivityController and ProcessObserver

This article explains how to implement reliable screen‑capture during Android black‑box testing by leveraging ActivityController, custom Shell services, IProcessObserver, and optional root‑level getevent commands, providing code examples and an integrated workflow for detecting page changes and handling crashes.

360 Quality & Efficiency
360 Quality & Efficiency
360 Quality & Efficiency
Effective Screenshot Capture in Android Black‑Box Testing Using ActivityController and ProcessObserver

The article introduces common challenges in Android black‑box testing for capturing useful screenshots and proposes three main approaches: using third‑party screenshot services, implementing a custom Shell service started via app_process, and employing ActivityController to monitor activity changes.

It recommends a custom Shell service because it continues running even if the test script crashes and works on Android R and later where uiautomator 1.0 is no longer supported. The service records screenshots on activity changes, process state changes, and app crashes or ANRs.

To capture activity changes, the article shows how to register an IActivityController implementation:

IActivityController mActivityController = new IActivityController.Stub() {<br/>    @Override<br/>    public boolean activityStarting(Intent intent, String pkg) throws RemoteException { return true; }<br/>    @Override<br/>    public boolean activityResuming(String pkg) throws RemoteException { return true; }<br/>    @Override<br/>    public boolean appCrashed(String processName, int pid, String shortMsg, String longMsg, long timeMillis, String stackTrace) throws RemoteException { return false; }<br/>    @Override<br/>    public int appEarlyNotResponding(String s, int i, String s1) throws RemoteException { return 0; }<br/>    @Override<br/>    public int appNotResponding(String processName, int pid, String processStats) throws RemoteException { return -1; }<br/>    @Override<br/>    public int systemNotResponding(String msg) throws RemoteException { return 0; }<br/>};

The article also discusses drawbacks of ActivityController, such as interference when multiple processes register the service and the inability to detect fragment switches.

To complement ActivityController, it introduces IProcessObserver for monitoring process lifecycle events, providing code:

IProcessObserver mProcessObserver = new IProcessObserver.Stub() {<br/>    @Override<br/>    public void onForegroundActivitiesChanged(int pid, int uid, boolean foregroundActivities) throws RemoteException { /* handle */ }<br/>    @Override<br/>    public void onProcessStateChanged(int pid, int uid, int importance) throws RemoteException { }<br/>    @Override<br/>    public void onProcessDied(int pid, int uid) throws RemoteException { }<br/>};

Both services are combined into an integrated workflow: ActivityController detects page changes, IProcessObserver validates them, a timer captures screenshots every 2 seconds, compares the new bitmap with the previous one using image similarity (threshold 0.8), and re‑registers the ActivityController if no updates occur within 5 seconds.

For rooted devices, the article presents a special method using the Linux getevent command to read raw touch events (e.g., ABS_MT_POSITION_X/Y, BTN_TOUCH) and derive click or swipe actions, which can then trigger screenshot capture and optionally draw the event trajectory on the image.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Shellscreenshotblack-box testingactivitycontrollerprocessobserver
360 Quality & Efficiency
Written by

360 Quality & Efficiency

360 Quality & Efficiency focuses on seamlessly integrating quality and efficiency in R&D, sharing 360’s internal best practices with industry peers to foster collaboration among Chinese enterprises and drive greater efficiency value.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.