刀柄注入不适用于BroadcastReceiver [英] Hilt Injection not working with BroadcastReceiver

查看:59
本文介绍了刀柄注入不适用于BroadcastReceiver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

BroadcastReceiver 内部的

依赖项注入不起作用.我尝试使用来自 MainActivity 的警报来调用 BroadcastReceiver ,并且我收到了 UninitializedPropertyAccessException .根据文档,它应该简单到将 @AndroidEntryPoint 批注添加到接收方,而不必添加.

Dependency injection inside BroadcastReceiver using Hilt isn't working. I try to invoke a BroadcastReceiver using an alarm from MainActivity and i am getting UninitializedPropertyAccessException. According to the documentation it should be as simple as adding the @AndroidEntryPoint annotation to the receiver but its not.

示例代码:

App.kt:

@HiltAndroidApp
class App: Application() {
    override fun onCreate() {
        super.onCreate()
        Log.d(App::class.simpleName, "onCreate: Application")
    }
}

TestHiltInjection.kt:

class TestHiltInjection @Inject constructor() {

    operator fun invoke() {
        Log.d(TestHiltInjection::class.java.simpleName, "invoke called.")
    }
}

HiltBroadcastReceiver.kt:

@AndroidEntryPoint
class HiltBroadcastReceiver : BroadcastReceiver() {

    @Inject lateinit var testHiltInjection: TestHiltInjection

    override fun onReceive(context: Context?, intent: Intent?) {
        testHiltInjection()
    }
}

MainActivity.kt:

@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val manager = getSystemService(Context.ALARM_SERVICE) as AlarmManager

        val pending = PendingIntent
            .getBroadcast(this, 0, Intent(this, HiltBroadcastReceiver::class.java), 0)
        manager.setInexactRepeating(
            AlarmManager.ELAPSED_REALTIME_WAKEUP,
            15000, 15000,
            pending
        )
    }
}

输出Logcat:

kotlin.UninitializedPropertyAccessException: lateinit property testHiltInjection has not been initialized

更新

问题已在2.29.1版本上解决,只需使用 @AndroidEntryPoint

Issue was already solved on 2.29.1 version, just use @AndroidEntryPoint

推荐答案

更新:根据问题问题应该在版本 2.29.1 匕首打击.因此,只需使用 2.29.1-alpha 或更高版本.不要忘记更新 hilt-android-gradle-plugin 版本

Update: According to the issue the problem should be fixed in version 2.29.1 of Dagger Hilt. So, just use version 2.29.1-alpha or above. Don't forget to update hilt-android-gradle-plugin version as well.

原始答案::有一个GitHub issue 和一个解决方法.注入似乎无效,因为注入实际上发生在生成的父类的 onReceive()方法内部.问题在于您不能调用super方法,因为它是抽象的.但是您可以创建一个简单的包装器类来解决该问题:

Original answer: There's a GitHub issue and a workaround. It seems that injection doesn't work because it actually happens inside onReceive() method of the generated parent class. The problem is that you cannot call the super method since it's abstract. But you can create a simple wrapper class that fixes the problem:

abstract class HiltBroadcastReceiver : BroadcastReceiver() {
  @CallSuper
  override fun onReceive(context: Context, intent: Intent) {}
}

@AndroidEntryPoint
class MyBroadcastReceiver : HiltBroadcastReceiver() {
  @Inject lateinit var testHiltInjection: TestHiltInjection

  override fun onReceive(context: Context?, intent: Intent?) {
    super.onReceive(context, intent) // <-- it's the trick

    ...
  }
}

这篇关于刀柄注入不适用于BroadcastReceiver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆