我已经阅读了几篇有关新“打瞌睡”的帖子。 Android M中的模式,也是Android开发者网站上的文章:https://developer.android.com/training/monitoring-device-state/doze-standby.html但我仍然遇到在手机上正确运行Foreground Service的问题(Android 6.0.1)。
问题:
我有一个处理使用MediaPlayer对象播放音乐的服务,Service Implements onCompletion
和onPrepared
MediaPlayer接口。
伪代码:
public class MusicService extends Service implements MediaPlayer.OnPreparedListener,
MediaPlayer.OnCompletionListener, MediaPlayer.OnErrorListener {
// Class Variables/Methods etc...
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
// update notification using startForeground()
}
@Override
public void onCompletion(MediaPlayer mp) {
// Go and get another music track in separate thread
// (Have used AsyncTask and RX Java/RX Android to test if problem is here - works fine)
}
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
// handleError
return false;
}
}
当手机插入电源,或者屏幕打开,或者应用程序已打开且屏幕已开启时,该应用程序运行良好。然而,当手机闲置并播放音乐时(5到40分钟之间),一旦调用onCompletion(),服务就无法检索下一个音轨。
服务已设置并符合以下条件:
startForeground
PARTIAL_WAKE_LOCK
prepareAsync()
(在后台线程中执行下一个跟踪uri的逻辑,仅在主线程中调用prepareAsync())我尝试过的事情:
我有一个logcat输出,从手机离开时,在屏幕关闭的情况下保持一段时间不变,并且UNPLUGGED(一旦我将其重新插入并且更新了日志就获得了logcat),这表明了问题。
06-15 02:31:39.346 musicplayer:music_service E/MusicService: onPrepared : Tom Petty GH - 11 - You Got Lucky
06-15 02:31:39.548 musicplayer:music_service D/MusicService: Releasing wakelock
06-15 02:35:15.467 musicplayer:music_service E/MusicService: onCompletion called
06-15 02:35:15.467 musicplayer:music_service D/MusicService: Acquiring wakelock
06-15 02:35:15.514 musicplayer:music_service D/NextActionStrategy: Retrieving song - rx java - Main Ui Thread =false
06-15 02:35:15.870 musicplayer:music_service D/NextActionStrategy: Song Retrieved : Ten Years- Main Ui Thread = true
06-15 02:35:15.950 musicplayer:music_service E/MusicService: onPrepared : Ten Years
06-15 02:35:16.149 musicplayer:music_service D/MusicService: Releasing wakelock
06-15 02:38:59.002 musicplayer:music_service E/MusicService: onCompletion called
06-15 02:38:59.002 musicplayer:music_service D/MusicService: Acquiring wakelock
06-15 02:38:59.049 musicplayer:music_service D/NextActionStrategy: Retrieving song - rx java - Main Ui Thread =false
06-15 02:38:59.211 musicplayer:music_service D/NextActionStrategy: Song Retrieved : What Am I- Main Ui Thread = true
06-15 02:50:07.642 musicplayer:music_service E/MusicService: onPrepared : What Am I
06-15 02:50:07.976 musicplayer:music_service D/MusicService: Releasing wakelock
06-15 02:50:22.097 musicplayer:music_service D/MusicService: onStartCommand called : action.PAUSE
例程将是
从日志中可以看到02:38:59.xxx onCompletion被调用,找到了一个轨道,并返回到调用prepareAsync()的主线程,但是在我手动唤醒之前12分钟没有任何反应打电话,然后只停止代码继续停止并开始播放曲目。
我不相信其设备,因为我使用的其他音乐应用程序在这种情况下工作正常,但我仅限于在一个真实设备上进行测试:(
如果你有这个远,感谢与我的关系!任何建议将不胜感激(希望有关解决方案,而不是我的音乐品味!)。
答案 0 :(得分:0)
我只是想让你看看我是如何实现我的startForeground()
方法的。也许这可以提供帮助:
void setUpAsForeground(String text) {
mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, Main.class), 0);
NotificationCompat.Builder noti = new NotificationCompat.Builder(this)
.setContentTitle( "My App Title")
.setContentText(text)
.setSmallIcon(R.drawable.ic_stat_recording)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(text))
.setPriority(Notification.PRIORITY_MAX)
.setContentIntent(contentIntent)
.setOngoing(false);
final Notification notification = noti.build();
//mNotificationManager.notify(NOTIFICATION_ID, noti.build());
startForeground(NOTIFICATION_ID, notification);
}
在onCreate()
:
@Override
public void onCreate() {
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"MyWakelockTag");
wakeLock.acquire();
***********
}