gpt4 book ai didi

android - 手机休眠时播放声音

转载 作者:行者123 更新时间:2023-12-03 02:22:16 26 4
gpt4 key购买 nike

我正在编写一项以随机间隔提醒用户的服务。该服务由 Activity 启动,并作为服务在后台运行。

我面临的挑战是,当用户将手机置于 sleep 状态(黑屏)时,在某些情况下根本不会播放声音。时间已用完,但声音既不会按时播放,也不会在用户唤醒手机时播放。

这是一些代码:

/**
* Starts a new thread which is checking every seconds how much time has
* elapsed and if the point of time has come to play the sound.
* Once the time has come, it gets the next ring time and continues
* until it is shut down by the user
*/
private void runCheck() {
Log.i(tag, "starting runCheck");

Thread thread = new Thread() {
public void run() {

Log.i(tag, "starting LogoTimerThread");
while (vRunningFlag) {

try {

// update the notification
makeNotification();

Log.v(tag,
"Next Ring in : ["
+ helper.TimeCalculator
.duration2_hh_MM_SS((vTimerNextRing - System
.currentTimeMillis()) / 1000)
+ " sec.]");

// check if time has run out
if (vTimerNextRing < System.currentTimeMillis()) {
// reset the timer
setNextRingTime();
// update the screen
makeNotification();
// play the sound
playLTimerSound();
}

sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
Log.i(tag, "finished LogoTimerThread");
}
};

thread.start();
}

整个服务作为设置为前台的远程服务运行,因此通知提醒用户该服务,他可以通过这种方式停止它。

一旦我注释掉 playLTimerSound() ,计时器只会倒计时。不知何故,线程通过播放声音而停止。

这也是该功能:
public void playLTimerSound() {
Log.i(tag, "playLogoTimerSound - volume:" + vVolume);
MediaPlayer mp = MediaPlayer.create(this, R.raw.enterprise);
mp.setVolume(2.0f, 2.0f);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.release();
}
});
}

最佳答案

在您的场景中无法获取 SCREEN_DIM_WAKE_LOCK 吗?

@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);




PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "App");
wl.acquire();

}

关于android - 手机休眠时播放声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10600642/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com