Android MediaProjection录屏权限处理

news/2024/9/19 9:05:02/ 标签: android

 MediaProjection服务在SystemServiceRegistry的静态块里面注册的

@frameworks/base/core/java/android/app/SystemServiceRegistry.java
public final class SystemServiceRegistry {private static final String TAG = "SystemServiceRegistry";/** @hide */public static boolean sEnableServiceNotFoundWtf = false;private static int sServiceCacheSize;private static volatile boolean sInitializing;// Not instantiable.private SystemServiceRegistry() { }static {registerService(Context.MEDIA_PROJECTION_SERVICE, MediaProjectionManager.class,

申请屏幕录制权限通过启动com.android.systemui/.media.MediaProjectionPermissionActivity来获取权限

requestScreenRecordPermissionIntent captureIntent = u3dClient.createScreenCaptureIntent();this.mProjectionManager.createScreenCaptureIntent();  //Intent { cmp=com.android.systemui/.media.MediaProjectionPermissionActivity } packagename=com.android.systemuistartActivityForResult(captureIntent, LOCAL_REQUEST_CODE);
@@frameworks/base/packages/SystemUI/src/com/android/systemui/media/MediaProjectionPermissionActivity.java/** Copyright (C) 2014 The Android Open Source Project** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**      http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package com.android.systemui.media;import static android.view.WindowManager.LayoutParams.SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS;import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.Typeface;
import android.media.projection.IMediaProjection;
import android.media.projection.IMediaProjectionManager;
import android.media.projection.MediaProjectionManager;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.text.BidiFormatter;
import android.text.SpannableString;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.style.StyleSpan;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;import com.android.systemui.R;
import com.android.systemui.util.Utils;public class MediaProjectionPermissionActivity extends Activityimplements DialogInterface.OnClickListener, DialogInterface.OnCancelListener {private static final String TAG = "MediaProjectionPermissionActivity";private static final float MAX_APP_NAME_SIZE_PX = 500f;private static final String ELLIPSIS = "\u2026";private String mPackageName;private int mUid;private IMediaProjectionManager mService;private AlertDialog mDialog;@Overridepublic void onCreate(Bundle icicle) {Log.e(TAG, "xww getprojectionperimssino 1");super.onCreate(icicle);mPackageName = getCallingPackage();IBinder b = ServiceManager.getService(MEDIA_PROJECTION_SERVICE);mService = IMediaProjectionManager.Stub.asInterface(b);if (mPackageName == null) {finish();Log.e(TAG, "xww getprojectionperimssino 2");return;}PackageManager packageManager = getPackageManager();ApplicationInfo aInfo;try {aInfo = packageManager.getApplicationInfo(mPackageName, 0);mUid = aInfo.uid;} catch (PackageManager.NameNotFoundException e) {Log.e(TAG, "unable to look up package name", e);finish();Log.e(TAG, "xww getprojectionperimssino 3");return;}try {if (mService.hasProjectionPermission(mUid, mPackageName)) {setResult(RESULT_OK, getMediaProjectionIntent(mUid, mPackageName));finish();Log.e(TAG, "xww getprojectionperimssino 4");return;} else {setResult(RESULT_OK, getMediaProjectionIntent(mUid, mPackageName));Log.e(TAG, "xww getprojectionperimssino 5");finish();return;}} catch (RemoteException e) {Log.e(TAG, "Error checking projection permissions", e);finish();Log.e(TAG, "xww getprojectionperimssino 6");return;}}@Overrideprotected void onDestroy() {super.onDestroy();if (mDialog != null) {mDialog.dismiss();}}@Overridepublic void onClick(DialogInterface dialog, int which) {try {if (which == AlertDialog.BUTTON_POSITIVE) {setResult(RESULT_OK, getMediaProjectionIntent(mUid, mPackageName));}} catch (RemoteException e) {Log.e(TAG, "Error granting projection permission", e);setResult(RESULT_CANCELED);} finally {if (mDialog != null) {mDialog.dismiss();}finish();}}private Intent getMediaProjectionIntent(int uid, String packageName)throws RemoteException {IMediaProjection projection = mService.createProjection(uid, packageName,MediaProjectionManager.TYPE_SCREEN_CAPTURE, false /* permanentGrant */);Intent intent = new Intent();intent.putExtra(MediaProjectionManager.EXTRA_MEDIA_PROJECTION, projection.asBinder());return intent;}@Overridepublic void onCancel(DialogInterface dialog) {finish();}
}

最后通过获取Activity的结果判断是否成功获取到屏幕录制权限

    public void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);if (resultCode != Activity.RESULT_CANCELED) {Log.e(TAG, "获取到屏幕录制权限----");return;}}

@@frameworks/base/packages/SystemUI/src/com/android/systemui/media/MediaProjectionPermissionActivity.java


/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.media;

import static android.view.WindowManager.LayoutParams.SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.Typeface;
import android.media.projection.IMediaProjection;
import android.media.projection.IMediaProjectionManager;
import android.media.projection.MediaProjectionManager;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.text.BidiFormatter;
import android.text.SpannableString;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.style.StyleSpan;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.android.systemui.R;
import com.android.systemui.util.Utils;

public class MediaProjectionPermissionActivity extends Activity
        implements DialogInterface.OnClickListener, DialogInterface.OnCancelListener {
    private static final String TAG = "MediaProjectionPermissionActivity";
    private static final float MAX_APP_NAME_SIZE_PX = 500f;
    private static final String ELLIPSIS = "\u2026";

    private String mPackageName;
    private int mUid;
    private IMediaProjectionManager mService;

    private AlertDialog mDialog;

    @Override
    public void onCreate(Bundle icicle) {

            Log.e(TAG, "xww getprojectionperimssino 1");
        super.onCreate(icicle);

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

        if (mPackageName == null) {
            finish();
            Log.e(TAG, "xww getprojectionperimssino 2");
            return;
        }

        PackageManager packageManager = getPackageManager();
        ApplicationInfo aInfo;
        try {
            aInfo = packageManager.getApplicationInfo(mPackageName, 0);
            mUid = aInfo.uid;
        } catch (PackageManager.NameNotFoundException e) {
            Log.e(TAG, "unable to look up package name", e);
            finish();
            Log.e(TAG, "xww getprojectionperimssino 3");
            return;
        }

        try {
            if (mService.hasProjectionPermission(mUid, mPackageName)) {
                setResult(RESULT_OK, getMediaProjectionIntent(mUid, mPackageName));
                finish();
                Log.e(TAG, "xww getprojectionperimssino 4");
                return;
            } else {
                setResult(RESULT_OK, getMediaProjectionIntent(mUid, mPackageName));
                Log.e(TAG, "xww getprojectionperimssino 5");
                finish();
                return;
            }
        } catch (RemoteException e) {
            Log.e(TAG, "Error checking projection permissions", e);
            finish();
            Log.e(TAG, "xww getprojectionperimssino 6");
            return;
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mDialog != null) {
            mDialog.dismiss();
        }
    }

    @Override
    public void onClick(DialogInterface dialog, int which) {
        try {
            if (which == AlertDialog.BUTTON_POSITIVE) {
                setResult(RESULT_OK, getMediaProjectionIntent(mUid, mPackageName));
            }
        } catch (RemoteException e) {
            Log.e(TAG, "Error granting projection permission", e);
            setResult(RESULT_CANCELED);
        } finally {
            if (mDialog != null) {
                mDialog.dismiss();
            }
            finish();
        }
    }

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

    @Override
    public void onCancel(DialogInterface dialog) {
        finish();
    }
}
 


http://www.ppmy.cn/news/1525470.html

相关文章

软考攻略/超详细/系统集成项目管理工程师/基础知识分享10

4.3 应用架构(掌握) 应用架构的主要内容是规划出目标应用分层分域架构,根据业务架构规划目标应用域、应用组和目标应用组件 4.3.1 基本原则(掌握) (1)业务适配性原则:应用架构具备一…

停车位检测-停车场车位识别

YOLO Parking Spot 概述 停车场获取的图像训练了四个YOLO模型来检测车辆。目标是收集信息,并可能开发一种停车解决方案以改善交通流量并优化空间利用率。通过识别汽车,我们生成了一份报告,其中包含图像细节,如可用停车位的数量、…

VPP配置网卡多队列no bufs问题

在VPP配置文件/etc/vpp/startup.conf中启用网卡的多队列功能,指定接收和发送队列的数量。 dpdk { dev default { num-rx-queues 4 num-tx-queues 4 } 在测试中发现如下情况,网卡的rx no bufs错误计数非常高。 vpp # show hardware-interfaces GigabitEt…

51单片机-DS1302(实时时钟+可调时钟)(可参考主页上一节内容介绍)

作者&#xff1a;王开心 时间&#xff1a;2024.9.10 目的&#xff1a;手撕51 main.c #include <REGX52.H> #include "LCD1602.h" #include "DS1302.h" #include "Key.h" #include "Delay.h" #include "Timer0.h"u…

如何测量一个(传输网络)系统的容量

Little 定律就能反算系统容量&#xff0c;但我这篇文章要正着算。 假想一个理发店场景。李大爷拥有一家占地 50 平米的理发店&#xff0c;经理到店里理发如果已经有经理在理发&#xff0c;就要拿个券等待&#xff0c;请问李大爷需要印多少等待券&#xff1f; 这是个系统容量问…

Flutter自定义Icon的简易使用(两种)

方式一&#xff1a;利用第三方库&#xff08;建议&#xff09; 1、在阿里图标库(iconfont-阿里巴巴矢量图标库)上&#xff0c;加载购物车后&#xff0c;点击“下载素材”svg。 2、把下载的图片放入asstes目录下。​​​​​​​ 3、修改yaml配置文件&#xff0c;设置Icon图标所…

【自费2W真机测评】三款热门/旗舰宠物空气净化器米家、希喂、352对比试用!

我家老大是三个月大的时候接回来的&#xff0c;接回来前就是家教好的小猫咪一只&#xff0c;不乱尿、不掉毛的。看朋友家都被猫咪掉毛困扰着&#xff0c;我还嘚瑟觉得自己养可好了&#xff0c;根本不掉毛。养了三个月老大长成大猫猫了&#xff0c;我又觉得我可以了&#xff0c;…

【系统架构设计师】享元模式

享元模式(Flyweight Pattern)是一种结构型设计模式,它用于减少需要创建的对象数量,以节省内存。享元模式通过共享已经存在的对象来表示尽可能多的新对象,从而降低系统中对象的数量,提高性能。享元模式尤其适合于系统中存在大量相似对象的情况,通过共享公共的部分数据,可…

跟《经济学人》学英文:2024年09月14日这期 The sweet story of Peru’s blueberry boom

The sweet story of Peru’s blueberry boom Plucky farmers have transformed the market in only ten years plucky&#xff1a;英 [ˈplʌki] 勇敢的&#xff1b;无畏的&#xff1b;有胆识的 原文&#xff1a; Peru’s blueberry harvest is just beginning, and Ivan Ja…

GitLab CI Runner安装

参考文章&#xff1a;[花了两天&#xff0c;搞了Gitlab-Runner CI/CD实现自动化部署&#xff0c;可比Jenkins香太多啦&#xff01;&#xff01;&#xff01;&#xff01;_gitlab的cicd取代jenkens-CSDN博客] Gitlab的CI需要安装CI专用的GitLab Runner&#xff0c;否则跑不起来…

《 C++ 修炼全景指南:九 》打破编程瓶颈!掌握二叉搜索树的高效实现与技巧

摘要 本文详细探讨了二叉搜索树&#xff08;Binary Search Tree, BST&#xff09;的核心概念和技术细节&#xff0c;包括插入、查找、删除、遍历等基本操作&#xff0c;并结合实际代码演示了如何实现这些功能。文章深入分析了二叉搜索树的性能优势及其时间复杂度&#xff0c;同…

pytorch正向传播没问题,loss.backward()使定义的神经网络中权重参数变为nan

记录一个非常坑爹的bug:loss回传导致神经网络中一个linear层的权重参数变为nan 1.首先loss值是正常数值&#xff1b; 2.查了好多网上的解决办法&#xff1a;检查原始输入神经网络数据有没有nan值&#xff0c;初始化权重参数&#xff0c;使用relu激活函数&#xff0c;梯度裁剪&a…

git如何灵活切换本地账号对应远程github的两个账号

git如何灵活切换本地账号对应远程github的两个账号 问题&#xff1a; 有时候我们会同时维护两个github的账号里面的仓库内容&#xff0c;这时候本地git需要频繁的切换ssh&#xff0c;以方便灵活的与两个账号的仓库可以通信。这篇日记将阐述我是怎么解决这个问题的。1. 第一个账…

黑龙江等保测评二级系统费用解析:如何合理预算?

在信息安全日益受到重视的今天&#xff0c;等保测评成为企业合规的重要环节。尤其是在黑龙江&#xff0c;随着网络安全法的实施&#xff0c;越来越多的企业开始关注等保测评的相关费用。那么&#xff0c;黑龙江等保测评二级系统的费用是如何计算的呢&#xff1f; 首先&#xf…

vue.nextTick()方法的使用

定义&#xff1a;在下次DOM更新循环结束之后执行延迟回调。在修改数据之后立即使用这个方法&#xff0c;获取更新后的DOM. PS:我其实一直读不懂这句话&#xff0c;真心觉得是语文功底太欠了。怀疑人生。 说下我遇到的问题&#xff0c;在创建报价单的初始界面&#xff0c;我需…

PP_HTONL(x)

#define PP_HTONL(x) ((((x) & (u32_t)0x000000ffUL) << 24) | \(((x) & (u32_t)0x0000ff00UL) << 8) | \(((x) & (u32_t)0x00ff0000UL) >> 8) | \(((x) & (u32_t)0xff000000UL) >> 24)) 这个宏 PP_HTONL(x) 用于将一个 32 位无符号…

【图像识别】摄像头捕捉运动到静止视频帧(免费源码分享)

【图像识别】摄像头捕捉运动到静止视频帧(免费源码分享) 1. 本文摘要 本文实现了一个OpenCV和PyQT5 结合的摄像头视频捕捉和运动检测线程&#xff0c;实现了一款界面软件用于功能演示。主要使用帧差法实现&#xff0c;摄像头捕捉运动到静止的图片&#xff0c;捕捉到的图片可用…

LombokJunit

1.lombok介绍 1.概述:第三方工具(oracle属于第一方,我们自己属于第二方,除此之外都是第三方) 2.作用:简化javabean 3.使用:想要使用第三方的工具,需要导入第三方提供给咱们得jar包(jar是一个压缩包,需要将jar导入到我们的模块下,解压才能使用里面的工具类) a.在当前模块…

解决 PyCharm 无法启动 Jupyter 服务器的问题:报错分析与解决方案

文章目录 报错背景报错详细信息解决方案pycharm 设置 报错背景 在使用 pycharm 付费版的过程中&#xff0c;发现一直无法启动 jupyter 服务器。 一直也不知道是为什么&#xff0c;直到在终端输入&#xff1a; jupyter notebook发现 jupyter 服务无法启动。 报错详细信息 下…

《Docker:轻量级虚拟化解决方案》

《Docker&#xff1a;轻量级虚拟化解决方案》 在当今的软件开发和部署领域&#xff0c;Docker 以其独特的优势成为了众多开发者和运维人员的得力工具。它是一个开源的应用容器引擎&#xff0c;基于 Go 语言并遵从 Apache2.0 协议开源。 一、Docker 的简介与概述 Docker 的主…