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

问题描述 投票:-1回答:1

当我正在运行应用程序的手机启动时,我正在尝试在我的应用程序中启动服务。我添加了一个广播接收器,在清单中添加了一个intent-filter,创建了一个服务并将其添加到了清单中。但每当我启动手机时,一段时间后它会显示我的应用程序已崩溃。

需要注意的一件重要事情是,如果服务从MainActivity开始,该服务将起作用。

我在Stackoverflow上看到了更多关于此问题的问题,但这些都没有解决我的问题,因为他们中的大多数是忘记将接收器添加到清单或其他东西的人。但我不知道如何从手机启动时读取logcat输出,所以我无法确定应用程序崩溃的原因。

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并反序列化它以检查是否必须发送通知的函数。

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

android kotlin service broadcastreceiver
1个回答
0
投票

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

谢谢@Pawel

© www.soinside.com 2019 - 2024. All rights reserved.