gpt4 book ai didi

android - 检查永久在线状态

转载 作者:行者123 更新时间:2023-11-30 04:07:06 25 4
gpt4 key购买 nike

我用这段代码检查手机是否有互联网连接:

online = SystemCheckInternetConn.getInstance(this).isOnline(this);

如何持续检测手机是否有连接?

最佳答案

你需要注册一个broadcastreceiver来监听连接的变化。尝试这样的事情:

registerReceiver(MyNetworkStateReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));

private BroadcastReceiver NetworkStateReceiver = new BroadcastReceiver(){

@Override
public void onReceive(Context context, Intent intent) {
boolean haveConnection = haveNetworkConnection(context);
}

private boolean haveNetworkConnection(Context context) {
boolean haveConnectedWifi = false;
boolean haveConnectedMobile = false;

ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] netInfo = cm.getAllNetworkInfo();
for (NetworkInfo ni : netInfo) {
if (ni.getTypeName().equalsIgnoreCase("WIFI"))
if (ni.isConnected())
haveConnectedWifi = true;
if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
if (ni.isConnected())
haveConnectedMobile = true;
}
return haveConnectedWifi || haveConnectedMobile;
}
};

然后用这些新发现的知识做点什么:)

关于android - 检查永久在线状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11424459/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com