hqt
hqt

Reputation: 30266

Android : WakefulIntentService does not release wake lock

Here is the code that I use in BroadCastReceiver:

public class SMSBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) { 
        // SMSReceivedService is my custom service, extends from Service class  
        Intent myIntent = new Intent(context, SMSReceivedService.class);
        myIntent.putExtras(intent);
        WakefulIntentService.sendWakefulWork(context, myIntent);
    }
}

public class SMSReceivedService extends extends WakefulIntentService {
    public SMSReceivedService(String name) {
        super(name);
    }

    @Override
    protected void doWakefulWork(Intent intent) {
        // main code here
    }
}

Here is the piece of code that's inside WakefulIntentService

synchronized private static PowerManager.WakeLock getLock(Context context) {
    if (lockStatic == null) {
        PowerManager mgr =
                (PowerManager)context.getSystemService(Context.POWER_SERVICE);
        lockStatic=mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, NAME);
        lockStatic.setReferenceCounted(true);
    }
    return(lockStatic);
}

public static void sendWakefulWork(Context ctxt, Intent i) {
    getLock(ctxt.getApplicationContext()).acquire();
    ctxt.startService(i);
}

As who has used WakefullIntentService will know that, just call sendWakefulWork and WakefullIntentService will do all stuff in the background. But in code above, WakefullIntentService just hold wake lock, but after finish, I don't see any code that release this lock. Is it true ? So it will affect the Android User. Please give me ideas.

Upvotes: 0

Views: 2169

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006564

Either you are using my WakefulIntentService, or you invented your own with the same name. If you are using my WakefulIntentService, the lock is released in onHandleIntent(), as you can tell by reading the source code.

Upvotes: 2

Related Questions