My Activity starts a service by calling startservice()
. To simplify my problem lets say the service will be a counter, and the counter will be increased in ever
You need to hold lock
if your service has to be running in the background
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl =
pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");
wl.acquire();
// when you done
wl.release();
Better way is to use AlarmManager
because keep the service running all the time will drain the battery and waste the system resources.