How to set a timer in android

前端 未结 13 1939
天命终不由人
天命终不由人 2020-11-22 06:43

What is the proper way to set a timer in android in order to kick off a task (a function that I create which does not change the UI)? Use this the Java way: http://docs.ora

13条回答
  •  忘掉有多难
    2020-11-22 07:08

    yes java's timer can be used, but as the question asks for better way (for mobile). Which is explained Here.


    For the sake of StackOverflow:

    Since Timer creates a new thread it may be considered heavy,

    if all you need is to get is a call back while the activity is running a Handler can be used in conjunction with a

    Runnable:

    private final int interval = 1000; // 1 Second
    private Handler handler = new Handler();
    private Runnable runnable = new Runnable(){
        public void run() {
            Toast.makeText(MyActivity.this, "C'Mom no hands!", Toast.LENGTH_SHORT).show();
        }
    };
    ...
    handler.postAtTime(runnable, System.currentTimeMillis()+interval);
    handler.postDelayed(runnable, interval);
    

    or a Message

    private final int EVENT1 = 1; 
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {         
            case Event1:
                Toast.makeText(MyActivity.this, "Event 1", Toast.LENGTH_SHORT).show();
                break;
    
            default:
                Toast.makeText(MyActivity.this, "Unhandled", Toast.LENGTH_SHORT).show();
                break;
            }
        }
    };
    
    ...
    
    Message msg = handler.obtainMessage(EVENT1);
    handler.sendMessageAtTime(msg, System.currentTimeMillis()+interval);
    handler.sendMessageDelayed(msg, interval);
    

    on a side note this approach can be used, if you want to run a piece of code in the UI thread from an another thread.

    if you need to get a call back even if your activity is not running then, you can use an AlarmManager

提交回复
热议问题