如何在 API 级别 21 以下的设备中检查 NO INTERNET
Posted
技术标签:
【中文标题】如何在 API 级别 21 以下的设备中检查 NO INTERNET【英文标题】:how to check for NO INTERNET in devices below API level 21 【发布时间】:2020-10-18 15:57:54 【问题描述】:这是我的 MainActivity,我已经在 onstart 中注册了我的网络调用。在此代码中,无论设备是否打开互联网 wifi,都不会调用 onUnavailable()。我想在点击一个按钮后检查互联网,如果互联网连接,它将继续下一个活动,否则没有
public class MainActivity extends AppCompatActivity
private TextView textView;
ConnectivityManager.NetworkCallback networkCallback;
ConnectivityManager manager;
public static final String TAG = "bantai";
private NetworkRequest networkRequest;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.internetTextview);
networkRequest = new NetworkRequest.Builder().build();
manager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
networkCallback = new ConnectivityManager.NetworkCallback()
@Override
public void onAvailable(@NonNull Network network)
super.onAvailable(network);
Log.d(TAG, "onAvailable: ");
runOnUiThread(new Runnable()
@Override
public void run()
Log.d(TAG, "onAvailable: "+ Thread.currentThread().getName());
textView.setText("onAvailable");
);
@Override
public void onLosing(@NonNull Network network, int maxMsToLive)
super.onLosing(network, maxMsToLive);
Log.d(TAG, "onLosing: ");
runOnUiThread(new Runnable()
@Override
public void run()
Log.d(TAG, "onAvailable: "+ Thread.currentThread().getName());
textView.setText("loosing");
);
@Override
public void onLost(@NonNull Network network)
super.onLost(network);
Log.d(TAG, "onLost: ");
runOnUiThread(new Runnable()
@Override
public void run()
Log.d(TAG, "onAvailable: "+ Thread.currentThread().getName());
textView.setText("lost");
);
@Override
public void onUnavailable()
super.onUnavailable();
Log.d(TAG, "onUnavailable: ");
runOnUiThread(new Runnable()
@Override
public void run()
Log.d(TAG, "onAvailable: "+ Thread.currentThread().getName());
textView.setText("unavailable");
);
@Override
public void onCapabilitiesChanged(@NonNull Network network, @NonNull NetworkCapabilities networkCapabilities)
super.onCapabilitiesChanged(network, networkCapabilities);
Log.d(TAG, "onCapabilitiesChanged: ");
runOnUiThread(new Runnable()
@Override
public void run()
Log.d(TAG, "onAvailable: "+ Thread.currentThread().getName());
textView.setText("capability");
);
@Override
public void onLinkPropertiesChanged(@NonNull Network network, @NonNull LinkProperties linkProperties)
super.onLinkPropertiesChanged(network, linkProperties);
Log.d(TAG, "onLinkPropertiesChanged: ");
runOnUiThread(new Runnable()
@Override
public void run()
Log.d(TAG, "onAvailable: "+ Thread.currentThread().getName());
textView.setText("link");
);
@Override
public void onBlockedStatusChanged(@NonNull Network network, boolean blocked)
super.onBlockedStatusChanged(network, blocked);
Log.d(TAG, "onBlockedStatusChanged: ");
runOnUiThread(new Runnable()
@Override
public void run()
Log.d(TAG, "onAvailable: "+ Thread.currentThread().getName());
textView.setText("blocked");
);
;
@Override
protected void onStart()
super.onStart();
manager.requestNetwork(networkRequest,networkCallback);
@Override
protected void onStop()
super.onStop();
manager.unregisterNetworkCallback(networkCallback);
还有ConnectivityManager.requestNetwork(networkRequest, networkCallback, 3000)
这个命令需要Api level 21 +,超时3秒。
【问题讨论】:
github.com/jumaallan/androidNetworkManager - 参考这个开源。 这能回答你的问题吗? Android check internet connection 是的,当时工作正常,但现在 networkinfo、activenetworkinfo 和 getactivenetwork 已弃用 【参考方案1】:为了方便查看互联网,请使用此功能,它在 api 16 及更高版本上响应:
在 AndroidManifest.xml 中添加:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
现在检查网络状态:
getActiveNetworkInfo 在 API 29 上已弃用。
getAllNetworkInfo 在 API 23 上已弃用。
fun isConnected(): Boolean
val hasInternet: Boolean
val connectivityManager =
ctx.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
val networkCapabilities = connectivityManager.activeNetwork ?: return false
val actNw =
connectivityManager.getNetworkCapabilities(networkCapabilities) ?: return false
hasInternet = when
actNw.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> true
actNw.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> true
actNw.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) -> true
else -> false
else
hasInternet = try
if (connectivityManager.activeNetworkInfo == null)
false
else
connectivityManager.activeNetworkInfo?.isConnected!!
catch (e: Exception)
false
return hasInternet
【讨论】:
对不起,妈妈,我不知道 Kotlin,这就是为什么如果有人说它有效,我无法将其标记为正确,我会标记它以上是关于如何在 API 级别 21 以下的设备中检查 NO INTERNET的主要内容,如果未能解决你的问题,请参考以下文章