Skip to content Skip to sidebar Skip to footer

Android Wakelock Not Released After Getactivenetworkinfo

I want to automatically check for an internet connection every x minutes and send data to a server. However, the following (minimal) code gives a warning in Eclipse, saying that th

Solution 1:

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:

PowerManagerpm= (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLockwl= pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
wl.acquire();

try {
  // check active networkConnectivityManagercm= (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
  NetworkInfoinfo= cm.getActiveNetworkInfo();
  // start a service that uploads data
}
finally {
  wl.release();
}

Post a Comment for "Android Wakelock Not Released After Getactivenetworkinfo"