我目前正在检测WIFI是否与以下代码连接:
BroadcastReceiver wifiStateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
int wifiStateExtra = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
WifiManager.WIFI_STATE_UNKNOWN);
Log.v("Log","Wifi Toggled");
}
};
IntentFilter intentFilter = new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION);
context.registerReceiver(wifiStateReceiver, intentFilter);
wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
无论何时打开或关闭wifi,此代码都会告诉我。我正在寻找这样的东西,只要我的网络状态发生变化,即当我有或没有互联网连接(WiFi或移动数据)时,它都会向我发送通知。我可以使用类似NetworkManger的功能来告诉我网络状态是否已更改吗?
答案 0 :(得分:0)
看看determining and monitoring the connectivity status的官方文档。
(venv) C:\Users\Tila>python -c "import tensorflow as tf; tf.enable_eager_exe
cution(); print(tf.reduce_sum(tf.random_normal([1000, 1000])))"
Traceback (most recent call last):
File "C:\Users\Tila\Anaconda3\lib\site-packages\tensorflow\python\pywrap_t
ensorflow.py", line 58, in <module>
from tensorflow.python.pywrap_tensorflow_internal import *
File "C:\Users\Tila\Anaconda3\lib\site-packages\tensorflow\python\pywrap_t
ensorflow_internal.py", line 28, in <module>
_pywrap_tensorflow_internal = swig_import_helper()
File "C:\Users\Tila\Anaconda3\lib\site-packages\tensorflow\python\pywrap_t
ensorflow_internal.py", line 24, in swig_import_helper
_mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, descript
ion)
File "C:\Users\Tila\venv\lib\imp.py", line 242, in load_module
return load_dynamic(name, filename, file)
File "C:\Users\Tila\venv\lib\imp.py", line 342, in load_dynamic
return _load(spec)
ImportError: DLL load failed with error code -1073741795
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Users\Tila\Anaconda3\lib\site-packages\tensorflow\__init__.py", l
ine 24, in <module>
from tensorflow.python import pywrap_tensorflow # pylint: disable=unused-im
port
File "C:\Users\Tila\Anaconda3\lib\site-packages\tensorflow\python\__init__
.py", line 49, in <module>
from tensorflow.python import pywrap_tensorflow
File "C:\Users\Tila\Anaconda3\lib\site-packages\tensorflow\python\pywrap_t
ensorflow.py", line 74, in <module>
raise ImportError(msg)
ImportError: Traceback (most recent call last):
File "C:\Users\Tila\Anaconda3\lib\site-packages\tensorflow\python\pywrap_t
ensorflow.py", line 58, in <module>
from tensorflow.python.pywrap_tensorflow_internal import *
File "C:\Users\Tila\Anaconda3\lib\site-packages\tensorflow\python\pywrap_t
ensorflow_internal.py", line 28, in <module>
_pywrap_tensorflow_internal = swig_import_helper()
File "C:\Users\Tila\Anaconda3\lib\site-packages\tensorflow\python\pywrap_t
ensorflow_internal.py", line 24, in swig_import_helper
_mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, descript
ion)
File "C:\Users\Tila\venv\lib\imp.py", line 242, in load_module
return load_dynamic(name, filename, file)
File "C:\Users\Tila\venv\lib\imp.py", line 342, in load_dynamic
return _load(spec)
ImportError: DLL load failed with error code -1073741795
Failed to load the native TensorFlow runtime.
See https://www.tensorflow.org/install/errors
for some common reasons and solutions. Include the entire stack trace
above this error message when asking for help.
(设备连接性可以通过移动数据,WiMAX,Wi-Fi和以太网连接来提供)
ConnectivityManager cm =
(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null &&
activeNetwork.isConnectedOrConnecting();
面向Android 7.0(API级别24)及更高版本的应用程序如果在清单中声明了广播接收器,则不会收到CONNECTIVITY_ACTION广播。如果应用向Context.registerReceiver()注册了自己的BroadcastReceiver,并且该上下文仍然有效,则它们仍将接收CONNECTIVITY_ACTION广播。
答案 1 :(得分:0)
Android Internet连接检查API 29 +
private static final String DEBUG_TAG = "NetworkStatusExample";
ConnectivityManager connMgr =
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
boolean isWifiConn = false;
boolean isMobileConn = false;
for (Network network : connMgr.getAllNetworks()) {
NetworkInfo networkInfo = connMgr.getNetworkInfo(network);
if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
isWifiConn |= networkInfo.isConnected();
}
if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
isMobileConn |= networkInfo.isConnected();
}
}
Log.d(DEBUG_TAG, "Wifi connected: " + isWifiConn);
Log.d(DEBUG_TAG, "Mobile connected: " + isMobileConn);