【问题标题】:Foreground Service get killed after Android Screen UnlockAndroid屏幕解锁后前台服务被杀死
【发布时间】:2015-01-22 14:34:42
【问题描述】:

我在前台服务中播放音乐,它与 Activity 绑定,当 Activity 运行时,如果屏幕关闭并再次打开,服务不会终止,但是 当活动不可见时意味着音乐仅在前台服务中运行,并且应用程序在后台播放音乐时关闭,当我关闭屏幕时,音乐播放正常,但是当我解锁屏幕时它杀死了服务

【问题讨论】:

  • 您是否为ACTION_SCREEN_ON 广播注册了广播接收器?
  • 不!我不想在事件中处理屏幕
  • 你有任何个广播注册吗?在 Jelly Bean 和 KitKat 中有一个 bug 会导致前台服务在其任务被用户删除后接收到后台广播时被杀死。

标签: android android-activity service foreground-service


【解决方案1】:

在@corsair992 评论中提供的页面上查看这个comment,我发现这个解决方案可以为我解决问题!

AlarmManager almgr = (AlarmManager)MyContext.getSystemService(Context.ALARM_SERVICE);
Intent timerIntent = new Intent(MyUniqueLabel);
timerIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
PendingIntent pendingOffLoadIntent = PendingIntent.getBroadcast(MyContext, 1, timerIntent, 0);

你必须做这些事情才能让它工作。

1.) 调用 addFlags 和 Intent 并将其传递到 FLAG_RECEIVER_FORGROUND

2.) 在 PendingIntent.getBroadcast 中使用非零请求代码

如果您忽略其中任何一个步骤,它将不起作用。

【讨论】:

    猜你喜欢
    • 2021-07-02
    • 2012-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-08
    相关资源
    最近更新 更多