本文介绍了Android 4.4 KitKat系统新增加的录屏功能以及录屏方法,和限制因素。如果App由于版权方面的原因,不想被记录屏幕录像的话,APP只需要在相应的SurfaceView请求“SurfaceView.setSecure()” 即可避免自己被录下来。在文末给出了实例代码。

关于Android困扰开发者已久的截屏录像问题,我们有一个好消息,一个坏消息要和大家分享。好消息是,随着Nexus 5同步发布的Android 4.4 KitKat系统,终于可以支持屏幕录制(截屏保存成mp4)了!当然,这里说的屏幕录制,并不是开放的API,而是adb里针对Android 4.4系统,新增了screenrecord工具,开发者可以通过命令行,或是IDE中的Screen Record功能录制Android智能机运行情况的视频了。这将大大方便项目末尾编辑发布APP测评/使用指南的孩子们:)
坏消息是,新发布的android api19 SDK 中的Emulator不支持屏幕录制,换句话说,没有Nexus5的同学们,试图通过建立一个模拟器来录屏的愿望破灭了…Google搞Android SDK的Team在Stackoverflow上被鄙视惨了。
于是他们决定: 将Screenrecord工具从Emulator System Image中移除,防止造成混淆。好消极的做法-_-! 他们应该做的难道不是将screenrecord的功能在Emulator上支持起来么。
结论:
1. Android 4.4 KitKat 支持通过adb shell screenrecord 命令来录屏;
2. 模拟器上不能通过该手段录屏;
3. SurfaceView可以通过调用setSecure()防止敏感/版权信息被录屏;
详细方法和示例代码如下。
Android KitKat系统手机录屏方法:
首先当然是用USB线连接运行着Android 4.4 KitKat系统的手机;并且确保Android Tools升级到了最新版本(v19)。
然后
方法1:通过adb 命令行实现录屏
adb shell screenrecord /sdcard/yourfilename.mp4 //设置录屏后得到的mp4文件路径
//或者手动设置参数
adb shell screenrecord --bit-rate 8000000 /sdcard/yourfilename.mp4
//CTRL+C中止录屏
adb pull /sdcard/yourfilename.mp4 //从sd卡取回录制好的视频</pre>
方法2: DDMS录屏命令(其实根子上也是基于adb方式的)
可以设置录制视频的参数:
设置最终视频存储路径
有同学要问了:那如果我的软件含有版权信息,不希望其他开发者在录屏的时候将我软件的版权或敏感内容路上,这时该怎么办呢?
Google已经考虑过这个问题了,解决方案也很简单,使用SurfaceView.setSecure() 方法即可。范例代码如下:
public class SecureSurfaceViewActivity extends Activity {
private GLSurfaceView mSurfaceView;
/**
* Initialization of the Activity after it is first created. Must at least
* call {@link android.app.Activity#setContentView setContentView()} to
* describe what is to be displayed in the screen.
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
// Be sure to call the super class.
super.onCreate(savedInstanceState);
// See assets/res/any/layout/secure_surface_view_activity.xml for this
// view layout definition, which is being set here as
// the content of our screen.
setContentView(R.layout.secure_surface_view_activity);
// Set up the surface view.
// We use a GLSurfaceView in this demonstration but ordinary
// SurfaceViews also support the same secure surface functionality.
mSurfaceView = (GLSurfaceView)findViewById(R.id.surface_view);
mSurfaceView.setRenderer(new CubeRenderer(false));
// Make the surface view secure. This must be done at the time the surface view
// is created before the surface view's containing window is attached to
// the window manager which happens after onCreate returns.
// It cannot be changed later.
mSurfaceView.setSecure(true);
}
Android开发周刊: http://www.mobiletuts.me