2016년 2월 21일 일요일

[안드로이드 개발] 일정시간 지난 다음 다른 Activity로 이동하기

Handler

일정시간 지난 다음 다른 Activity로 이동할 수 있도록 도와주는 Class입니다.

A Handler allows you to send and process Message and Runnable objects associated with a thread's MessageQueue. Each Handler instance is associated with a single thread and that thread's message queue. When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it -- from that point on, it will deliver messages and runnables to that message queue and execute them as they come out of the message queue.
핸들러는 앱이 메세지를 프로세스에 보내거나 스레드의 메세지큐와 실행객체를 연결시킬 수 있습니다.
각 핸들러는 하나의 스레드와 그 스레드내의 메세지큐와 관련됩니다. 핸들러는 생셩후 스레드/스레드내 메세지큐와 연결되어야 합니다. ...........중략
There are two main uses for a Handler: (1) to schedule messages and runnables to be executed as some point in the future; and (2) to enqueue an action to be performed on a different thread than your own.
두 가지 사용법이 있습니다. (1) 메세지와 실행객체가 장래 특정시점에 실행되도록 예약하는 것과 (2) 다른 스레드에서 어떤 동작이 실행되도록 메세지큐에 더하는 방법이 있습니다.
Scheduling messages is accomplished with the post(Runnable), postAtTime(Runnable, long), postDelayed(Runnable, long), sendEmptyMessage(int), sendMessage(Message), sendMessageAtTime(Message, long), and sendMessageDelayed(Message, long) methods.
예약 메세지는 다음의 메소드들를 사용하여 완성하여야 합니다.
* 참조사이트: http://developer.android.com/reference/android/os/Handler.html#postDelayed(java.lang.Runnable, long)
여기서는 지연후 실행 postDelayed를 사용합니다.
public class StartActivity extends AppCompatActivity {

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_start);
        Handler mHandler = new Handler();        mHandler.postDelayed(new Runnable() {
            //Do Something            @Override            public void run() {
                // TODO Auto-generated method stub                Intent intent = new Intent(StartActivity.this, MainActivity.class);                startActivity(intent );
                finish();            }
        }, 2000); 
    }
}

댓글 없음:

댓글 쓰기