获取当前的前景应用程序的截图在Android上使用root特权时特权、截图、应用程序、前景

2023-09-12 04:51:30 作者:簡簡單單ゞ

我开发的应用程序被安装在系统分区,我想知道是否有可能从一个服务当前前台应用程序的屏幕截图。当然,应用程序是任何第三方应用程序。

I'm developing an application that is installed on the system partition and I would like to know if it's possible to get a screenshot of the current foreground application from a service. Of course, the application being any third party app.

我不感兴趣的安全问题或任何有关该事项。我只是想获得当前的前景第三方应用程序的快照。

I'm not interested in security issues or anything related to that matter. I only want to get a snapshot of the current foreground third party app.

注:我知道的 /系统/斌/ screencap 解决方案,但我在寻找一个更优雅的替代方案,以编程方式做的一切

Note: I'm aware of the /system/bin/screencap solution but I'm looking for a more elegant alternative that does everything programmatically.

推荐答案

这是我将在下面描述会让你以编程方式采取一切应用程序是从一个后台进程前景的屏幕截图的方法。

The method that I'm going to describe below will let you to programmatically take screen shots of whatever app it's in the foreground from a background process.

我假设你有一个根深蒂固的设备。 我这种情况下,你可以使用以完成工作的 uiautomator框架。

I am assuming that you have a rooted device. I this case you can use the uiautomator framework to get the job done.

此框架具有创建自动化的Andr​​oid应用程序的黑盒测试,但它会适合这个目的也是如此。 我们将使用的方法

This framework has a been created to automate black box testing of apps on android, but it will suite this purpose as well. We are going to use the method

takeScreenshot(文件storePath,浮规模,INT质量)

本云在服务类:

File f = new File(context.getApplicationInfo().dataDir, "test.jar");

//this command will start uiautomator
String cmd = String.format("uiautomator runtest %s -c com.mypacket.Test", f.getAbsoluteFile());
Process p = doCmds(cmd);
if(null != p)
{
    p.waitFor();
}
else
{
    Log.e(TAG, "starting the test FAILED");
}

private Process doCmds(String cmds)
{
    try
    {
        Process su = Runtime.getRuntime().exec("su");
        DataOutputStream os = new DataOutputStream(su.getOutputStream());

        os.writeBytes(cmds + "\n");
        os.writeBytes("exit\n");
        os.flush();
        os.close();

        return su;
    }
    catch(Exception e)
    {
        e.printStackTrace();
        Log.e(TAG, "doCmds FAILED");

        return null;
    }
}

这是类uiautomator:

This is the class for uiautomator:

public class Test extends UiAutomatorTestCase
{
    public void testDemo()
    {
        UiDevice dev = UiDevice.getInstance();

        File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
        dev.takeScreenshot(f, 1.0, 100);
    }
}

,如果你创建一个后台线程中uiautomator运行,这样,它不会运行到UI线程是最好的。 (该服务运行在UI线程)。

It's best if you create a background thread in which uiautomator will run, that way it will not run onto the ui thread. (the Service runs on the ui thread).

uiatuomator不知道或有一个Android的环境。 一旦uiautomator得到控制,你就可以在它里面调用Android的方法是不带上下文参数或属于上下文类。

uiatuomator doesn't know about or have a android context. Once uiautomator gets the control you will be able to call inside it android methods that do not take a context parameter or belong to the context class.

如果您需要uiautomator和服务(或其他Android组件)之间的通信可以使用的 LocalSocket 。 这将允许以两种方式进行通信。

If you need to communicate between uiautomator and the service (or other android components) you can use LocalSocket. This will allow communication in both ways.