android 录屏不录制自身的悬浮框

Posted xjz729827161

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android 录屏不录制自身的悬浮框相关的知识,希望对你有一定的参考价值。

通过MediaProjectionManager的录屏操作,做了个简单的应用,但是每次都把自身的悬浮框录制了进去,脑壳疼。使用系统自带的录屏工具就不会有这个问题。为了一探究竟,捣鼓出了系统的录屏工具apk后,导出查看代码的方式可以参考操作指引,查看了下代码。得到了一份解决方案,附上工具类的代码。思路就是伪造window的标题和flag

package com.ux.xposed.utils;

import android.graphics.PixelFormat;
import android.os.Build;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.WindowManager;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Field;

public class RecorderFakeUtils 

    private static final String TAG = "RecorderFakeUtils";

    public static final String ROM_MIUI = "MIUI";
    public static final String ROM_EMUI = "EMUI";
    public static final String ROM_FLYME = "FLYME";
    public static final String ROM_OPPO = "OPPO";
    public static final String ROM_SMARTISAN = "SMARTISAN";
    public static final String ROM_VIVO = "VIVO";
    public static final String ROM_QIKU = "QIKU";

    private static final String KEY_VERSION_MIUI = "ro.miui.ui.version.name";
    private static final String KEY_VERSION_EMUI = "ro.build.version.emui";
    private static final String KEY_VERSION_OPPO = "ro.build.version.opporom";
    private static final String KEY_VERSION_SMARTISAN = "ro.smartisan.version";
    private static final String KEY_VERSION_VIVO = "ro.vivo.os.version";

    private static String sName;
    private static String sVersion;

    //华为
    public static boolean isEmui() 
        return check(ROM_EMUI);
    
    //小米
    public static boolean isMiui() 
        return check(ROM_MIUI);
    
    //vivo
    public static boolean isVivo() 
        return check(ROM_VIVO);
    
    //oppo
    public static boolean isOppo() 
        return check(ROM_OPPO);
    
    //魅族
    public static boolean isFlyme() 
        return check(ROM_FLYME);
    
    //360手机
    public static boolean is360() 
        return check(ROM_QIKU) || check("360");
    

    public static boolean isSmartisan() 
        return check(ROM_SMARTISAN);
    

    public static String getName() 
        if (sName == null) 
            check("");
        
        return sName;
    

    public static String getVersion() 
        if (sVersion == null) 
            check("");
        
        return sVersion;
    

    public static WindowManager.LayoutParams getFakeRecorderWindowLayoutParams() 
        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) 
            if (isMiui()) 
                layoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;
             else 
                layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
            

         else 
            layoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;
        
        layoutParams.format = PixelFormat.RGBA_8888;
        layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        layoutParams.gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP;//悬浮框在布局的位置
        layoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;//悬浮窗的宽,不指定则无法滑动
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;//悬浮窗的高,不指定则无法滑动
        layoutParams.x = 0; //初始位置的x坐标
        layoutParams.y = 0; //初始位置的y坐标
        layoutParams.setTitle(RecorderFakeUtils.getFakeRecordWindowTitle());

        return layoutParams;
    



    /**
     * 总结下可以操作的是华为,魅族,oppo
     * @return
     */
    public static String getFakeRecordWindowTitle() 
        if (sName == null) 
            check("");
        
        if (sName == null) 
            return "";
        
        switch (sName) 
            case ROM_OPPO:
                //oppo手机的悬浮框截屏,每次都需要弹出授权框,我去,这就很尴尬了
                return "com.coloros.screenrecorder.FloatView";
            case ROM_VIVO:
                return "screen_record_menu";
        
        return "";
    

    public static boolean check(String rom) 
        if (sName != null) 
            return sName.equals(rom);
        

        if (!TextUtils.isEmpty(sVersion = getProp(KEY_VERSION_MIUI))) 
            sName = ROM_MIUI;
         else if (!TextUtils.isEmpty(sVersion = getProp(KEY_VERSION_EMUI))) 
            sName = ROM_EMUI;
         else if (!TextUtils.isEmpty(sVersion = getProp(KEY_VERSION_OPPO))) 
            sName = ROM_OPPO;
         else if (!TextUtils.isEmpty(sVersion = getProp(KEY_VERSION_VIVO))) 
            sName = ROM_VIVO;
         else if (!TextUtils.isEmpty(sVersion = getProp(KEY_VERSION_SMARTISAN))) 
            sName = ROM_SMARTISAN;
         else 
            sVersion = Build.DISPLAY;
            if (sVersion.toUpperCase().contains(ROM_FLYME)) 
                sName = ROM_FLYME;
             else 
                sVersion = Build.UNKNOWN;
                sName = Build.MANUFACTURER.toUpperCase();
            
        
        return sName.equals(rom);
    

    public static String getProp(String name) 
        String line = null;
        BufferedReader input = null;
        try 
            Process p = Runtime.getRuntime().exec("getprop " + name);
            input = new BufferedReader(new InputStreamReader(p.getInputStream()), 1024);
            line = input.readLine();
            input.close();
         catch (IOException ex) 
            MyLogUtils.d(TAG, "Unable to read prop " + name + ex);
            return null;
         finally 
            if (input != null) 
                try 
                    input.close();
                 catch (IOException e) 
                    e.printStackTrace();
                
            
        
        return line;
    




有不少人找我针对某个系统进行有偿适配,大部分人都很nice, 但是有些人极其恶劣。我要花点时间,付出了劳动,要时间,也要精力。请尊重我的劳动的成果,也尊重自己,也不要给我找我麻烦。
大部分的系统是可以适配的,我只写了以上vivo和oppo的

以上是关于android 录屏不录制自身的悬浮框的主要内容,如果未能解决你的问题,请参考以下文章

有没有免费的录屏软件

联想怎么录屏

打开打开录制声音后,为啥录屏还是没声音?

android开发设置屏蔽录制

手机录制视频没有声音怎么办

为啥我的屏幕录像无声音或者有杂音