开发者

Android Timers Problem

i found this code for Android timers, public class myActivity extends Activity {

private Timer myTimer;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);

    myTimer = new Timer();
    myTimer.schedule(new TimerTas开发者_JAVA技巧k() {
        @Override
        public void run() {
            TimerMethod();
        }

    }, 0, 1000);
}

private void TimerMethod()
{
    //This method is called directly by the timer
    //and runs in the same thread as the timer.

    //We call the method that will work with the UI
    //through the runOnUiThread method.
    this.runOnUiThread(Timer_Tick);
}

private Runnable Timer_Tick = new Runnable() {
    public void run() {

    //This method runs in the same thread as the UI.               

    //Do something to the UI thread here

    }
};

} I need to understand one thing... the comments say that this method runs in the same thread as the Ui does it mean that this method will keep running even when the screen is turned off or the activity is pushed to the background for eg. in case of an incoming phone call ?


No, all this means is that particular method is run in the same thread context as THAT ACTIVITY'S UI.

Meaning, that yes, it'll run in the background as expected. (As long as your activity is running which is not always a guarantee. I.e. if a user presses home or starts any other app the OS can kill your activity at any time.

So, if you have a call, or the screen dims that particular code will be able to modify the UI via things like toasts, or its activity UI.

At least in theory, it wouldn't hurt to actually test these things yourself.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜