我设置了唤醒锁,以便在屏幕超时或按下屏幕锁定按钮时仍能听到声音。从我通过在线阅读可以理解的是,我只需要一个部分唤醒锁。以下是代码,但它不起作用。无logcat错误
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;
private PowerManager.WakeLock wl;
// Initialize the activity
@Override
public void onCreate(Bundle savedInstanceState) {
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "sleeplock");
super.onCreate(savedInstanceState);
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:
wl.acquire();
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();
}
};
}发布于 2014-01-21 01:02:42
如果Di Vero Labs的答案解决了这个问题,我会感到惊讶,因为Android文档中说,这些额外的标志对部分尾锁没有任何作用。
wakelock就像你使用它一样简单,它应该可以工作:
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "sleeplock");
wl.acquire();
//release
wl.release(); 请注意,在执行这些方法之前,您将需要检查是否为空,并检查是否尚未获取它,等等,否则可能会得到空指针异常。
我认为你的问题是你正在调用cleanup(),它包含了wakelock释放,所以你并不是真的持有一个wakelock。为了确保它能正常工作(虽然可能会导致电池耗尽的问题),提前握住你的唤醒锁,然后在你完成时释放,否则你显然不会有唤醒锁。
我遇到了一大堆问题,因为wakelocks不工作,但我相信我已经把问题解决到了一个定制的ROM上。所以要当心!(不支持自定义ROM,包括您自己的!)
发布于 2013-09-29 16:36:53
尝试:
wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK |
PowerManager.ACQUIRE_CAUSES_WAKEUP |
PowerManager.ON_AFTER_RELEASE, "sleeplock");我发现"PowerManager.ACQUIRE_CAUSES_WAKEUP“缓解了我遇到的一些相同的问题。
还要确保:
<uses-permission android:name="android.permission.WAKE_LOCK" />已在您的清单中声明。
https://stackoverflow.com/questions/19074074
复制相似问题