唤醒锁定释放不起作用

时间:2014-06-19 17:32:45

标签: android button crash wakelock android-wake-lock

我必须创建一个使用唤醒锁保持屏幕的应用程序。我有两个按钮,一个用于打开唤醒锁定,另一个用于关闭它。

用于打开唤醒锁定的按钮工作正常,但关闭唤醒锁定的按钮会导致应用程序在按下时崩溃。

以下是代码:

    public class MainActivity extends ActionBarActivity {


@Override @SuppressWarnings("deprecation")
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    setupScreenONButton();

    setupScreenOFFButton();


}

@SuppressWarnings("deprecation")
private void setupScreenOFFButton() {

    Button ScreenOffButton = (Button) findViewById(R.id.buttonScreenOFF);

    ScreenOffButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
            PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE,"Screen OFF");
            wl.release();
            Toast.makeText(MainActivity.this, "Screen OFF", Toast.LENGTH_SHORT).show();

        }
    });


}

@SuppressWarnings("deprecation")
private void setupScreenONButton() {

    Button ScreenOnButton = (Button) findViewById(R.id.buttonScreenON);

    ScreenOnButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
            PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP,"Screen ON");
            wl.acquire();
            Toast.makeText(MainActivity.this,"Screen ON", Toast.LENGTH_SHORT).show();

        }
    });




}

关于我做错的任何想法? 谢谢

2 个答案:

答案 0 :(得分:4)

你需要发布之前获得的相同的Wakelock,而不是新的。

编辑:添加代码(未经测试):

    public class MainActivity extends ActionBarActivity {

PowerManager.WakeLock wl;

@Override @SuppressWarnings("deprecation")
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE,"Screen OFF");

    setContentView(R.layout.activity_main);

    setupScreenONButton();

    setupScreenOFFButton();


}

@SuppressWarnings("deprecation")
private void setupScreenOFFButton() {

    Button ScreenOffButton = (Button) findViewById(R.id.buttonScreenOFF);

    ScreenOffButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            wl.release();
            Toast.makeText(MainActivity.this, "Screen OFF", Toast.LENGTH_SHORT).show();

        }
    });


}

@SuppressWarnings("deprecation")
private void setupScreenONButton() {

    Button ScreenOnButton = (Button) findViewById(R.id.buttonScreenON);

    ScreenOnButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            wl.acquire();
            Toast.makeText(MainActivity.this,"Screen ON", Toast.LENGTH_SHORT).show();

        }
    });




}

答案 1 :(得分:-1)

上述解决方案将有效。添加此保护条件以避免锁定或锁定,即您不应尝试解锁先前未锁定的锁定,反之亦然。

if (w1.isHeld())
w1.release();

类似地

if (w1.isHeld())
w1.aquire();

它也是在OnResume()方法中释放锁定的良好编程习惯。 希望它有所帮助!