title: Android Java code to mimic a 2 Keystroke sequence (to perform a device screenshot)
body: <p>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.</p>
  
  <p>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.</p>
  
  <p>I also know from digging around there are these intents: android.intent.action.SCREEN_OFF &amp; android.intent.category.HOME</p>
  
  <p>(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).</p>
  
  <p>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)...</p>
  
  <p>More: Here is a snip of the code I am attempting:</p>
  
  <p>Button click for test... ...</p>
  
  <pre><code>  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
  </code></pre>
  
  <p>Doesnt work as the inst.sendKeyDownUpSync is wrong as I need a sendKeyDown (&amp; hold) behavior or its equivel</p>
  
  <p>Many thanks for any advise.  If I do get this working, I will post the solution here.  Cheers GH </p>
  
  <p>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 ?</p>
  
  <p>EDIT (MORE)... 9/24/11</p>
  
  <p>More. Still not working but I am heading down this path &amp; think it is closer...</p>
  
  <p>// Attempt to SIMULATE A Long press (DOWN) + HOME to tell the HTC to invoke the 'Screenshot' command (WARNING: HTC Tablet specific behavior!)</p>
  
  <pre><code>        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(); 
        ...
  </code></pre>
  
  <p>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 <em>really</em> want.  Through some log examinations (when you Long-Power + Home click on HTC) a program called 'com.htc.mysketcher' (FlashActivity) is being called...</p>
  
  <p>Again, if I figure this out then I will post to the group... Cheers GH</p>