gpt4 book ai didi

java - 即使没有启用互联网,isProviderEnabled(LocationManager.NETWORK_PROVIDER) 也会返回 true

转载 作者:太空宇宙 更新时间:2023-11-04 14:42:04 26 4
gpt4 key购买 nike

我正在谷歌地图应用程序中检查 Activity 的互联网连接,并且我想在互联网处于 Activity 状态时执行特定操作,如果互联网处于非 Activity 状态则执行另一个操作。

if(manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
{
getcurrentlocation();
}
else
{
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setMessage("Internet is mandatory for this application,\n connect and restart the app")
.setCancelable(false)
.setNeutralButton("OK",new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();

}
});
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}

问题在于,即使没有互联网,manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)也会返回true。请帮忙!!

最佳答案

isproviderenabled 返回当前启用的提供程序的字符串。它不提供 Activity 网络连接的状态。检查 this

如果您想检查设备是否有互联网连接,请调用以下函数。检查它的值,如果它为空则警告用户否则做你的事情

public boolean isconnected() {
ConnectivityManager cm =(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
return false;
}

将 if 条件更改为如下

 if(isconnected())
getcurrentlocation();
else
//alert user

关于java - 即使没有启用互联网,isProviderEnabled(LocationManager.NETWORK_PROVIDER) 也会返回 true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24838417/

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