Android Java 代码模拟 2 次击键序列(以执行设备屏幕截图)
几天来我一直在尝试获取 SurfaceView 的位图屏幕截图,但随着我深入研究,目前似乎没有针对我的 HTC 设备基于 Android OS 2.3.4 的操作系统的解决方案。
接下来是 B 计划,我刚刚在其中发现了另一篇博客:“在我的 HTC Evo 3d 上,我所要做的就是按住电源按钮 1-2 秒,然后按主页按钮,然后就可以截屏了。不需要应用程序。”事实证明这在我的平板电脑上完美运行。
我还通过挖掘知道有这些意图: android.intent.action.SCREEN_OFF & android.intent.category.HOME
(所以我尝试了一堆代码实验来尝试模仿代码中的2键组合以在这个暴力庄园中获取屏幕截图。不幸的是没有成功)。
那么我的? -- 有谁对从 java 代码为我的 HTC 设备调用此“屏幕截图序列”的方法有任何见解吗? (假设我需要欺骗操作系统,让它认为我按住电源键并同时点击主页键)...
更多:这是我正在尝试的代码片段:
单击按钮进行测试... .. 不起作用,
Thread t = new Thread() {
public void run() {
Instrumentation inst = new Instrumentation();
inst.sendKeyDownUpSync(KeyEvent.KEYCODE_POWER);
Instrumentation inst2 = new Instrumentation();
inst2.sendKeyDownUpSync(KeyEvent.KEYCODE_HOME);
} // run
}; // thread t
因为 inst.sendKeyDownUpSync 是错误的,因为我需要 sendKeyDown (&hold)行为或其等效行为
非常感谢您的任何建议。如果我确实能做到这一点,我将在这里发布解决方案。干杯 GH
PS;我想这背后有一些自定义意图吗?是否有系统日志可供查看调用树以找出它是什么?
编辑(更多)... 2011 年 9 月 24 日
更多。仍然没有工作,但我正在沿着这条路前进&认为它更接近...
//尝试模拟长按(向下)+ HOME告诉HTC调用“屏幕截图”命令(警告:HTC平板电脑特定行为!)
Thread tt = new Thread() {
public void run() {
final KeyEvent dapowerkey = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_POWER);
Handler onesecondhandler = new Handler();
onesecondhandler.postDelayed(new Runnable() {
public void run() {
// fpr about 1 second send power down keystrokes (DOWN ONLY)
while (true) { dispatchKeyEvent(dapowerkey); }
} // we are done running on the timer past time point
}, 750); // 3/4 second key press
// send the HOME keystroke
Instrumentation inst1 = new Instrumentation();
inst1.sendKeyDownUpSync(KeyEvent.KEYCODE_HOME);
} // outer thread run tp mpt block the GUI
}; // outer thread t
tt.start();
...
还想如果我可以直接发送正确的意图到设备上的正确位置,我可以直接启动屏幕捕获功能(这是我真正想要的。通过一些日志检查(当您长按 + Home 单击 HTC )一个名为'com.htc.mysketcher' (FlashActivity) 正在被调用...
再次,如果我弄清楚了这一点,那么我将发布到该组...干杯 GH
I have been trying to get a bitmap screenshot of a SurfaceView for days but the more I look into it, there doesn't seem to be a solution at present for Android OS 2.3.4 based OSs my device from HTC.
So on to Plan B, where I just found out another blog: "On my HTC Evo 3d, all I have to do is hold the power button for 1-2 sec and then hit the home button and it takes a screen shot. No app required." Turns out this works perfectly on my tablet.
I also know from digging around there are these intents: android.intent.action.SCREEN_OFF & android.intent.category.HOME
(So I tried a bunch of code experiments to try to mimic the 2-key combo in code to get a screenshot in this brute force manor. Unfortunately without success).
So my ? -- Does anyone have any insights into a method to invoke this 'screenshot sequence' for my HTC device from java code? (Presume I need to fool the OS into thinking I am holding down the power key AND tap the Home key, simultaneously)...
More: Here is a snip of the code I am attempting:
Button click for test... ...
Thread t = new Thread() {
public void run() {
Instrumentation inst = new Instrumentation();
inst.sendKeyDownUpSync(KeyEvent.KEYCODE_POWER);
Instrumentation inst2 = new Instrumentation();
inst2.sendKeyDownUpSync(KeyEvent.KEYCODE_HOME);
} // run
}; // thread t
Doesnt work as the inst.sendKeyDownUpSync is wrong as I need a sendKeyDown (& hold) behavior or its equivel
Many thanks for any advise. If I do get this working, I will post the solution here. Cheers GH
PS; I presume there is some custom intent under the hood doing this? Is there a system log somewhere to trey to peek at the call tree to find out what it is ?
EDIT (MORE)... 9/24/11
More. Still not working but I am heading down this path & think it is closer...
// Attempt to SIMULATE A Long press (DOWN) + HOME to tell the HTC to invoke the 'Screenshot' command (WARNING: HTC Tablet specific behavior!)
Thread tt = new Thread() {
public void run() {
final KeyEvent dapowerkey = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_POWER);
Handler onesecondhandler = new Handler();
onesecondhandler.postDelayed(new Runnable() {
public void run() {
// fpr about 1 second send power down keystrokes (DOWN ONLY)
while (true) { dispatchKeyEvent(dapowerkey); }
} // we are done running on the timer past time point
}, 750); // 3/4 second key press
// send the HOME keystroke
Instrumentation inst1 = new Instrumentation();
inst1.sendKeyDownUpSync(KeyEvent.KEYCODE_HOME);
} // outer thread run tp mpt block the GUI
}; // outer thread t
tt.start();
...
Also thought if I can send the right intent directly to the proper place on the device that I might be able to kick off a screen capture function directly (which is what I really want. Through some log examinations (when you Long-Power + Home click on HTC) a program called 'com.htc.mysketcher' (FlashActivity) is being called...
Again, if I figure this out then I will post to the group... Cheers GH
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论