-
[Android] postDelayed()를 이용해서 주기적 메소드 실행Android 2020. 6. 16. 17:23반응형
final Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { //Do something after 100ms Toast.makeText(c, "check", Toast.LENGTH_SHORT).show(); handler.postDelayed(this, 2000); } }, 1500);
Keep life-cycle in mind
Handler handler = new Handler(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); handler.post(sendData); } @Override protected void onDestroy() { super.onDestroy(); handler.removeCallbacks(sendData); } private final Runnable sendData = new Runnable(){ public void run(){ try { //prepare and send the data here.. handler.postDelayed(this, 1000); } catch (Exception e) { e.printStackTrace(); } } };
반응형'Android' 카테고리의 다른 글
Testable API provide site for client devopment (0) 2020.06.16 Android Icon Animator & Vector Drawable Testable (0) 2020.06.16 [Android] CountDownTime 주기적 실행 타이머 (0) 2020.06.16 [Android] TextView string bold 처리 in string.xml (0) 2020.06.16 After migrating to AndroidX, Error inflating class android.support.design.widget.AppBarLayout (0) 2020.06.14