[android-developers] Re: What's the best way to have your app 'sleep' for N milliseconds?
Inside your MyActivity.onCreate method: Handler timeoutHandler = new Handler() { //@Override public void handleMessage( Message msg ) { // after receiving the timeout message, start the next activity Intent intent = new Intent( MyActivity.this, MyOtherActivity.class ); startActivity( intent ); // terminate this activity if you don't want to leave it on the activity stack: MyActivity.this.finish(); super.handleMessage( msg ); } }; Message msg = new Message(); timeoutHandler.sendMessageDelayed( msg, 6 ); // milliseconds On Nov 29, 9:49 am, charlest stevegut...@gmail.com wrote: I want to display a message on the screen, have it displayed for 60 seconds, then display another message. There is nothing specific going on during the 60 seconds that I'm waiting to complete, so AsyncTask doesn't seem to apply. Does someone have a generic code snippet that does this so that I can use it in other places as needed? -- 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
Re: [android-developers] Re: What's the best way to have your app 'sleep' for N milliseconds?
Thread.sleep ( 1000 ) will make the app pause for AT LEAST 1 sec. Not exactly 1 sec. Thread is not guaranteed. Ivo On Mon, Nov 29, 2010 at 5:55 PM, dipti dvai...@gmail.com wrote: You can use java's Thread.sleep(1000) . This will make the application pause for 1 sec ( 1000 millisec ) On Nov 29, 6:49 am, charlest stevegut...@gmail.com wrote: I want to display a message on the screen, have it displayed for 60 seconds, then display another message. There is nothing specific going on during the 60 seconds that I'm waiting to complete, so AsyncTask doesn't seem to apply. Does someone have a generic code snippet that does this so that I can use it in other places as needed? -- 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.comandroid-developers%2bunsubscr...@googlegroups.com For more options, visit this group at http://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
[android-developers] Re: What's the best way to have your app 'sleep' for N milliseconds?
On Nov 30, 7:50 am, Emanuel Moecklin 1gravity...@gmail.com wrote: I would recommend using something like: This was actually unnecessarily complex. There's no need to post a whole new runnable to a handler that, in turn, sends a message to the same handler. You can just send the delayed message to the handler and you're done. Doug -- 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
[android-developers] Re: What's the best way to have your app 'sleep' for N milliseconds?
You are right: private static final int WAKE_UP_DELAY = 5000; private static final int WAKE_UP_CALL = 0; private Handler handler; handler = new Handler(new Handler.Callback() { @Override public boolean handleMessage(Message msg) { if (msg.what==WAKE_UP_CALL) { // change message, your code goes here... handler.sendMessageDelayed(handler.obtainMessage(WAKE_UP_CALL), WAKE_UP_DELAY); return true; } return false; } } ); handler.sendMessageDelayed(handler.obtainMessage(WAKE_UP_CALL), WAKE_UP_DELAY); // do something... Emanuel Moecklin 1gravity LLC On Dec 1, 4:47 am, Doug beafd...@gmail.com wrote: On Nov 30, 7:50 am, Emanuel Moecklin 1gravity...@gmail.com wrote: I would recommend using something like: This was actually unnecessarily complex. There's no need to post a whole new runnable to a handler that, in turn, sends a message to the same handler. You can just send the delayed message to the handler and you're done. Doug -- 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
[android-developers] Re: What's the best way to have your app 'sleep' for N milliseconds?
The Thread.sleep(1000) is probably not the best idea on Android because responsiveness is an issue if you care about user ratings ;-). With Thread.sleep() the app just sit's there and waits and won't respond to user input. I would recommend using something like: private static final int WAKE_UP_DELAY = 5000; private static final int WAKE_UP_CALL = 0; private Handler handler; private Runnable wakeUpCall = new Runnable() { @Override public void run() { handler.sendEmptyMessage(WAKE_UP_CALL); } }; handler = new Handler(new Handler.Callback() { @Override public boolean handleMessage(Message msg) { if (msg.what==WAKE_UP_CALL) { // change message, your code goes here... // let's start the timer again handler.postDelayed(wakeUpCall, WAKE_UP_DELAY); return true; } return false; } } ); handler.postDelayed(wakeUpCall, WAKE_UP_DELAY); // do something... The // do something part is the important difference to the Thread.sleep(). With the latter the app sleeps while with the my version the app can do whatever the app is supposed to do. And after a certain time (5 seconds in my example) you can change the message or whatever else there is to do. Emanuel Moecklin 1gravity LLC A big bang experience for our customers! On Nov 29, 2:55 pm, dipti dvai...@gmail.com wrote: You can use java's Thread.sleep(1000) . This will make the application pause for 1 sec ( 1000 millisec ) On Nov 29, 6:49 am, charlest stevegut...@gmail.com wrote: I want to display a message on the screen, have it displayed for 60 seconds, then display another message. There is nothing specific going on during the 60 seconds that I'm waiting to complete, so AsyncTask doesn't seem to apply. Does someone have a generic code snippet that does this so that I can use it in other places as needed? -- 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
[android-developers] Re: What's the best way to have your app 'sleep' for N milliseconds?
Whatever the case is, you cannor prevent the user to press the Home key to display the Homescreen, right? If you don't care about the Home button, then I guess, an Activity or a dialog should be fine. On Nov 30, 8:50 pm, Emanuel Moecklin 1gravity...@gmail.com wrote: The Thread.sleep(1000) is probably not the best idea on Android because responsiveness is an issue if you care about user ratings ;-). With Thread.sleep() the app just sit's there and waits and won't respond to user input. I would recommend using something like: private static final int WAKE_UP_DELAY = 5000; private static final int WAKE_UP_CALL = 0; private Handler handler; private Runnable wakeUpCall = new Runnable() { @Override public void run() { handler.sendEmptyMessage(WAKE_UP_CALL); } }; handler = new Handler(new Handler.Callback() { @Override public boolean handleMessage(Message msg) { if (msg.what==WAKE_UP_CALL) { // change message, your code goes here... // let's start the timer again handler.postDelayed(wakeUpCall, WAKE_UP_DELAY); return true; } return false; } } ); handler.postDelayed(wakeUpCall, WAKE_UP_DELAY); // do something... The // do something part is the important difference to the Thread.sleep(). With the latter the app sleeps while with the my version the app can do whatever the app is supposed to do. And after a certain time (5 seconds in my example) you can change the message or whatever else there is to do. Emanuel Moecklin 1gravity LLC A big bang experience for our customers! On Nov 29, 2:55 pm, dipti dvai...@gmail.com wrote: You can use java's Thread.sleep(1000) . This will make the application pause for 1 sec ( 1000 millisec ) On Nov 29, 6:49 am, charlest stevegut...@gmail.com wrote: I want to display a message on the screen, have it displayed for 60 seconds, then display another message. There is nothing specific going on during the 60 seconds that I'm waiting to complete, so AsyncTask doesn't seem to apply. Does someone have a generic code snippet that does this so that I can use it in other places as needed? -- 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
[android-developers] Re: What's the best way to have your app 'sleep' for N milliseconds?
You can use java's Thread.sleep(1000) . This will make the application pause for 1 sec ( 1000 millisec ) On Nov 29, 6:49 am, charlest stevegut...@gmail.com wrote: I want to display a message on the screen, have it displayed for 60 seconds, then display another message. There is nothing specific going on during the 60 seconds that I'm waiting to complete, so AsyncTask doesn't seem to apply. Does someone have a generic code snippet that does this so that I can use it in other places as needed? -- 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