google 给出一个截屏录屏的方法5.0以上 需要onActivityResult()里的intent参数

1、startActivityForResult(mMediaProjectionManager.createScreenCaptureIntent(),REQUEST_MEDIA_PROJECTION);

2、在onActivityResult里的

setUpMediaProjection();

setUpVirtualDisplay();

/*

* 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.example.android.screencapture;

import android.app.Activity;

import android.content.Context;

import android.content.Intent;

import android.hardware.display.DisplayManager;

import android.hardware.display.VirtualDisplay;

import android.media.projection.MediaProjection;

import android.media.projection.MediaProjectionManager;

import android.os.Bundle;

import android.support.annotation.Nullable;

import android.support.v4.app.Fragment;

import android.util.DisplayMetrics;

import android.view.LayoutInflater;

import android.view.Surface;

import android.view.SurfaceView;

import android.view.View;

import android.view.ViewGroup;

import android.widget.Button;

import android.widget.Toast;

import com.example.android.common.logger.Log;

/**

* Provides UI for the screen capture.

*/

public class ScreenCaptureFragment extends Fragment implements View.OnClickListener {

private static final String TAG = "ScreenCaptureFragment";

private static final String STATE_RESULT_CODE = "result_code";

private static final String STATE_RESULT_DATA = "result_data";

private static final int REQUEST_MEDIA_PROJECTION = 1;

private int mScreenDensity;

private int mResultCode;

private Intent mResultData;

private Surface mSurface;

private MediaProjection mMediaProjection;

private VirtualDisplay mVirtualDisplay;

private MediaProjectionManager mMediaProjectionManager;

private Button mButtonToggle;

private SurfaceView mSurfaceView;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

if (savedInstanceState != null) {

mResultCode = savedInstanceState.getInt(STATE_RESULT_CODE);

mResultData = savedInstanceState.getParcelable(STATE_RESULT_DATA);

}

}

@Nullable

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

return inflater.inflate(R.layout.fragment_screen_capture, container, false);

}

@Override

public void onViewCreated(View view, Bundle savedInstanceState) {

mSurfaceView = (SurfaceView) view.findViewById(R.id.surface);

mSurface = mSurfaceView.getHolder().getSurface();

mButtonToggle = (Button) view.findViewById(R.id.toggle);

mButtonToggle.setOnClickListener(this);

}

@Override

public void onActivityCreated(Bundle savedInstanceState) {

super.onActivityCreated(savedInstanceState);

Activity activity = getActivity();

DisplayMetrics metrics = new DisplayMetrics();

activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);

mScreenDensity = metrics.densityDpi;

mMediaProjectionManager = (MediaProjectionManager)

activity.getSystemService(Context.MEDIA_PROJECTION_SERVICE);

}

@Override

public void onSaveInstanceState(Bundle outState) {

super.onSaveInstanceState(outState);

if (mResultData != null) {

outState.putInt(STATE_RESULT_CODE, mResultCode);

outState.putParcelable(STATE_RESULT_DATA, mResultData);

}

}

@Override

public void onClick(View v) {

switch (v.getId()) {

case R.id.toggle:

if (mVirtualDisplay == null) {

startScreenCapture();

} else {

stopScreenCapture();

}

break;

}

}

@Override

public void onActivityResult(int requestCode, int resultCode, Intent data) {

if (requestCode == REQUEST_MEDIA_PROJECTION) {

if (resultCode != Activity.RESULT_OK) {

Log.i(TAG, "User cancelled");

Toast.makeText(getActivity(), R.string.user_cancelled, Toast.LENGTH_SHORT).show();

return;

}

Activity activity = getActivity();

if (activity == null) {

return;

}

Log.i(TAG, "Starting screen capture");

mResultCode = resultCode;

mResultData = data;

setUpMediaProjection();

setUpVirtualDisplay();

}

}

@Override

public void onPause() {

super.onPause();

stopScreenCapture();

}

@Override

public void onDestroy() {

super.onDestroy();

tearDownMediaProjection();

}

private void setUpMediaProjection() {

mMediaProjection = mMediaProjectionManager.getMediaProjection(mResultCode, mResultData);

}

private void tearDownMediaProjection() {

if (mMediaProjection != null) {

mMediaProjection.stop();

mMediaProjection = null;

}

}

private void startScreenCapture() {

Activity activity = getActivity();

if (mSurface == null || activity == null) {

return;

}

if (mMediaProjection != null) {

setUpVirtualDisplay();

} else if (mResultCode != 0 && mResultData != null) {

setUpMediaProjection();

setUpVirtualDisplay();

} else {

Log.i(TAG, "Requesting confirmation");

// This initiates a prompt dialog for the user to confirm screen projection.

startActivityForResult(

mMediaProjectionManager.createScreenCaptureIntent(),

REQUEST_MEDIA_PROJECTION);

}

}

private void setUpVirtualDisplay() {

Log.i(TAG, "Setting up a VirtualDisplay: " +

mSurfaceView.getWidth() + "x" + mSurfaceView.getHeight() +

" (" + mScreenDensity + ")");

mVirtualDisplay = mMediaProjection.createVirtualDisplay("ScreenCapture",

mSurfaceView.getWidth(), mSurfaceView.getHeight(), mScreenDensity,

DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,

mSurface, null, null);

mButtonToggle.setText(R.string.stop);

}

private void stopScreenCapture() {

if (mVirtualDisplay == null) {

return;

}

mVirtualDisplay.release();

mVirtualDisplay = null;

mButtonToggle.setText(R.string.start);

}

}

android 截屏 简书,Android基础 截屏ScreenCapture相关推荐

  1. android 截屏 简书,Android 内置应用截屏方法

    Android 中,内置应用如何更好的截屏 在开发系统应用的时候,有时候需要用到截屏,因为 View.getDrawingCache() 截不到状态栏,所以这个方案不理想 这有一个更好的方案,就是用 ...

  2. android nfc开发 简书,Android-NFC基础

    弄懂一个东西,必须知根知底,需知其然,知其所以然. 字面理解 NFC全称:Near Field Communication(近距离无线通讯技术).因此它的使用条件仅限于近场感应,终端与标签感应距离超出 ...

  3. android自动登录简书,Android开发之简单登录界面

    用户界面基础 Android系统的四大组件分别是活动(Activity).服务(Service).广播接收器(Broadcast Receiver).内容提供器(Content Provider). ...

  4. android 读写文件 简书,Android 中的文件操作

    Android 文件操作 概述 Android 中的文件操作主要涉及到两个部分,一个是内部存储的读写,一个是外部存储的读写两者的主要区别如下表: 内部存储 外部存储 始终可用 它并非始终可用,因为用户 ...

  5. android波浪动画简书,Android贝塞尔曲线————波浪效果(大波浪)

    Hello大家好,很高兴又一次与大家见面,今天是农历丁酉鸡年(大年初四),现在跟大家拜年有点晚,算是拜晚年,祝大家晚年幸福. 这么快大伙都到了晚年了,Android贝塞尔曲线我也准备以一个大波浪来结束 ...

  6. android运行过程简书,Android系统的启动流程

    Android系统有哪些进程 在Linux系统启动时,会读取init.rc,里面配置了一些需要启动的进程.注意:SystemServer进程不在init.rc里,因为SystemServer进程是由z ...

  7. android 开发环境简书,Android Studio 开发环境快速搭建

    目录 - 1.下载JDK.AS - 1.1 JDK下载 - 1.2 AS下载 - 1.3 百度云资源统一下载 - 2.安装JDK - 3.配置JDK及JAVA的环境 - 3.1 打开系统环境 - 3. ...

  8. android 辅助服务 简书,Android AccessibilityService使用

    测试demo主要使用了Android 的无障碍辅助服务(AccessibilityService),主要注意是 1. 开启服务,绑定目标app的监听(demo中根据包命绑定了体积计算的app),需要在 ...

  9. android四大组件 简书,android四大组件

    android有四大组件,分别是Activity, Service(服务),Content Provider(内容提供者),BoadcastReceiver广播接收器. Activity 一个Acti ...

  10. android 存储方式简书,Android的多种数据存储方式

    1. 简介 作为一个完整的应用程序,数据存储操作是必不可少的.现在Android的离线缓存就是通过网络良好状态时将从服务器收到的数据保存到本地.Android系统中主要提供了3种方式进行数据存储,分别 ...

最新文章

  1. 编辑流程图_作为一名采购商,做不好采购?送你5套采购流程图模板
  2. linux系统下docker安装,Linux下Docker的安装与使用
  3. 223. Rectangle Area
  4. 0122 - EOS 编程学习日志(1)
  5. DolphinPHP
  6. python pickle库_Python使用Pickle库实现读写序列操作示例
  7. 一个简单的重命名,在git中也有这么多学问
  8. completionService
  9. 3.jenkins 基础
  10. 苹果开发者中心宕机8天终于回归
  11. Box(-and-Whisker) Plot SPSS and Excel 箱图学习
  12. 数字全息干涉偏振相移实验经验总结
  13. UDS04-清除诊断信息服务【ServiceID = 0x14】
  14. Office 2013
  15. 基于k-means聚类算法对NBA球员数据的一次聚类分析
  16. 世界上最奇葩的成人礼大盘点,被子弹蚁咬的同时还要跳十分钟舞
  17. 鸟哥的Linux私房菜(基础班)第八章学习笔记
  18. 3Dmax怎么渲染AO图?云渲染AO图是什么?
  19. 文件大小单位Bytes, KB, MB, GB, TB, PB等及换算关系,英语怎么说?
  20. Android ION 内存管理

热门文章

  1. Java Swing窗体JFrame之窗体美化-窗体及组件外观设置
  2. 网络工具之PacketTracer8安装
  3. Qt界面语言设置(官方汉化)
  4. java后端开发(二):web开发历史解读
  5. 单点登录cas常见问题(十四) - ST和TGT的过期策略是什么?
  6. 微信小程序开发图片太多怎么办
  7. 数学史思维导图_高中数学课堂教学中思维导图的制作运用
  8. 猫鼠游戏之勒索病毒来袭
  9. wps表格怎么用计算机,怎么样在wps表格中添加计算器
  10. 物流企业matlab效率,基于DEA原理的物流企业综合效率评价