Vlad Pintea
Vlad Pintea

Reputation: 71

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() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, MainService.class);
            intent.putExtra("timer", timer.getText().toString());
            startService(intent);

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

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

MainService:

    public class MainService extends Service {

    String usedTimer;
    long interval;

    TimerTask myTask = new TimerTask() {
        public void run() {
            stopSelf();
        }
    }; //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;

    @Override
    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() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(MainService.this, "Whoops! You've Lost.", Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    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);
    }

    @Override
    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.ACQUIRE_CAUSES_WAKEUP
                | PowerManager.ON_AFTER_RELEASE, "MyWakeLock");
        wakeLock.acquire();

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

        android.os.Process.killProcess(android.os.Process.myPid());
    }

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

Upvotes: 0

Views: 40

Answers (1)

Omar Aflak
Omar Aflak

Reputation: 2962

It is because of this line:

myTimer.schedule(myTask, interval);

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

AFAIK:

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

Upvotes: 2

Related Questions