Download - GKAC 2015 Apr. - Android Looper
Android Looper차영호 @ganachoco
발표자 소개
Agenda● Java VS Android● Where is main?● Event Loop● Create and Handle Event with Message● Advanced Topic
Android Looper● Event Loop for Android● 공기 같은 존재
○ 어디에나 있지만, 여간해선 사용할 일은 없다.○ 사용할 일은 없지만, 모르면 가끔씩 해결이 곤란할 때가 생긴다.
Java vs Android
Start Entry
public class Hello { public static void main(String[] s) { System.out.println("Hello, World"); }}
public class HelloActivity extends Activity { @Override public void onCreate(Bundle bundle) { super.onCreate(bundle); setContentView(R.layout.activity_hello); }}
Start EntryC:\ > java HelloHello, World
????
간단한 호출경로 확인 방법(Log)
간단한 호출경로 확인 방법(Log)
main()package android.app;
public final class ActivityThread {
public static void main(String[] args) {
// blahblah
Looper.loop();
throw new RuntimeException("Main thread loop unexpectedly exited");
}
}
Event Loopwhile (true) {
// do someting
}
Event Loopwhile (true) {
msg = waitAndGetMessage();
handler = msg.getTarget();
handler.handleMessage(msg);
}
간단한 호출경로 확인 방법(Log)
ANR이 반겨줍니다.
Events● Component Lifecycle Event
○ Activity○ Service
● Intent Handler○ BroadcastReceiver.onReceive
● View layout & draw● Input Event
Event Loop● android.os.Message● android.os.Handler● android.os.Looper
○ android.os.HandlerThread
Message@Overridepublic void onClick(View v) { setText("Text 1");}
private static final int MSG_HANDLE_CHANGE_TEXT = 1;
public void setText(String text) { // Message msg = new Message(); // bad usage Message msg = mHandler.obtainMessage(MSG_HANDLE_CHANGE_TEXT, text); mHandler.sendMessage(msg);}
Handlerprivate mHandler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case MSG_HANDLE_TEXT_CHANGED: mTextView.setText((String)(msg.obj)); break; default: break; } }}
Call FlowLooper.loop onClick setText mHandler.sendMessage
... mHandler.handleMessage mTextView.setText
시간
직구를 배웠습니다
변화구도 배워봅시다
Image Source: https://mirror.enha.kr/wiki/야구의 구종
Message Queue
Image Source: MSDN
sendMessageAtFrontOfQueue()
Message Queue
Image Source: MSDNremoveMessages()
Message Queue
Image Source: MSDN
sendMessageDelayed()
sendMessageAtTime()
Requirement
● 안드로이드앱이 시작하고나서 1분 후에 팝업을 띄워주세요.
Solution
Advanced Topic● Wakelock● AlarmManager
Wakelock● View.setKeepScreenOn()● WakeLock
○ FULL_WAKE_LOCK○ SCREEN_BRIGHT_WAKE_LOCK○ SCREEN_DIM_WAKE_LOCK○ PARTIAL_WAKE_LOCK
AlarmManager
● ELAPSED_REALTIME● ELAPSED_REALTIME_WAKEUP● RTC● RTC_WAKEUP
Q&A