S.Drumble3
S.Drumble3

Reputation: 59

Run service until device will be locked

I wanna run my service only when device is not locked.How exactly it has to work:

  1. User runs the application.

  2. Then service is run

  3. Stop working when device is locked.
  4. When user unlock device service runs again.

I'm in need of help with first 3 points. Because fourth point can be done with ACTION_USER_PRESENT. Help me please.

Upvotes: 0

Views: 232

Answers (1)

Droidman
Droidman

Reputation: 11608

1) You can start a Service by calling startService(Intent) from onCreate() or onStart() of your first Activity (alternatively, from onCreate() of your Application class).

2) Your Service is already running

3) To do so, you will need to detect the device lock event. The only approach I can think of here is listening for the ACTION_SCREEN_OFF. You can find a detailed example in the accepted answer of this question.

After you have detected the screen off action, you can stop your Service by calling stopService(Intent)

Upvotes: 2

Related Questions