Timer AutoSave
Timer timer;
TimerTask timerTask;
final Handler handler = new Handler();
+++++++++++++++++++++++++++++++++++++++++++++
protected void onResume() {
super.onResume();
startTimer();
}
public void startTimer() {
timer = new Timer();
initializeTimerTask();
timer.schedule(timerTask, 5000, 10000); //
}
public void stoptimertask() {//stoptimertask(View v)
if (timer != null) {
timer.cancel();
timer = null;
timer.purge();
try{
}
catch(Exception e){}
}
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
stoptimertask();
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
public void initializeTimerTask() {
timerTask = new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
Log.d("Simpan","Simpan data");
new save().execute();
Toast toast = Toast.makeText(getApplicationContext(), "Simpan data", 0);
toast.show();
}
});
}
};
}
+++++++++++++
String getTime(){
Calendar cal = Calendar.getInstance();
int jam = cal.get(Calendar.HOUR);
int menit= cal.get(Calendar.MINUTE);
int detik= cal.get(Calendar.SECOND);
int tgl= cal.get(Calendar.DATE);
int bln= cal.get(Calendar.MONTH)+1;
int thn= cal.get(Calendar.YEAR);
String stgl=String.valueOf(tgl)+"-"+String.valueOf(bln)+"-"+String.valueOf(thn);
String sjam=String.valueOf(jam)+":"+String.valueOf(menit)+":"+String.valueOf(detik);
String gb=stgl+", "+sjam +" WIB";
return gb;
}
Tidak ada komentar:
Posting Komentar