Android的前台服务被在一定条件下丧生 [英] Android foreground service being killed under certain conditions

查看:538
本文介绍了Android的前台服务被在一定条件下丧生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有用于播放音频服务。它与 START_STICKY 创建,调用 startForeground()时,被告知要播放音乐, stopForeground()时,被告知要停止播放音乐。

该服务将继续运行良好和播放音乐,应用程序被刷出任务管理器4.2.1即使经过。但是,如果应用程序是刷出来,应用主/屏幕锁定小部件相互作用或如果我尝试 sendBroadcast()的服务范围内。

我找不到任何会导致崩溃,没有内存不足警告,该服务不绑定到任何东西,只是:

  10-09 17:14:42.186:I / ActivityManager(2591):杀死18400:com.xperia64.timidityae / u0a10079:删除任务
10-09 17:14:42.236:W / ActivityManager(2591):在5000毫秒坠毁服务com.xperia64.timidityae / .MusicService调度重启

该dumpsys活动服务命令总是产生这一点,因为它应该是:

  PROC#8的:a = PRCP / FS TRM = 0 22326:com.xperia64.timidityae / u0a10079(FG-服务)

我的应用程序确实有些沉重JNI MIDI-> WAV处理,但即使我打了的MediaPlayer 基本的MP3,服务杀害仍会发生。另外,我有一个按钮,自定义通知,如果有差别。我也试图迫使 PARTIAL_WAKELOCK ,但这并没有帮助。


解决方案

在这里找到这种解决方法:的 HTTPS://$c$c.google.com/p/android/issues/detail ID = 53313#C1

在前台服务:

  @覆盖
公共无效onTaskRemoved(意向rootIntent){
  意向意图=新意图(这一点,DummyActivity.class);
  intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  startActivity(意向);
}

在清单:

 <活动
  机器人:名字=。DummyActivity
  机器人:主题=@安卓风格/ Theme.NoDisplay
  机器人:启用=真
  机器人:allowTaskReparenting =真
  机器人:noHistory =真
  机器人:excludeFromRecents =真
  机器人:alwaysRetainTaskState =假
  机器人:stateNotNeeded =真
  机器人:clearTaskOnLaunch =真
  机器人:finishOnTaskLaunch =真
  />

在DummyActivity.java:

 公共类DummyActivity延伸活动{
    @覆盖
    公共无效的onCreate(捆绑冰柱){
        super.onCreate(冰柱);
        完();
    }
}

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.

The service continues to run fine and play music, even after the app is swiped out of the task manager on 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.

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:

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. 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.

解决方案

Found this workaround here: 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:

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

这篇关于Android的前台服务被在一定条件下丧生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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