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

您所在的位置:网站首页 修改安卓app权限教程视频 android 录屏流程以及权限管理(底层权限修改及讲解)

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

2024-07-08 21:12:46| 来源: 网络整理| 查看: 265

     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 must 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); } } }

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

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

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



【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭