NetworkInfo is not properly responding to network changes on phone running Nougat

佐手、 提交于 2019-12-04 08:05:24

According to android docs,

Apps targeting Android 7.0 (API level 24) and higher do not receive CONNECTIVITY_ACTION broadcasts if they declare the broadcast receiver in their manifest. Apps will still receive CONNECTIVITY_ACTION broadcasts if they register their BroadcastReceiver with Context.registerReceiver() and that context is still valid.

So what you need to do is start a Service, create a new instance of the Broadcast receiver NetworkChangeReceiver and register it to listen it to android.net.conn.CONNECTIVITY_CHANGE by using

registerReceiver(new NetworkChangeReceiver(), new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE"))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!