高通Android 12/Android 13截屏

正常截屏Power+音量-键 组合键同时长按,实现截屏逻辑。

PhoneWindowManager #init #interceptScreenshotChord

复制代码
init(Context context, IWindowManager windowManager,
            WindowManagerFuncs windowManagerFuncs)

2、在PhoneWindowManager中init方法中注册广播

frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java

复制代码
        // zm fix
		IntentFilter screenfilter = new IntentFilter();
        screenfilter.addAction("android.intent.action.SCREEN_SHOT");
		context.registerReceiver(mScreenshotReceiver, screenfilter);

3、广播实现代码如下图所示

复制代码
    private BroadcastReceiver mScreenshotReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if ("android.intent.action.SCREEN_SHOT".equals(intent.getAction())) {
			Log.e("Receive Screen Shot Action","Screen Shot="+intent.getAction());
			String path = intent.getStringExtra("path");
            Log.e("onReceive", "Screenshot path: " + path);
            interceptScreenshotChord();
			copyLatestFile("/sdcard/Pictures/Screenshots",path);
			Log.e("onReceive", "Screenshot path completed: " + path);
			
        }
    }
};

4、自定义Service里面发送广播 代码如下所示

复制代码
 /**
     * 截屏
     *
     * @param path
     */
    public boolean capture(String path) {
		Log.e("capture","=========start capture=============");
		Intent intent = new Intent("android.intent.action.SCREEN_SHOT");
		intent.putExtra("path", path);
        mContext.sendBroadcast(intent);
	 
    }

5、PhoneWindowManager中调用 是发广播到 PhoneWindowManager interceptScreenshotChord中调用,具体源码细节,大家感兴趣去源码跟下哈,这里不再赘述哈。

复制代码
    private void interceptScreenshotChord() {
        mHandler.removeCallbacks(mScreenshotRunnable);
        mScreenshotRunnable.setScreenshotType(TAKE_SCREENSHOT_FULLSCREEN);
        mScreenshotRunnable.setScreenshotSource(SCREENSHOT_KEY_CHORD);
        mHandler.postDelayed(mScreenshotRunnable, getScreenshotChordLongPressDelay());
    }

6、最后截屏具体实现代码逻辑 如下所示

复制代码
private class ScreenshotRunnable implements Runnable {
        private int mScreenshotType = TAKE_SCREENSHOT_FULLSCREEN;
        private int mScreenshotSource = SCREENSHOT_KEY_OTHER;

        public void setScreenshotType(int screenshotType) {
            mScreenshotType = screenshotType;
        }

        public void setScreenshotSource(int screenshotSource) {
            mScreenshotSource = screenshotSource;
        }

        @Override
        public void run() {
            mDefaultDisplayPolicy.takeScreenshot(mScreenshotType, mScreenshotSource);
        }
    }

    private final ScreenshotRunnable mScreenshotRunnable = new ScreenshotRunnable();

到这里基本结束,转载请注明出处,高通Android 12/Android 13截屏-CSDN博客谢谢!

相关推荐
砖厂小工2 小时前
用 GLM + OpenClaw 打造你的 AI PR Review Agent — 让龙虾帮你审代码
android·github
张拭心3 小时前
春节后,有些公司明确要求 AI 经验了
android·前端·人工智能
张拭心3 小时前
Android 17 来了!新特性介绍与适配建议
android·前端
Kapaseker5 小时前
Compose 进阶—巧用 GraphicsLayer
android·kotlin
黄林晴6 小时前
Android17 为什么重写 MessageQueue
android
阿巴斯甜1 天前
Android 报错:Zip file '/Users/lyy/develop/repoAndroidLapp/l-app-android-ble/app/bu
android
Kapaseker1 天前
实战 Compose 中的 IntrinsicSize
android·kotlin
xq95271 天前
Andorid Google 登录接入文档
android
黄林晴1 天前
告别 Modifier 地狱,Compose 样式系统要变天了
android·android jetpack
冬奇Lab2 天前
Android触摸事件分发、手势识别与输入优化实战
android·源码阅读