I don't see how wl.release() could possibly not be called
Well, if nothing else, you are not handling any exceptions. If something between acquire()
and release()
raises a RuntimeException
, you will crash and leak the WakeLock
. Use this:
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
wl.acquire();
try {
// check active network
ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
// start a service that uploads data
}
finally {
wl.release();
}