Android Приемник вещания не запускается (Kotlin) - PullRequest
0 голосов
/ 23 января 2020

Я установил вещательный приемник через несколько недель go в Java, теперь я хочу сделать это в Kotlin, но, к сожалению, он не работает.

Мой манифест:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.test.test"
    android:installLocation="internalOnly">

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <receiver
            android:name="com.test.test.BootReceiver"
            android:enabled="true"
            android:exported="true"
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.QUICKBOOT_POWERON" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

Мой приемник:

package com.test.test

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.Intent.ACTION_BOOT_COMPLETED
import android.util.Log

class BootReceiver : BroadcastReceiver() {

    override fun onReceive(c: Context, intent: Intent?) {
        val action = intent?.action
        Log.d("ASD123123", "RECEIVED BOOT")
        val b = intent?.toUri(Intent.URI_INTENT_SCHEME)
        when (action) {
            ACTION_BOOT_COMPLETED -> startWork(c)
        }
    }


    private fun startWork(context: Context) {
        Log.d("Test", "Test")
    }
}

Когда я перезагружаю свое устройство (Android 8.0.0), оно не запускает службу. И при запуске

adb shell
am broadcast -a android.intent.action.BOOT_COMPLETED

с

am broadcast -a android.intent.action.BOOT_COMPLETED -n com.test.test/.Bootreceiver

это тоже не работает. Что не так с моим кодом?

Ответы [ 2 ]

0 голосов
/ 23 января 2020
<receiver android:name="com.example.startuptest.StartUpBootReceiver" android:enabled="true" android:exported="true">
<intent-filter>
     <action android:name="android.intent.action.BOOT_COMPLETED" />
     <category android:name="android.intent.category.DEFAULT" /> // add this line
</intent-filter>            

А в широковещательном приемнике:

private fun startWork(context: Context) {
    Log.d("Test", "Test") // remove this and toast
    Toast(context,"Test", Toast.LENGTH_SHORT).show()
} 
0 голосов
/ 23 января 2020

BOOT_COMPLETED приемник вещания устарел и больше не работает.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...