简体   繁体   English

如何从BroadcastReceiver隐藏和显示MainActivity上的视图

[英]How can I hide and show views on a MainActivity from a BroadcastReceiver

So I'm running into an error with my BroadcastREceiver...the receiver is running and detecting the change in network..as soon as I add the visiblity it cans out... 所以我的BroadcastREceiver遇到错误...接收器正在运行并检测网络的变化..一旦添加可见性,它就会消失...

Where it is canning out is at the change of visibility of the warning_message in the 罐头化的地方是警告消息中可见性的变化

 val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        val networkInfo = connectivityManager.activeNetworkInfo
        if (networkInfo != null && networkInfo.detailedState == NetworkInfo.DetailedState.CONNECTED) {
            Log.d("Network Connected","NC is connected")
                MainActivity().warning_message.visibility = View.INVISIBLE
            MainActivity().isConnected = true
        } else if (networkInfo != null) {
            val state = networkInfo.detailedState
            Log.d("NetworkReceiver", state.name)
        } else {
            MainActivity().warning_message.visibility = View.VISIBLE
            MainActivity().isConnected = false
            Log.d("Network Connected","NC")
        }

    }

The warning message is just a: 警告消息只是:

 <FrameLayout
        android:id="@+id/warning_message"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:visibility="invisible"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/top_toolbar">

in the Main Activity layout. 在主要活动布局中。

The error I receive is: Process: com.example.user.appname, PID: 19972 我收到的错误是:进程:com.example.user.appname,PID:19972

java.lang.RuntimeException: Error receiving broadcast Intent { act=android.net.conn.CONNECTIVITY_CHANGE flg=0x4000010 (has extras) } in com.example.user.appname.ConnectionReceiver@531d646 java.lang.RuntimeException:接收com.example.user.appname.ConnectionReceiver@531d646中的广播意图{act = android.net.conn.CONNECTIVITY_CHANGE flg = 0x4000010(has extras)}时出错

There is no specific error message as to why. 没有关于原因的特定错误消息。

Don't call MainActivity directly from the broadcast receiver. 不要直接从广播接收机调用MainActivity。 Use an event bus, and post and event to the activity from the broadcast receiver. 使用事件总线,并从广播接收器向事件发布事件。 I have already answered to a question like yours here , where I show (lol basically copying and pasting what I read on greenrobot's github) how to do it. 我已经在这里回答了像您这样的问题,在这里我演示了(大体上是复制并粘贴我在greenrobot的github上阅读的内容)如何做到这一点。 Now I get that you are using Kotlin, so you can end the installation of Greerobot's event bus by reading this . 现在,我知道您正在使用Kotlin,因此您可以通过阅读this结束Greerobot事件总线的安装。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 BroadcastReceiver 中访问 MainActivity 的对象或方法? - How can I access MainActivity's objects or methods inside BroadcastReceiver? 如何从 AlarmManager BroadcastReceiver 调用 MainActivity 方法? - How to call MainActivity method from AlarmManager BroadcastReceiver? 如何从broadcastReceiver回调访问MainActivity - How to access MainActivity from broadcastReceiver callback 如何将数组从broadcastReceiver类发送到MainActivity - How to send an array from broadcastReceiver class to MainActivity 如何将属性从BroadcastReceiver1发送到BroadcastReceiver2? - How can I send property from BroadcastReceiver1 to BroadcastReceiver2? 如何在MainActivity(或应用程序)的开头隐藏片段? - How can I hide a fragment on start of my MainActivity( or the application)? 如何在弹出 AlertDialog 时隐藏 MainActivity? - How Can i hide the MainActivity while pop up AlertDialog? 如何从ListFragment显示/隐藏Activity视图 - How to show/hide Activity views from a ListFragment 如何从扩展的BroadcastReceiver类调用MainActivity函数 - How to call MainActivity function from the extended BroadcastReceiver class 如何在BroadcastReceiver中获取mainactivity的SharedPreferences? - How to get SharedPreferences of the mainactivity in BroadcastReceiver?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM