android 录屏流程以及权限管理(底层权限修改及讲解)

     android正常录屏流程需要申请权限,只需要调用正常的api,用户自己点击确定按钮,即可获取到录屏权限,上层app获取录屏权限的流程,废话不多说,下面看代码:

   

public void takeScreenshot(Activity activity, int width, int height, ScreenshotCallback cb) {
        this.width = width;
        this.height = height;
        this.cb = cb;
        mMediaProjectionManager = (MediaProjectionManager) activity.getSystemService(Context.MEDIA_PROJECTION_SERVICE);
        if (mMediaProjectionManager == null) {
            return;
        }
        activity.startActivityForResult(mMediaProjectionManager.createScreenCaptureIntent(), MEDIA_PROJECTION_CODE);
    }

这是申请上层录屏权限的部分,createScreenCaptureIntent()此api就是跳转到MediaProjectionManager中去跳转dialog,引导用户获取权限,然后,底层会给我们返回一个Intent ,intent里面包含的权限信息,我们需要在上层写一个OnactivityForReslut方法用来接收底层给我们的权限信息,代码如下:

/**
     * This method must be called under the activity's onActivityResult()
     * @param resultCode resultCode
     * @param data data
     */
    public void onActivityResult(int resultCode, Intent data) {
        imageAvailable = 0;
        mImageReader = ImageReader.newInstance(width, height, PixelFormat.RGBA_8888, 2);
        if (mMediaProjection == null) {
            mMediaProjection = mMediaProjectionManager.getMediaProjection(resultCode, data);
        }
        try {
            virtualDisplay = mMediaProjection.createVirtualDisplay("Screenshot", width, height, 60,
                    DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
                    mImageReader.getSurface(), null, null);
            mImageReader.setOnImageAvailableListener(ScreenShotUtil.this, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

这段代码解释一下,首先,创建一个ImageReader对象,然后,创建一个虚拟桌面,即virtualDisplay对象,imageReader就来读这个虚拟桌面,获取这个虚拟桌面,然后,我们在要保存的地方调用获取权限的方法,代码如下:

ScreenShotUtil.getInstance().takeScreenshot(activity, getWidth(), getHeight(),
                        new ScreenShotUtil.ScreenshotCallback() {
                            @Override
                            public void onScreenshot(Bitmap bitmap) {
                                FileOutputStream fos = null;
                                try {
                                    fos = new FileOutputStream(file);
                                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
                                    activity.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));
                                    fos.flush();
                                    Toast.makeText(getContext(), R.string.save_success, Toast.LENGTH_SHORT).show();
                                } catch (Exception ignored) {
                                    Toast.makeText(getContext(), R.string.save_failed, Toast.LENGTH_SHORT).show();
                                } finally {
                                    if (fos != null) {
                                        try {
                                            fos.close();
                                        } catch (IOException e) {
                                            e.printStackTrace();
                                        }
                                    }
                                    if (isExit) {
                                        activity.exit();
                                    } else {
                                        activity.showWriteBottomBar();
                                    }
                                    isSaving = false;
                                    bitmap.recycle();
                                }
                            }
                        });

     好了,上层就是这么简单的,那我们来看看底层是怎么实现的这个流程呢?

首先,如果要调用这个录屏权限,前面说道 要获取权限要调用createScreenCaptureIntent()方法,这个方法在哪呢?MediaProjectionManager类在fragment/base/media/android/media/projection中,里面有下面这个方法

/**
     * Returns an Intent that <b>must</b> passed to startActivityForResult()
     * in order to start screen capture. The activity will prompt
     * the user whether to allow screen capture.  The result of this
     * activity should be passed to getMediaProjection.
     */
    public Intent createScreenCaptureIntent() {
        Intent i = new Intent();
        i.setClassName("com.android.systemui",
                "com.android.systemui.media.MediaProjectionPermissionActivity");
        return i;
    }

此方法一看就看出来了,这是跳转到MediaProjectionPermissionActivity类中去处理逻辑了,这个类重要的部分其实也不多,无非就是弹了一个dialog,但是,这个类里面有相关服务的创建以及类的传输工作,下面我们就来看看

IBinder b = ServiceManager.getService(MEDIA_PROJECTION_SERVICE);
        mService = IMediaProjectionManager.Stub.asInterface(b);

由此,创建了binder通道,并且拿到了Service,当然,MediaProjectionManager本身就是一个service,这个service就是用来为上层赋予权限的。那么创建这两个通道的作用什么呢?

    private Intent getMediaProjectionIntent(int uid, String packageName, boolean permanentGrant)
            throws RemoteException {
        IMediaProjection projection = mService.createProjection(uid, packageName,
                MediaProjectionManager.TYPE_SCREEN_CAPTURE, permanentGrant);
        Intent intent = new Intent();
        intent.putExtra(MediaProjectionManager.EXTRA_MEDIA_PROJECTION, projection.asBinder());
        return intent;
    }

在这里,通过service来创建一个projection对象,拿到这个对象之后,由binder创建代理对象,统一交给intent来传递出去,intent中的数据总要拿出来处理的,那在哪里处理呢?在ManagerProjectManager中创建的对象,当然在这里面进行结果的处理了,处理的方法如下:

/**
     * Retrieve the MediaProjection obtained from a succesful screen
     * capture request. Will be null if the result from the
     * startActivityForResult() is anything other than RESULT_OK.
     *
     * @param resultCode The result code from {@link android.app.Activity#onActivityResult(int,
     * int, android.content.Intent)}
     * @param resultData The resulting data from {@link android.app.Activity#onActivityResult(int,
     * int, android.content.Intent)}
     */
    public MediaProjection getMediaProjection(int resultCode, @NonNull Intent resultData) {
        if (resultCode != Activity.RESULT_OK || resultData == null) {
            return null;
        }
        IBinder projection = resultData.getIBinderExtra(EXTRA_MEDIA_PROJECTION);
        if (projection == null) {
            return null;
        }
        return new MediaProjection(mContext, IMediaProjection.Stub.asInterface(projection));
    }

由此可见,这里返回了一个projection对象,这个对象用来干嘛呢?就是我们上面说的,用这个对象来创建一个虚拟的显示桌面,由ImageReader来写入这个虚拟桌面,创建虚拟桌面的方法在哪?由projection对象创建,当然在MediaProjection中,这个类也在media/java/media/projection/包中,这个包中有一个创建虚拟桌面的方法,如下:

   /**
     * Creates a {@link android.hardware.display.VirtualDisplay} to capture the
     * contents of the screen.
     *
     * @param name The name of the virtual display, must be non-empty.
     * @param width The width of the virtual display in pixels. Must be
     * greater than 0.
     * @param height The height of the virtual display in pixels. Must be
     * greater than 0.
     * @param dpi The density of the virtual display in dpi. Must be greater
     * than 0.
     * @param surface The surface to which the content of the virtual display
     * should be rendered, or null if there is none initially.
     * @param flags A combination of virtual display flags. See {@link DisplayManager} for the full
     * list of flags.
     * @param callback Callback to call when the virtual display's state
     * changes, or null if none.
     * @param handler The {@link android.os.Handler} on which the callback should be
     * invoked, or null if the callback should be invoked on the calling
     * thread's main {@link android.os.Looper}.
     *
     * @see android.hardware.display.VirtualDisplay
     */
    public VirtualDisplay createVirtualDisplay(@NonNull String name,
            int width, int height, int dpi, int flags, @Nullable Surface surface,
            @Nullable VirtualDisplay.Callback callback, @Nullable Handler handler) {
        DisplayManager dm = (DisplayManager) mContext.getSystemService(Context.DISPLAY_SERVICE);
        return dm.createVirtualDisplay(this, name, width, height, dpi, surface, flags, callback,
                handler, null /* uniqueId */);
    }

至此,整个流程已经走完了,说到底,无非就是对象之间的传递,有binder进行c/s之间的权限信息的传递,有的公司可能想要自己定制这一块流程,不想弹这个权限的dialog,首先,如果想要做这个功能就必须有root权限,或者,你能在源码中编译,恰好,我们公司有这个需求,下面的实现方式,解决了不弹窗的问题,直接赋予权限,而不用用户手动点击:

 public void takeScreenshot(Activity activity, int width, int height, ScreenshotCallback cb) {
        this.width = width;
        this.height = height;
        this.cb = cb;
        int uid;
        MediaProjectionManager mediaProjectionManager = (MediaProjectionManager) activity.getSystemService(Context.MEDIA_PROJECTION_SERVICE);
        if (mediaProjectionManager == null) {
            return;
        } else {
            String packageName = activity.getPackageName();
            if (packageName == null) {
                return;
            }
            IBinder b = ServiceManager.getService(activity.MEDIA_PROJECTION_SERVICE);
            IMediaProjectionManager mService = IMediaProjectionManager.Stub.asInterface(b);

            PackageManager packageManager = activity.getPackageManager();
            ApplicationInfo aInfo;
            try {
                aInfo = packageManager.getApplicationInfo(packageName, 0);
                uid = aInfo.uid;

                IMediaProjection projection = mService.createProjection(uid, packageName,
                        MediaProjectionManager.TYPE_SCREEN_CAPTURE, !mService.hasProjectionPermission(uid, packageName));
                takeScreen(new MediaProjection(activity, projection));
            } catch (RemoteException e) {
                Log.e(TAG, "Error checking projection permissions", e);
            } catch (PackageManager.NameNotFoundException e) {
                Log.e(TAG, "unable to look up package name", e);
            }
        }
    }

这样,就可以解决权限问题,当然,在清单文件中必须加上

<uses-permission android:name="android.permission.MANAGE_MEDIA_PROJECTION"/>

这条权限,不然编译不过,因为binder获取service中有这个权限!

大家有不明白的,可以在下面留言,一起学习

  • 3
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
Android 录屏权限是指用户在使用Android设备时,可以给予应用程序录制屏幕的权限。这样,应用程序就可以在设备屏幕上进行录制,并将其保存为视频文件。 为了保护用户的隐私和安全,Android系统为录屏权限设置了限制。通常情况下,应用程序默认是没有录屏权限的,需要用户在使用应用程序时手动授权。 要在Android应用程序中获取录屏权限,开发者需要在应用的清单文件(Manifest)中添加相应的权限声明,以指示应用程序需要访问录屏功能。例如,可以使用以下代码添加录屏权限声明: ```xml <uses-permission android:name="android.permission.CAPTURE_VIDEO_OUTPUT" /> ``` 用户在安装或打开应用程序时,系统会显示让用户选择是否授予录屏权限的对话框。用户可以选择授予或拒绝该权限。如果用户授予了权限,应用程序就可以在设备屏幕上进行录制。 需要注意的是,获取录屏权限并不意味着应用程序可以随意录制屏幕。Android系统对录屏权限进行了限制,只有当应用程序处于前台运行状态时,才有权限进行录制。这是为了保护用户的隐私和避免恶意行为。 最后,当应用程序不再需要录屏权限时,开发者应该及时释放这个权限,以提高用户的隐私保护和系统性能。在应用程序中,可以使用以下代码取消录屏权限: ```kotlin revokeUriPermission(/*uri*/) ``` 总结来说,Android 录屏权限是一项重要的功能,可以在屏幕上录制应用程序的操作,并将其保存为视频文件。然而,获取这个权限需要用户手动授权,并且仅允许在应用程序处于前台运行状态时进行录制。开发者在使用录屏权限需要注意保护用户的隐私和合理使用这项功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值