唤醒锁不起作用

时间:2013-10-02 21:05:09

标签: android wakelock

我之前发过这个问题,没有人可以回答,所以我再次尝试,因为这个问题使我的应用毫无价值。当屏幕超时或用户点击电源按钮时,我需要声音继续播放。我已经阅读了几乎所有关于唤醒锁的在线帖子,我可以找到它,但我无法让它工作。下面是基于用户选择的输入播放声音的.Java文件之一。一切都很好,除了当屏幕变暗时声音停止播放。请注意,我对此非常陌生,所以如果这段代码是草率或多余的,请告诉我。

package com.androidsleepmachine.gamble;

import android.app.Activity;
import android.content.Context;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.PowerManager;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

public class Ship extends Activity implements View.OnClickListener {
public static final Integer[] TIME_IN_MINUTES = { 30, 45, 60, 180, 360 };
public MediaPlayer mediaPlayer;
public Handler handler = new Handler();
public Button button2;
public Spinner spinner2;
public PowerManager.WakeLock wl;


// Initialize the activity
@Override
public void onCreate(Bundle savedInstanceState) {
    PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
    PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, 
"Playwhenoff");

    super.onCreate(savedInstanceState);
     wl.acquire();
    setContentView(R.layout.ship);
    button2 = (Button) findViewById(R.id.btn2);
    button2.setOnClickListener(this);
    spinner2 = (Spinner) findViewById(R.id.spinner2);
    ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(this,
            android.R.layout.simple_spinner_item, TIME_IN_MINUTES);

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner2.setAdapter(adapter);

}

// Play the sound and start the timer
private void playSound(int resourceId) {
    // Cleanup any previous sound files
    cleanup();
    // Create a new media player instance and start it

    mediaPlayer = MediaPlayer.create(this, resourceId);
    mediaPlayer.start();
    // Create the timer to stop the sound after x number of milliseconds
    int selectedTime = TIME_IN_MINUTES[spinner2.getSelectedItemPosition()];
    handler.postDelayed(runnable, selectedTime * 60 * 1000);
}

// Handle button callback
@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.btn2:
        playSound(R.raw.ocean_ship);
        break;
    }
}

protected void onStop() {

    cleanup();
    super.onStop();
}

// Stop the sound and cleanup the media player
public void cleanup() {
    if (mediaPlayer != null) {
        mediaPlayer.stop();
        mediaPlayer.release();
        mediaPlayer = null;
        wl.release();
    }
    // Cancel any previously running tasks
    handler.removeCallbacks(runnable);
}

// Runnable task used by the handler to stop the sound
public Runnable runnable = new Runnable() {
    public void run() {
        cleanup();
    }
};


@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    wl.release();
}

}

2 个答案:

答案 0 :(得分:1)

这可能与WakeLock无关。当屏幕关闭时,您的活动可能会被onStop()调用。

音频播放器通常使用服务进行音频播放,因此播放可以独立于此类UI问题运行。

答案 1 :(得分:0)

您正在释放wakeLock onPause - &gt;

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    wl.release(); // -> This is the line where you release the wakelock
}

当您的设备“进入睡眠状态”时,它就像onPause - &gt;的onStop。

如果您在挂机时释放唤醒锁,则无法在关闭屏幕的情况下播放音乐,因为系统不会让您的CPU处于唤醒状态。

将它释放到其他地方(也许是onDestroy?),它应该可以工作。