简体   繁体   English

Android前台服务在某些情况下被杀死

[英]Android foreground service being killed under certain conditions

I have a Service used for playing audio. 我有一个用于播放音频的服务。 It is created with START_STICKY , and calls startForeground() when it is told to play music, and stopForeground() when it is told to stop playing music. 它与创建START_STICKY ,并调用startForeground()时,被告知要播放音乐, stopForeground()时,被告知要停止播放音乐。

The service continues to run fine and play music, even after the app is swiped out of the task manager on 4.2.1. 即使在4.2.1上将应用程序从任务管理器中刷出后,该服务仍可以正常运行并播放音乐。 However, the service is inexplicably killed if the app is swiped out and the app's home/lockscreen widget is interacted with or if I try to sendBroadcast() within the service. 但是,如果应用程序被刷出并且与应用程序的主sendBroadcast() /锁屏小部件进行了交互,或者如果我尝试在服务中发送sendBroadcast() ,则该服务将被莫名其妙地杀死。

I cannot find anything that would cause a crash, no low memory warnings, the Service is not bound to anything, just: 我找不到任何会导致崩溃的内容,没有低内存警告,该服务未绑定任何内容,只是:

10-09 17:14:42.186: I/ActivityManager(2591): Killing 18400:com.xperia64.timidityae/u0a10079: remove task
10-09 17:14:42.236: W/ActivityManager(2591): Scheduling restart of crashed service com.xperia64.timidityae/.MusicService in 5000ms

The dumpsys activity services command always produces this, as it should be: dumpsys活动服务命令始终会产生此结果,因为它应该是:

Proc # 8: adj=prcp /FS trm= 0 22326:com.xperia64.timidityae/u0a10079 (fg-service)

My app does some heavy JNI MIDI->wav processing, but even if I play a basic MP3 with a MediaPlayer , the killing of the service still occurs. 我的应用程序执行了一些繁重的JNI MIDI-> wav处理,但是即使我使用MediaPlayer播放了基本的MP3,该服务的终止仍然会发生。 Also, I have a custom notification with buttons if that makes a difference. 另外,如果有区别的话,我会有一个带有按钮的自定义通知。 I have also tried forcing a PARTIAL_WAKELOCK , but that did not help either. 我也尝试过强制PARTIAL_WAKELOCK ,但这也没有帮助。

Found this workaround here: https://code.google.com/p/android/issues/detail?id=53313#c1 在这里找到此解决方法: https : //code.google.com/p/android/issues/detail?id=53313#c1

In the foreground service: 在前台服务中:

@Override
public void onTaskRemoved( Intent rootIntent ) {
  Intent intent = new Intent( this, DummyActivity.class );
  intent.addFlags( Intent.FLAG_ACTIVITY_NEW_TASK );
  startActivity( intent );
}

In the manifest: 在清单中:

<activity
  android:name=".DummyActivity"
  android:theme="@android:style/Theme.NoDisplay"
  android:enabled="true"
  android:allowTaskReparenting="true"
  android:noHistory="true"
  android:excludeFromRecents="true"
  android:alwaysRetainTaskState="false"
  android:stateNotNeeded="true"
  android:clearTaskOnLaunch="true"
  android:finishOnTaskLaunch="true"
  /> 

In DummyActivity.java: 在DummyActivity.java中:

public class DummyActivity extends Activity {
    @Override
    public void onCreate( Bundle icicle ) {
        super.onCreate( icicle );
        finish();
    }
}

Here's a workaround that seems pretty reliable and doesn't close the recents screen when your swipe the app's task away: 这是一种似乎很可靠的解决方法,当您将应用程序的任务移开时, 它不会关闭“最近显示”屏幕:

In every broadcast sent by your app to your app (also including PendingIntents), add the Intent.FLAG_RECEIVER_FOREGROUND flag to the intent being broadcast. 在您的应用发送到您的应用的每个广播中(还包括PendingIntents),将Intent.FLAG_RECEIVER_FOREGROUND标志添加到正在广播的意图中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM