获取 android 上其他正在运行的应用的截图 [英] Get screenshots of other running apps on android

查看:85
本文介绍了获取 android 上其他正在运行的应用的截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Android 上,我可以通过在活动管理器上执行 getRunningAppProcesses 来获取正在运行的应用程序列表:

On Android I can get list of running applications by executing getRunningAppProcesses on activity manager:

        ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> runningAppProcessInfo = am.getRunningAppProcesses();

但是我想要更多的东西,当我打开系统任务管理器时,有运行应用程序的截图,比如截图我可以在自己的应用程序中获取这些屏幕截图吗?如何获取表示给定窗口内容的图像?

But I want something more, when I open system task manager, there are screenshots of running applications, like on Screenshot Can I get those screenshots in my own application? How to get image representing contents of given window?

恩德鲁

推荐答案

无需root权限即可截图:

take screenshot without needs root access:

@SuppressLint("NewApi")
    public void takeScreenshot(Context context, String fileFullPath)
    {
        if(fileFullPath == ""){
            format = new SimpleDateFormat("yyyyMMddHHmmss");
            String fileName = format.format(new Date(System.currentTimeMillis())) + ".png";
            fileFullPath = "/data/local/tmp/" + fileName;
        }

        if(ShellUtils.checkRootPermission()){
            if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH){
                ShellUtils.execCommand("/system/bin/screencap -p "+ fileFullPath,true);
            }
        }
        else {
            if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN_MR2 && android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH){
                wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
                mDisplay = wm.getDefaultDisplay();
                mDisplayMatrix = new Matrix();
                mDisplayMetrics = new DisplayMetrics();
                // We need to orient the screenshot correctly (and the Surface api seems to take screenshots
                // only in the natural orientation of the device :!)
                mDisplay.getRealMetrics(mDisplayMetrics);
                float[] dims =
                {
                        mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels
                };
                float degrees = getDegreesForRotation(mDisplay.getRotation());
                boolean requiresRotation = (degrees > 0);
                if (requiresRotation){
                    // Get the dimensions of the device in its native orientation
                    mDisplayMatrix.reset();
                    mDisplayMatrix.preRotate(-degrees);
                    mDisplayMatrix.mapPoints(dims);
                    dims[0] = Math.abs(dims[0]);
                    dims[1] = Math.abs(dims[1]);
                }

                Bitmap mScreenBitmap = screenShot((int) dims[0], (int) dims[1]);
                if (requiresRotation)   {
                    // Rotate the screenshot to the current orientation
                    Bitmap ss = Bitmap.createBitmap(mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels,
                            Bitmap.Config.ARGB_8888);
                    Canvas c = new Canvas(ss);
                    c.translate(ss.getWidth() / 2, ss.getHeight() / 2);
                    c.rotate(degrees);
                    c.translate(-dims[0] / 2, -dims[1] / 2);
                    c.drawBitmap(mScreenBitmap, 0, 0, null);
                    c.setBitmap(null);
                    mScreenBitmap = ss;
                    if (ss != null && !ss.isRecycled()) {
                        ss.recycle();
                    }
                }

                // If we couldn't take the screenshot, notify the user
                if (mScreenBitmap == null){
                    Toast.makeText(context, "screen shot fail", Toast.LENGTH_SHORT).show();
                }

                // Optimizations
                mScreenBitmap.setHasAlpha(false);
                mScreenBitmap.prepareToDraw();

                saveBitmap2file(context, mScreenBitmap, fileFullPath);  // save image file
            }
        }

    }

来源:ScreentShotUtil.java

这篇关于获取 android 上其他正在运行的应用的截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆