应用程序启动时未触发广播接收器类

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

我需要帮助。我有 4 个广播接收器:

SimCardStateReceiver
AutoTimeReceiver
AutoTimeZoneReceiver
TimeZoneReceiver
。 目前,仅触发
SimCardStateReceiver
。所有其他接收器都没有被触发(我调试了我的代码,这就是我注意到除了我在每个广播接收器中添加的日志之外没有触发其他接收器的方式)。请问我做错了什么?我已经分享了所有
BroadcastReceiver

class TimeZoneReceiver : BroadcastReceiver() {

    override fun onReceive(context: Context, intent: Intent) {
        if (intent.action == Intent.ACTION_TIMEZONE_CHANGED) {
           isWestAfricaTimeZoneSet(context)
        }
    }

    private fun isWestAfricaTimeZoneSet(context: Context) {
        val baseActivity = context as? BaseActivity
        val viewModel = baseActivity?.simCardViewModel

        val desiredTimeZone = "West Africa Standard Time"
        val currentTimeZone = TimeZone.getDefault().displayName
        val compareTimeZone = currentTimeZone.equals(desiredTimeZone, ignoreCase = true)

        if(viewModel?.zoneCheck == true){
            if(compareTimeZone) {
                viewModel.setShowStringDialog("Please Ensure TimeZone is set to West Africa Standard Time.")
            }
        }
    }
}

:::

 class AutoTimeReceiver : BroadcastReceiver() {

    override fun onReceive(context: Context, intent: Intent) {
       // logEText("AUTOTIME_RECEIVER", intent.action.toString())
        if (intent.action == "com.mypackageName.ACTION_AUTO_TIME_CHANGED") {
           isAutoTimeEnabled(context)
        }
    }

    private fun isAutoTimeEnabled(context: Context) {
        val baseActivity = context as? BaseActivity
        val viewModel = baseActivity?.simCardViewModel
        val isAutoTimeEnabled = Settings.Global.getInt(context.contentResolver, Settings.Global.AUTO_TIME) == 1

        if(viewModel?.timeCheck == true){
            if(!isAutoTimeEnabled)
                viewModel.setShowStringDialog(context.getString(R.string.time_setup))
        }
    }
}

::::

 class AutoTimeZoneReceiver : BroadcastReceiver() {

    override fun onReceive(context: Context, intent: Intent) {
        if (intent.action == "com.mypackageName.ACTION_AUTO_TIME_ZONE_CHANGED") {
           isAutoTimeZoneEnabled(context)
        }
    }

    private fun isAutoTimeZoneEnabled(context: Context) {
        val baseActivity = context as? BaseActivity
        val viewModel = baseActivity?.simCardViewModel
        val isAutoTimeZone = Settings.Global.getInt(context.contentResolver, Settings.Global.AUTO_TIME_ZONE) == 1

        if(viewModel?.timeZoneCheck == true){
            if(!isAutoTimeZone) {
                viewModel.setShowStringDialog(context.getString(R.string.time_zone_setup))
            }
        }
    }
}

我有一个 BaseActivity 类,它由我的项目中的所有活动扩展,这就是我注册所有广播者的方式

 open class BaseActivity : AppCompatActivity() {
        val prefStore: PrefStore by inject()
        val simCardViewModel: SimCardViewModel by viewModel()
    
        private var progDialog: Dialog? = null
        private val intentSim = IntentFilter("android.intent.action.SIM_STATE_CHANGED")
        val intentFilterAutoTime = IntentFilter("com.mypackageName.ACTION_AUTO_TIME_CHANGED")
        val intentFilterAutoTimeZone = IntentFilter("com.mypackageName.ACTION_AUTO_TIME_ZONE_CHANGED")
        val intentFilterTimeZone = IntentFilter(Intent.ACTION_TIMEZONE_CHANGED)
    
        private val simCardStateReceiver = SimCardStateReceiver()
        private val autoTimeReceiver = AutoTimeReceiver()
        private val autoTimeZoneReceiver = AutoTimeZoneReceiver()
        private val timeZoneReceiver = TimeZoneReceiver()
    
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            val crashlytics = FirebaseCrashlytics.getInstance()
            crashlytics.setUserId(prefStore.uuid)
            crashlytics.didCrashOnPreviousExecution()
            crashlytics.log(this.javaClass.simpleName)
        }
    
        private fun observer() {
            simCardViewModel.showStringDialog.observe(this) { message ->
                if (progDialog != null && progDialog!!.isShowing) {
                    progDialog?.dismiss()
                }
                if (message.contains("restart device")) {
                    setupDialog(false, message)
                } else {
                    setupDialog(true, message)
                }
               
            }
    
        }
    
        override fun onStart() {
            super.onStart()
            observer()
        }
        private fun registerReceivers() {
            registerReceiver(simCardStateReceiver, intentSim)
            registerReceiver(autoTimeReceiver, intentFilterAutoTime)
            registerReceiver(autoTimeZoneReceiver, intentFilterAutoTimeZone)
            registerReceiver(timeZoneReceiver, intentFilterTimeZone)
        }
    
        private fun unregisterReceivers() {
            unregisterReceiver(simCardStateReceiver)
            unregisterReceiver(autoTimeReceiver)
            unregisterReceiver(autoTimeZoneReceiver)
            unregisterReceiver(timeZoneReceiver)
        }
    
        override fun onResume() {
            super.onResume()
            registerReceivers()
        }
    
    
        override fun onPause() {
            try {
                unregisterReceivers()
            } catch (e: Exception) {
                logEText("BROADCASTER->PAUSE", e.message.toString())
            }
            super.onPause()
        }
    
        private fun setupDialog(isTimeSetup: Boolean = true, titleString: String) {
            progDialog = Dialog(this)
            progDialog?.requestWindowFeature(Window.FEATURE_NO_TITLE)
            progDialog?.setContentView(R.layout.dialog_setup_screen)
            progDialog?.setCancelable(false)
            val title = progDialog?.findViewById(R.id.title) as TextView
            val settingButton = progDialog?.findViewById(R.id.return_btn) as Button
    
            if (!isTimeSetup) {
                settingButton.visibility = View.GONE
                title.text = titleString
            }
            settingButton.setOnClickListener {
                progDialog?.dismiss()
                startActivity(Intent(Settings.ACTION_DATE_SETTINGS))
              
            }
            progDialog?.show()
        }
    }

请任何人都可以帮助我查看我的代码并告诉我为什么只有

SimCardStateReceiver
显示其日志,而其他 3 个 BroadcastReceiver 没有响应?

android kotlin broadcastreceiver
1个回答
0
投票

我相信你需要专门调用 sendBroadcast 才能使其工作:

Intent().also { intent ->
     intent.setAction("com.example.broadcast.MY_NOTIFICATION")
     intent.putExtra("data", "Nothing to see here, move along.")
     sendBroadcast(intent)
}

https://developer.android.com/guide/components/broadcasts

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