Results 1 to 2 of 2

Thread: [Android] "WifiLock under-locked my_lock" error.

  1. #1
    Neuer skobbler
    Join Date
    04.06.2015
    Posts
    6

    [Android] "WifiLock under-locked my_lock" error.

    Hello,

    When I tried to use sdktool to download packages for offline usage my app was crashed with this error:
    Code:
    java.lang.RuntimeException: WifiLock under-locked my_lock
                at android.net.wifi.WifiManager$WifiLock.release(WifiManager.java:2147)
                at com.skobbler.ngx.sdktools.download.SKToolsDownloadPerformer.releaseResourcesWhenDownloadThreadFinishes(SKToolsDownloadPerformer.java:488)
                at com.skobbler.ngx.sdktools.download.SKToolsDownloadPerformer.run(SKToolsDownloadPerformer.java:347)
    I found already this issue here http://stackoverflow.com/questions/2...locked-my-lock and I solved this problem for me.

    But I suppose sdktool code has this bug, so I want to share it with you.

    In SKToolsDownloadPerformer class WifiLock is created but never acquired.
    Code:
    if ((downloadListener != null) && (downloadListener instanceof Activity)) {
       WifiManager wifimanager = (WifiManager) ((Activity) downloadListener).getSystemService(Context.WIFI_SERVICE);
       wifiLock = wifimanager.createWifiLock("my_lock");
    }
    So release() method throws an exception here:
    Code:
    private void releaseResourcesWhenDownloadThreadFinishes() {
       // release the WI-FI lock
       if (wifiLock != null) {
          wifiLock.release();
       }
    The solution is to check isHeld() before do any release(), or to add acquire() right after creating the lock (or both).

    Code:
    if ((downloadListener != null) && (downloadListener instanceof Activity)) {
       WifiManager wifimanager = (WifiManager) ((Activity) downloadListener).getSystemService(Context.WIFI_SERVICE);
       wifiLock = wifimanager.createWifiLock("my_lock");
       wifiLock.acquire();
    }
    Code:
    private void releaseResourcesWhenDownloadThreadFinishes() {
       // release the WI-FI lock
       if (wifiLock != null) {
          if(wifiLock.isHeld()) {
             wifiLock.release();
          }
       }
    P.S. And don't forget to add
    HTML Code:
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    in your Manifest.

    Maxim.

  2. #2
    Oberskobbler
    Join Date
    22.07.2014
    Posts
    399
    Thanks makzimi

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •