Vlad Pintea Vlad Pintea - 8 months ago 31
Android Question

Trying to stop a service after an inputted number of seconds

What I need to do is create an application which, after the user enters a number of seconds and presses a button, the service starts and the screen turns off. The screen turns on and the service stops, only after the respective number of seconds pass.

As it currently stands, whatever I input, the service just stops. Any ideas why?

MainActivity (truncated):

final EditText timer = (EditText) findViewById(R.id.insertTimer);

findViewById(R.id.startApp).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, MainService.class);
intent.putExtra("timer", timer.getText().toString());

Toast.makeText(MainActivity.this, "Countdown, Started", Toast.LENGTH_SHORT).show();

Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, 1000);


public class MainService extends Service {

String usedTimer;
long interval;

TimerTask myTask = new TimerTask() {
public void run() {
}; //TimerTask that will cause the run() runnable to happen.
Timer myTimer = new Timer(); //Timer that will make the runnable run.

private static final String TAG = "HelloService";
private boolean isRunning = false;

public void onCreate() {
registerReceiver(counter, new IntentFilter(Intent.ACTION_SCREEN_ON));
myTimer.schedule(myTask, interval);

Log.i(TAG, "Service, onCreate");
Toast.makeText(MainService.this, "Service, Created", Toast.LENGTH_SHORT).show();

isRunning = true;

private BroadcastReceiver counter = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
Toast.makeText(MainService.this, "Whoops! You've Lost.", Toast.LENGTH_SHORT).show();

public int onStartCommand(Intent intent, int flags, int startId) {
usedTimer = intent.getStringExtra("timer");

try {
interval = Long.parseLong(usedTimer);
} catch(NumberFormatException nfe) {
System.out.println("NumberFormatException: " + nfe.getMessage());

Log.i(TAG, "Service, onStartCommand");
Toast.makeText(MainService.this, usedTimer, Toast.LENGTH_SHORT).show();
return super.onStartCommand(intent, flags, startId);

public void onDestroy() {
Log.i(TAG, "Service, onDestroy");

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
| PowerManager.ON_AFTER_RELEASE, "MyWakeLock");

Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, 30000);


public IBinder onBind(Intent intent) {
Log.i(TAG, "Service, onBind");
return null;



It is because of this line:

myTimer.schedule(myTask, interval);

At this stage, interval has not been initialized because onCreate() is called before onStartCommand().


Intent i = new Intent(this, YourService.class) // onCreate() called
startService(i); // onStartCommand() called