带有 BroadcastReceiver 的应用程序在启动时触发时崩溃

App with BroadcastReceiver crashes when triggered on boot

当 运行 所在的 phone 应用程序启动时,我正在尝试在我的应用程序中启动服务。我已经添加了一个 broadcastreceiver,在清单中添加了一个 intent-filter,创建了一个服务并将其也添加到清单中。但是每当我启动 phone 时,过一会儿它就会显示我的应用程序崩溃了。

需要注意的重要一点是,如果服务是从 MainActivity 启动的,则该服务可以正常工作。

我在 Whosebug 上看到了更多关于此的问题,但其中 none 解决了我的问题,因为他们中的大多数人忘记将接收器添加到清单或其他东西。 但我也不知道如何读取 logcat-输出,从 phone 开始时开始,所以我无法确定是什么导致应用程序崩溃。

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="nl.arnovanliere.nuntia">

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <uses-feature android:name="android.hardware.location.gps" />

    <application
        android:allowBackup="true"
        android:fullBackupContent="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:resizeableActivity="false"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsPictureInPicture="false"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        tools:ignore="GoogleAppIndexingWarning">

        <receiver
            android:name=".receivers.BroadcastReceiver"
            android:enabled="true"
            android:exported="true"
            android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>


        <service
            android:name=".services.CheckMessagesService"
            android:enabled="true"
            android:exported="true"
            android:permission="false" />

        <activity
            android:label="@string/app_name"
            android:name=".MainActivity"
            android:theme="@style/AppTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="AIzaSyBhlLDqLSihI41pIs-ELuomRWUv6513CeE" />
    </application>

</manifest>

BroadcastReceiver.kt

class BroadcastReceiver : BroadcastReceiver() {
    @SuppressLint("UnsafeProtectedBroadcastReceiver")
    override fun onReceive(context: Context?, intent: Intent?) {
        context?.startService(Intent(context, CheckMessagesService::class.java))
        Log.d(LOG_TAG, "onReceive BroadcastReceiver called")
    }
}

CheckMessagesService

class CheckMessagesService : Service() {

    override fun onBind(intent: Intent): IBinder? {
        return null
    }

    override fun onCreate() {
        Log.d(LOG_TAG, "onCreate of service called")
        super.onCreate()
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        Log.d(LOG_TAG, "onStartCommand of service called")
        val runnable = Runnable {
            checkMessages()
            // Only need to check for messages every minute
            Thread.sleep(60000)
        }

        // New thread for checking messages, otherwise the UI-thread would be blocked
        val thread = Thread(runnable)
        thread.start()

        return super.onStartCommand(intent, flags, startId)
    }

    override fun onDestroy() {
        Log.d(LOG_TAG, "onDestroy of service called")
        super.onDestroy()
    }

checkMessages() 只是一个调用 API 并反序列化它以检查是否必须发送通知的函数。

我希望你们中的一个能帮助我。

对于任何未来的观众:问题是我不得不使用 startForegroundService() 而不是 startService() 因为我在 Android Oreo 上 运行 它。

感谢@Pawel