1

I have a started service that is started from an activity of my application. I am using this service to play online radio (like TuneIn)

The problem is, when I lock down the phone;

  • If my application is on screen (started and visible), service continues to run.
  • If my application is stopped (hidden) or destroyed, service also stops running. But sometimes later it starts up again.

I checked TuneIn for this case, it runs in each situation.

How can I achieve this?

Edit:

I tried to use WakeLock but my case continues:

public void onCreate() {

 ...

 pm = (PowerManager) getSystemService(Context.POWER_SERVICE);

 wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "RadyoService WakeLock");

 wl.acquire();

 super.onCreate();
}



public void onDestroy() {

      wl.release();

      super.onDestroy();
}

And also take the permission:

<uses-permission android:name="android.permission.WAKE_LOCK" />

Can you help me to locate the error that I've made?

Thanks.

Taner
  • 4,511
  • 3
  • 18
  • 14

1 Answers1

4

You are going to need to maintain a WakeLock, to prevent the device from going into sleep mode.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I've searched this solution but it has the side effect of draining the battery, so I don't prefer. – Taner Jul 04 '13 at 06:22
  • 2
    @Taner: You act like you have a choice. You do not. – CommonsWare Jul 04 '13 at 10:51
  • I used the WakeLock as you suggest. But can not get it worked. Edited my question with the code I used. What can be the problem? – Taner Jul 05 '13 at 20:07
  • @Taner: "But can not get it worked" -- this is not a particularly useful explanation. I am assuming that you mean that your music is stopping. Are you correctly handling connectivity changes, as the device switches from WiFi to mobile data? – CommonsWare Jul 05 '13 at 20:09
  • Thanks for your fast responses. Yes you're right. Let me explain the case: While the audio from service is running, I stopped/destroyed the starter application. Then locked the phone by pressing the power button. At this point the audio gone off. But some time later the music starts to play again. (I think OS recovers the service) Besides, if I locked down the phone while the starter application is on screen, none of the above present; audio continues to work. So I couldn't match connectivity state and this case. – Taner Jul 05 '13 at 20:29