在Kotlin的BroadcastReceiver不起作用

使用本手册http://www.techotopia.com/index.php/Kotlin_Android_Broadcast_Intents_and_Broadcast_Receivers#.EF.BB.BFSummary我已经在Kotlin中实现了BroadcastReceiver,所以我期望在重新启动后,应用程序将启动,但它不会。

请帮忙。 谢谢!

广播接收器

class BroadcastReceiverOnBootComplete : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { if (intent.action.equals(Intent.ACTION_BOOT_COMPLETED, ignoreCase = true)) { val message = "Broadcast intent detected " + intent.action Toast.makeText(context, message, Toast.LENGTH_LONG).show() } } } 

清单文件

                                                

MainActivity与BroadcastReceiver

 class MainActivity : SimpleActivity(), RefreshRecyclerViewListener { private var launchers = ArrayList() private var mStoredPrimaryColor = 0 private var mStoredTextColor = 0 private var mStoredUseEnglish = false private var receiver: BroadcastReceiver? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) appLaunched() setupLaunchers() checkWhatsNewDialog() storeStateVariables() configureReceiver() fab.setOnClickListener { AddAppLauncherDialog(this, launchers) { setupLaunchers() } } } override fun onDestroy() { // super.onDestroy() unregisterReceiver(receiver) } override fun onResume() { super.onResume() if (mStoredUseEnglish != config.useEnglish) { restartActivity() return } if (mStoredTextColor != config.textColor) { getGridAdapter()?.updateTextColor(config.textColor) } if (mStoredPrimaryColor != config.primaryColor) { getGridAdapter()?.updatePrimaryColor(config.primaryColor) } updateTextColors(coordinator_layout) } override fun onPause() { super.onPause() storeStateVariables() } private fun configureReceiver() { val filter = IntentFilter() filter.addAction("com.simplemobiletools.applauncher.sendbroadcast") filter.addAction("android.intent.action.ACTION_POWER_DISCONNECTED") filter.addAction("android.intent.action.BOOT_COMPLETED") receiver = BroadcastReceiverOnBootComplete() registerReceiver(receiver, filter) } override fun onCreateOptionsMenu(menu: Menu): Boolean { menuInflater.inflate(R.menu.menu, menu) return true } override fun onOptionsItemSelected(item: MenuItem): Boolean { when (item.itemId) { R.id.settings -> launchSettings() R.id.about -> launchAbout() else -> return super.onOptionsItemSelected(item) } return true } private fun launchSettings() { startActivity(Intent(applicationContext, SettingsActivity::class.java)) } private fun launchAbout() { startAboutActivity(R.string.app_name, LICENSE_KOTLIN or LICENSE_MULTISELECT or LICENSE_STETHO, BuildConfig.VERSION_NAME) } private fun getGridAdapter() = launchers_grid.adapter as? LaunchersAdapter private fun setupLaunchers() { launchers = dbHelper.getLaunchers() checkInvalidApps() val adapter = LaunchersAdapter(this, launchers, this, launchers_grid) { val launchIntent = packageManager.getLaunchIntentForPackage((it as AppLauncher).packageName) if (launchIntent != null) { startActivity(launchIntent) finish() } else { val url = "https://play.google.com/store/apps/details?id=${it.packageName}" val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url)) startActivity(intent) } } adapter.setupDragListener(true) launchers_grid.adapter = adapter } private fun checkInvalidApps() { val invalidIds = ArrayList() for ((id, name, packageName) in launchers) { val launchIntent = packageManager.getLaunchIntentForPackage(packageName) if (launchIntent == null && !packageName.isAPredefinedApp()) { invalidIds.add(id.toString()) } } dbHelper.deleteLaunchers(invalidIds) launchers = launchers.filter { !invalidIds.contains(it.id.toString()) } as ArrayList } private fun storeStateVariables() { config.apply { mStoredPrimaryColor = primaryColor mStoredTextColor = textColor mStoredUseEnglish = useEnglish } } override fun refreshItems() { setupLaunchers() } private fun checkWhatsNewDialog() { arrayListOf().apply { add(Release(7, R.string.release_7)) checkWhatsNew(this, BuildConfig.VERSION_CODE) } } } 

因为我可以发现这个问题,他们在这里丢失了一些设置

 android:enabled="true" android:stopWithTask="false" 

所以应该是这样的