重启后android警报管理器声音

编程入门 行业动态 更新时间:2024-10-23 04:49:11
本文介绍了重启后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.

developer.android/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:

请参见 developer.android/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警报管理器声音

本文发布于:2023-11-27 03:53:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636470.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:管理器   警报   重启   声音   android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!