Skip to content

Instantly share code, notes, and snippets.

@bgloh
Created July 7, 2017 05:42
Show Gist options
  • Save bgloh/4c03442550522429d42e21edc1e2a669 to your computer and use it in GitHub Desktop.
Save bgloh/4c03442550522429d42e21edc1e2a669 to your computer and use it in GitHub Desktop.
Java periodic timer handler
package com.example.brian.myapplication_timer_handler;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
Handler handler = new Handler();
int timeRemaining = 100000; // 100 second
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
handler.post(runnable);
}
private Runnable runnable = new Runnable() {
@Override
public void run() {
// repeat unttil time remaining is greater than zero.
timeRemaining = timeRemaining - 5000; //
if (timeRemaining > 0)
handler.postDelayed(runnable,5000);
Log.d("bgloh", "handlier is working");
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment