Frage

I have a quite strange issue with a cwac-wakeful library and wakelocks.

In my app, I'm using cwac-wakeful to periodically download a data from a web server and this works very well. But when somewhere in the app I acquire a wakelock (even when the set alarms were cancelled), cwac-wakeful starts to behave a little odd - it starts a job at full hours exactly every 5 minutes (14:00, 14:05...), regardless of set repeating interval.

Releasing the wakelock gives no effect and the only method to fix this is to completely restart the application.

I know that cwac-wakeful takes advantage of wakelocks while performing a job, so maybe the problem is here.

Here is a class that implements the AlarmListener.

public class ServiceWaker implements WakefulIntentService.AlarmListener
{
    private static final Logger log = Logger.getLogger(ServiceWaker.class);

    public ServiceWaker()
    {
        log.info("New ServiceWaker");
    }

    @Override
    public void scheduleAlarms(AlarmManager alarmManager, PendingIntent pendingIntent, Context context)
    {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        long syncFrequency = Long.parseLong(prefs.getString("sync_frequency", "1"));
        syncFrequency = syncFrequency * 60 * 1000;

        log.info("Alarm scheduled with a repeat interval:" + syncFrequency);

        alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                SystemClock.elapsedRealtime()+60000, syncFrequency, pendingIntent);
    }

    @Override
    public void sendWakefulWork(Context context)
    {
        log.info("Sent wakeful work");
        WakefulIntentService.sendWakefulWork(context, CheckServiceWakeful.class);
    }

    @Override
    public long getMaxAge()
    {
        return AlarmManager.INTERVAL_FIFTEEN_MINUTES*2;
    }
}

Details:

  • Target SDK: 17
  • Minimum SDK: 12
  • Compile SDK: 17
  • BuildTools version: 19.0.3
War es hilfreich?

Lösung

Late answer but I forgot to post it after I found out the solution.

The problem was an energy manager in my mobile. I have 3 battery plans available:

  • Energy saving - runs only essential services
  • Smart - prolongs the battery life by doing some optimization with wake locks, I have had this plan set to active
  • Normal - everything runs normally

"Smart" plan was responsible for this issue. As I have read somewhere, when set to active, the system tries to batch wake locks and network tasks, so the mobile is not continuously being waken. Thus my task was being re-scheduled with a longer period, in order to run it with another tasks every - for example - 15 minutes.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top