NetworkInfo无法正确响应运行牛轧糖的电话上的网络更改 [英] NetworkInfo is not properly responding to network changes on phone running Nougat
问题描述
前言:在运行6.0.1的电话上,此方法工作正常.但在运行7.1.1的手机上却没有,如下所示.
Preface: This is working just fine on a phone running 6.0.1. but on my phone that is running 7.1.1, it is not, as shown below.
build.gradle(Module:app)
compileSdkVersion 25
buildToolsVersion "25.0.0"
applicationId "com.gesslar.threshvote"
minSdkVersion 19
targetSdkVersion 25
AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<receiver
android:name=".NetworkChangeReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.net.wifi.WIFI_STATE_CHANGED"/>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
</receiver>
NetworkUtil.java 我有一个BroadcastReceiver,可用来简单地调用NetworkUtil.updateNetworkStatus(context);
NetworkUtil.java I have a BroadcastReceiver that I use to simply call NetworkUtil.updateNetworkStatus(context);
public static void updateNetworkStatus(Context context) {
final ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
if (activeNetwork == null) {
Log.d(TAG, "NO CONNECTION INFORMATION");
return;
}
if (activeNetwork.isConnected()) {
Log.d(TAG, "WE ARE CONNECTED TO A NETWORK");
} else {
Log.d(TAG, "WE ARE NOT CONNECTED TO A NETWORK");
return;
}
Log.d(TAG, "NETWORK TYPE: " + activeNetwork.getTypeName());
}
从WiFi和数据连接开始,我已经进行了以下测试 关闭WiFi
03-08 20:15:04.664 29796-29796/com.gesslar.threshvote D/NetworkUtil: NO CONNECTION INFORMATION
打开WiFi:
03-08 20:15:26.333 29796-29796/com.gesslar.threshvote D/NetworkUtil: WE ARE CONNECTED TO A NETWORK
03-08 20:15:26.333 29796-29796/com.gesslar.threshvote D/NetworkUtil: NETWORK TYPE: MOBILE
关闭数据:
No log line written
打开数据:
No log line written
关闭WiFi,然后关闭数据:
1) 03-08 20:17:13.029 29796-29796/com.gesslar.threshvote D/NetworkUtil: NO CONNECTION INFORMATION
2) No log line written
同时关闭两者,打开WiFi:
03-08 20:18:00.198 29796-29796/com.gesslar.threshvote D/NetworkUtil: NO CONNECTION INFORMATION
^ showed as soon as it started seeking for WiFi, but no further log line was written when WiFi became active.
我完全被结果迷住了.我看到其他地方应该在运行时绑定它,但是,我唯一的活动是首选项"活动.该应用程序不打算具有运行时UI,而是使用后台IntentService和Notifications.
I am absolutely bemused by the results. I saw somewhere else that I should bind it at runtime, however, my only activity is a Preferences activity. The app is not intended to have a runtime UI, rather using background IntentService and Notifications.
推荐答案
根据 Android文档,
针对Android 7.0(API级别24)及更高版本的应用不会收到 CONNECTIVITY_ACTION广播,如果它们声明了广播接收者 在他们的清单上.应用仍将获得CONNECTIVITY_ACTION 如果他们向其注册了BroadcastReceiver广播 Context.registerReceiver(),并且该上下文仍然有效.
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.
因此,您需要做的是启动Service
,创建广播接收器NetworkChangeReceiver
的新实例,并使用
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"))
这篇关于NetworkInfo无法正确响应运行牛轧糖的电话上的网络更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!