You have to use start() to execute instrumentation in its own thread.
This new thread will call onStart() where you can call your
sendKeyDownUpSync(int) method. If you are using this for testing
purposes only, you can create a separate test class that extends
ActivityInstrumentationTestCase2<YourActivity> and implement your
testing accordingly.

On Apr 27, 4:16 am, "Ryan, Nguyen" <lenguyen0...@gmail.com> wrote:
> I developed an application to generate keypress. I find out that we
> can use Instrumentation object to generate. However, when I run the
> following code, I got these errors.
>
> 04-27 16:12:16.720: ERROR/AndroidRuntime(4406): Caused by:
> java.lang.RuntimeException: This method can not be called from the
> main application thread
> 04-27 16:12:16.720: ERROR/AndroidRuntime(4406):     at
> android.app.Instrumentation.validateNotAppThread(Instrumentation.java:
> 1427)
> 04-27 16:12:16.720: ERROR/AndroidRuntime(4406):     at
> android.app.Instrumentation.sendKeySync(Instrumentation.java:857)
> 04-27 16:12:16.720: ERROR/AndroidRuntime(4406):     at
> android.app.Instrumentation.sendKeyDownUpSync(Instrumentation.java:
> 871)
>
> Please help. All answers are appreciated.
>
> @Override
>         protected void onCreate(Bundle savedInstanceState) {
>                 // TODO Auto-generated method stub
>                 super.onCreate(savedInstanceState);
>                 setContentView(R.layout.main);
>         }
>
>         @Override
>         protected void onStart() {
>                 // TODO Auto-generated method stub
>                 super.onStart();
>
>                 //press key
> Instrumentation inst = new Instrumentation();
>                 inst.sendKeyDownUpSync(KeyEvent.KEYCODE_1);
>
>         }
>
>         @Override
>         protected void onStop() {
>                 // TODO Auto-generated method stub
>                 super.onStop();
>         }
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to android-developers@googlegroups.com
> To unsubscribe from this group, send email to
> android-developers+unsubscr...@googlegroups.com
> For more options, visit this group 
> athttp://groups.google.com/group/android-developers?hl=en

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to