【问题标题】:Handle network changes on android 7 Nougat在 android 7 Nougat 上处理网络更改
【发布时间】:2017-03-09 01:03:41
【问题描述】:

有没有办法在 android N 上处理 wi-fi 或蜂窝网络启用/禁用。我尝试添加带有意图过滤器 "android.net.conn.CONNECTIVITY_CHANGE" 的广播接收器,但它已被 N 及更高版本弃用。

【问题讨论】:

  • 谢谢,看了很多遍了,没看到这个Apps that are running can still listen for CONNECTIVITY_CHANGE on their main thread by registering a BroadcastReceiver with Context.registerReceiver().
  • 这里的想法是,如果您的应用已经因其他原因运行,您可以通过广播跟踪连接变化。

标签: android networking event-handling android-broadcastreceiver android-7.0-nougat


【解决方案1】:

android N 和 high 的网络更改监听器实现

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    registerReceiver(mNetworkReceiver, 
        new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
}

非常感谢 CommonsWare 有用的答案。

【讨论】:

  • 但是一旦注册了这个rec​​eiver的activity被杀死了,它就会被杀死?
  • @ParasWatts 只使用应用程序上下文
  • 我现在正在使用 firebase 作业调度程序执行此操作。工作正常。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-21
  • 1970-01-01
  • 2017-06-02
  • 1970-01-01
  • 2021-12-28
相关资源
最近更新 更多