[英]WakeLock not working
我設置了喚醒鎖,以便在屏幕超時或按下屏幕鎖定按鈕時仍能聽到聲音。 通過在線閱讀我能理解的是,我只需要部分喚醒鎖。 這是代碼,但它不起作用。 沒有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();
}
};
}
如果Di Vero Labs的答案解決了這一點,我會感到驚訝,因為Android文檔說這些額外的標志對部分喚醒鎖無效。
喚醒鎖就像你使用它一樣簡單,它應該工作:
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "sleeplock");
wl.acquire();
//release
wl.release();
請注意,在執行這些方法之前,您需要檢查空值,並檢查它是否已被獲取,否則您可能會收到空指針異常。
我認為你的問題是你正在調用cleanup(),其中包括wakelock發布,所以你並沒有真正拿着喚醒鎖。 為了確保這個工作(盡管可能會導致電池耗盡問題),請先保留你的喚醒鎖,然后在完成后釋放,而不是在你完成之前,你顯然沒有喚醒鎖。
我遇到了很多問題,因為喚醒鎖沒有工作,但我相信我已經把問題釘在了自定義ROM上。 所以要小心! (DONT支持自定義ROM,包括你自己的!)
嘗試:
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" />
在你的清單中宣布。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.