即使没有启用Internet,isProviderEnabled(LocationManager.NETWORK_PROVIDER)也会返回true

时间:2014-07-19 08:35:40

标签: java android eclipse google-maps

我正在检查Google地图应用中的有效互联网连接,我希望在互联网处于活动状态时执行特定操作,而在互联网处于非活动状态时则执行另一项操作。

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。请帮助!!

1 个答案:

答案 0 :(得分:1)

isproviderenabled返回当前启用的提供程序的字符串。它不提供活动网络连接的状态。检查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
相关问题