2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Android で指定した時間後に処理を行う方法についての話

Last updated at Posted at 2022-05-27

はじめに

Android で指定された時間後に処理を実行する方法についての備忘録まとめとなります。
「10 秒後に処理を実行したい!」「1 分間隔で繰り返し実行したい。」などの時に実装します。

Android Developers Reference

10 秒後に処理を実行する

// Java
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        // do something
    }
}, 10 * 1000);
// Kotlin
val handler = Handler(Looper.getMainLooper())
handler.postDelayed({
    // do something
}, 10 * 1000)

1 分間隔で処理を繰り返し実行する

// Java
final int delayMillis = 1 * 60 * 1000;
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        // do something

        handler.postDelayed(this, delayMillis);
    }
}, delayMillis);
// Kotlin
val delayMillis: Long = 1 * 60 * 1000
val handler = Handler(Looper.getMainLooper())
val runnable = object : Runnable{
    override fun run() {
        // do something

        handler.postDelayed(this, delayMillis)
    }
}
handler.postDelayed(runnable, delayMillis)
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?