android 如何将广播Intent与Jetpack的ComposeComponentActivity以及addOnNewIntentListener回调一起使用

aiazj4mn  于 2023-01-07  发布在  Android
关注(0)|答案(1)|浏览(256)

我正在Kotlin和Jetpack Compose中重写一个应用程序,它来自我之前构建的React Native应用程序,我似乎无法接收到另一个处理条形码扫描硬件的应用程序正在广播的Intent。我正在使用相同的AndroidManifest.xml来过滤这些相同的意图。这在我构建的另一个应用程序中是有效的,但我现在使用的是更新的SDK和库。我看到的唯一主要区别是,这是使用ComponentActivity,而旧应用程序使用AppCompatActivity (example here)
我添加了一些日志记录,以查看它是否被调用,我看到“设置意图..”被调用,但我使用addOnNewIntentListener添加的回调似乎没有被调用。
由于生命周期活动,我是否遗漏了某些内容或需要调用其他内容?

package com.android.composefolderscanner
 import android.annotation.SuppressLint
 import android.content.Intent
 import android.os.Bundle
 import android.util.Log
 import androidx.activity.ComponentActivity
 import androidx.activity.compose.setContent
 import androidx.activity.viewModels
 import androidx.compose.runtime.DisposableEffect
 import androidx.compose.runtime.LaunchedEffect
 import androidx.compose.runtime.mutableStateOf
 import androidx.compose.runtime.remember
 import androidx.core.util.Consumer
 import com.android.composefolderscanner.services.*
 import com.android.composefolderscanner.ui.FolderApp
 import com.android.composefolderscanner.ui.screens.ScanViewModel
 import com.android.composefolderscanner.ui.theme.IgniteFolderScannerTheme
 import dagger.hilt.android.AndroidEntryPoint
 import javax.inject.Inject

@AndroidEntryPoint
class MainActivity : ComponentActivity() {

   private val viewModel: ScanViewModel by viewModels()
   

   @SuppressLint("MissingPermission", "UnusedMaterialScaffoldPaddingParameter")
   override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      // setContent 
      setContent {
         val resultV = remember { mutableStateOf<Int?>(101) }
         // Use a disposable effect
         DisposableEffect(key1 = resultV.value) {
            Log.i("MainActivity","Setting up intent...")
            val listener = Consumer<Intent>{
               Log.i("MainActivity","Receiving intent...")
               val decodedData = it?.getStringExtra("com.symbol.datawedge.data_string")
               if (decodedData!= null){
                  viewModel.addBarcode(decodedData)
               }
            }
            // Hook callback
            addOnNewIntentListener(listener)
            onDispose {
               Log.i("MainActivity","Removing intents...")
               removeOnNewIntentListener(listener) }
            }
 

         IgniteFolderScannerTheme(useDarkTheme = true, content = {
            FolderApp( )
         })
      }
   }
}
iyzzxitl

iyzzxitl1#

看起来它可能在上下文可用之前过早地设置回调。将其 Package 在peekAvailableContext()中后,现在可以工作了

DisposableEffect(key1 = result.value) {
            peekAvailableContext().let {
               Log.i("MainActivity","Setting up intent...")
               val listener = Consumer<Intent>{
                  Log.i("MainActivity","Receiving intent...")
                  val decodedData = it?.getStringExtra("com.symbol.datawedge.data_string")
                  if (decodedData!= null){
                     viewModel.addBarcode(decodedData)
                  }
               }
               addOnNewIntentListener(listener)
               onDispose {
                  Log.i("MainActivity","Removing intents...")
                  removeOnNewIntentListener(listener)
               }
            }
         }

相关问题