重启后android警报管理器声音 [英] android alarm manager sound after reboot

查看:112
本文介绍了重启后android警报管理器声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用android的AlarmManager开发类似服务的应用程序。该应用程序基本上连接到服务器并调用远程过程。如果返回的值为True,它将播放声音:

I'm working on a service-like application using android's AlarmManager. The app basically connects to a server and calls a remote procedure. If the value returned is True, it plays a sound:

MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_RING); 
try {
    mediaPlayer.setDataSource(getApplicationContext(), 
     Uri.parse("file:///system/media/audio/ringtones/Rolling_tone.ogg"));
}
...
mediaPlayer.start();

当手动启动应用程序时,此方法可以正常工作。但是,当电话重启时(我已经实现了BroadcastReceiver),声音播放了大约一秒钟,然后立即被打断。

This works all and well when the application is started manually. However when the phone is rebooted (I've implemented the BroadcastReceiver), the sound is played for like a second then gets interrupted immediately.

听起来几乎像是在播放被某种东西切断。我需要停止并重新启动该应用程序才能使其再次正常运行。

It sounds almost like the play cycle is being cut off by something. I need to stop and re-start the application to have it working correctly again.

关于可能原因的任何线索吗?

Any clues on what the cause could be?

推荐答案

您可以使用 MediaPlayer setScreenOnWhilePlaying(),以确保在后台播放音频时您的应用程序不会进入睡眠状态。

You could use MediaPlayer's setScreenOnWhilePlaying() to ensure that your application doesn't go to sleep while the audio plays in the background.

http://developer.android.com/reference/android/media/MediaPlayer.html#setWakeMode %28android.content.Context,%20int%29

另一种可能的解决方案是使用 PowerManager 使用 WakeLock 使设备保持唤醒状态:

Another possible solution is the use the PowerManager to keep your device awake with a WakeLock:

请参见 http://developer.android.com/reference/android/os/PowerManager.html

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
wl.acquire();
   ... wait until sound has finished playing (with listener)
wl.release();

这篇关于重启后android警报管理器声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆