在没有活动的情况下启动FCM服务

Dra*_*g29 9 boot service android-studio firebase-cloud-messaging

我已经按照Firebase快速入门消息传递教程,我遇到了问题.

我想在系统启动时启动两个服务(MyFirebaseMessagingServiceMyFirebaseInstanceIDService).

为此,我已将RECEIVE_BOOT_COMPLETED权限添加到我的AndroidManifest.xml.

我也把它添加到Manifest:

<receiver android:name=".AutoStart">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <action android:name="android.intent.action.QUICKBOOT_POWERON" />
    </intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)

在我的AutoStart班上,有这样的:

@Override
public void onReceive(Context context, Intent intent) {
    context.startService(new Intent(context, MyFirebaseInstanceIDService.class));
    context.startService(new Intent(context, MyFirebaseMessagingService.class));
}
Run Code Online (Sandbox Code Playgroud)

这两项服务与我上面提供的链接几乎相同.我MainActivity只包含一些Views.

但它不起作用:一旦服务启动,服务就会自动终止,我在logcat中收到这样的消息:

I/ActivityManager? Killing 3100:com.company.E/u0a85 (adj 15): empty #17
Run Code Online (Sandbox Code Playgroud)

我已经搜索了关于这个"杀戮问题"的解决方案,我想我在这里找到了一些有趣的东西(关于WakefulBroadcastReceiver).

如果这部分解决方案,我遇到了这个答案的另一个问题...... onHandleIntent()他谈到的覆盖方法是IntentService我的两个服务所在的一部分Service.

如果这不是解决方案的一部分,我不知道如何防止我的应用程序被杀死 ...