【问题标题】:background service has stopped on miui rommiui rom 后台服务已停止
【发布时间】:2017-01-29 06:11:14
【问题描述】:

我正在开发一个后台服务,并且在 OnDestroy 方法中,我调用了一个意图来重新启动我的服务。 miui rom(小米手机和华为手机)我没有重新开始。

我该如何处理?

public class NotificationsService extends Service {

@Override
public void onCreate() {
    ApplicationLoader.postInitApplication();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    return START_STICKY;
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}

public void onDestroy() {
    Intent intent = new Intent("example.app.start");
    sendBroadcast(intent);
}
}

在清单中:

    <receiver android:name=".AppStartReceiver" android:enabled="true">
        <intent-filter>
            <action android:name="example.app.start" />
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

【问题讨论】:

  • 你发布广播接收器代码了吗?
  • @W4R10CK 还没有,因为 stackoverflow 说:您每 90 分钟只能发布一次。 ://
  • @AliKouroshfar 请看看这个stackoverflow.com/a/56335831/5685911
  • 正如您所提到的,这是在不同供应商的定制 ROM 中非常常见的问题。并且解决方案已经可用@Akki 建议了。

标签: android android-service


【解决方案1】:

这对小米来说并不新鲜,因为小米有一个叫做应用权限的功能,用户必须允许应用自动启动(服务)。

像这样让你的应用自动启动:

Settings &gt; permissions &gt; Autostart

或者,

不要尝试在onDestroy() 中重新启动相同的Service,而是在onStartCommand(Intent intent, int flags, int startId) 方法中使用START_STICKY

再次发送广播而不启动服务,请正确使用onDestroy

@Override
public void onDestroy() {
Intent intent = new Intent("example.app.start");
sendBroadcast(intent);
super.onDestroy();
}

【讨论】:

  • 这是真的。这对小米来说并不新鲜。但是有没有办法在没有 Mi 设置的情况下以编程方式使服务保持活动状态?
  • 我正在使用 OnDestroy 测试 START_STICKY,但无法正常工作并且我的服务已停止!
  • 不,它是 MIUI 中的一个记忆术语,用户必须自动启动选定的应用程序,并且不要在 onDestroy 中使用 START_STICKYonStartCommand 中使用。
  • @AliKouroshfar 您正在发送广播而不是启动服务
  • 这是真的。我正在发送广播。但我有一个接收器接收到我的广播并启动我的服务
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-17
  • 2019-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多