I have this code:
package com.powergroupbd.timer;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class TimerActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
EditText minute, seconds;
Button fire;
String min, sec;
TextView Remain;
MyCount timercount;
Intent serviceIntent;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initializer();
fire.setOnClickListener(this);
}
private void initializer() {
minute = (EditText) findViewById(R.id.etMinute);
seconds = (EditText) findViewById(R.id.etSeconds);
fire = (Button) findViewById(R.id.bFireTimer);
Remain = (TextView) findViewById(R.id.tvRemain);
serviceIntent = new Intent(TimerActivity.this, MyService.class);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bFireTimer:
min = minute.getText().toString();
sec = seconds.getText().toString();
int MIN = Integer.parseInt(min);
int SEC = Integer.parseInt(sec);
long TIME = (MIN * 60) + SEC;
timercount = new MyCount(TIME * 1000, 1000);
timercount.start();
startService(serviceIntent);
break;
}
}
public class MyCount extends CountDownTimer {
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
// some script here
Remain.setText("0:0");
}
@Override
public void onTick(long millisUntilFinished) {
// some script here
int remseconds = (int) millisUntilFinished / 1000;
int remminuts = remseconds / 60;
remseconds = remseconds % 60;
remminuts = remminuts % 60;
Remain.setText(String.format("%d : %02d", remminuts, remseconds));
// Remain.setText(""+millisUntilFinished);
}
}
}
I am putting some numeric value in an edittext and do countdown from that value to zero. But when I restart this app it starts from the begining and have to put those values again. But I want to do that countdown in background when I am not in that application. I used service here and My service class code is here:
package com.powergroupbd.timer;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class MyService extends Service {
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
}
But nothing heppens, what to do now?
In general, there’s no way to run in background constantly. Background services are subject to termination by the OS when memory runs low. Try alarms (as in
AlarmManager) in conjuction with persistent variables (e. g. stored inPreferences).