本文介紹了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