Need code example on how to run an Android service forever in the background even when device sleeping, like Whatsapp?

后端 未结 5 1983
死守一世寂寞
死守一世寂寞 2020-11-30 19:51

I have tried various ways to achieve this, but my service eventually gets killed.

I want to use AlarmManager to trigger a class every one hour. Even if the device is

5条回答
  •  日久生厌
    2020-11-30 20:35

    Follow these easy steps to keep servce alive forever in android device. 1. Call a service by using alarm manager. 2. return START_STICKY in onStart method. 3. In on destroy call the alarm manager and restart service by using startService method. 4.(Optional)Repeat the point number 3 in onTaskRemoved method.

提交回复
热议问题