0

我从来没有做过太多的 Java 编程,大部分都是 PHP / RUBY / Javascript,所以我不完全确定如何访问wLock.release()from onDestroy

public class SlammedNavigatorActivity extends DroidGap {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.loadUrl("file:///android_asset/www/index.html");

        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        PowerManager.WakeLock wLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "SN Wake Lock");
        wLock.acquire();
    }

    public void onDestroy() {
        wLock.release();
    }
}
4

3 回答 3

4

您应该将局部变量转换wLock为私有字段:

public class SlammedNavigatorActivity extends DroidGap {
  private PowerManager.WakeLock wLock;


  public void onCreate(Bundle savedInstanceState) {
    // ...
    wLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "SN Wake Lock");
    // ...
  }
}
于 2012-05-04T10:32:59.300 回答
1

使其成为实例变量:

public class SlammedNavigatorActivity extends DroidGap {

    private PowerManager.WakeLock wLock;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.loadUrl("file:///android_asset/www/index.html");

        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        this.wLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "SN Wake Lock");
        wLock.acquire();

    }

    public void onDestroy() {
        this.wLock.release();
    }
}

为什么在做 Android 编程之前不学习语言基础知识?阅读Java 教程,或一本关于 Java 的好书。

于 2012-05-04T10:34:08.457 回答
0

使 wLock 成为类成员

public class SlammedNavigatorActivity extends DroidGap {

    private PowerManager.WakeLock wLock;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.loadUrl("file:///android_asset/www/index.html");

        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        wLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "SN Wake Lock");
        wLock.acquire();

    }

    public void onDestroy() {

        wLock.release();
    }
}
于 2012-05-04T10:34:19.197 回答