检查网络连接并清空edittext

时间:2016-01-18 03:29:43

标签: android

我正在登录农场,但我有一个问题,我不明白为什么 这是我的代码:

if (isNetworkOnline()) {
                if (!(edtuserid.getText().toString().equals("") && !(edtpass.getText().toString().equals("")))){
                    DoLogin doLogin = new DoLogin();
                    doLogin.execute("");
                    Log.e("Login", "logining");
                }else {
                    Log.e("Login","empty");
                }
            }else {
                Log.e("Login", "no network");
            }

它始终显示"登录"在logcat ...我试过setText到edtuserid =""和edtpass.getText => Logcat ="登录" ,转而使用wifi,然后显示"登录"

这是我的支票网络代码:

public boolean isNetworkOnline() {
    boolean status=false;
    try{
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getNetworkInfo(0);
        if (netInfo != null && netInfo.getState()==NetworkInfo.State.CONNECTED) {
            status= true;
        }else {
            netInfo = cm.getNetworkInfo(1);
            if(netInfo!=null && netInfo.getState()==NetworkInfo.State.CONNECTED)
                status= true;
        }
    }catch(Exception e){
        e.printStackTrace();
        return false;
    }
    return status;
}

1 个答案:

答案 0 :(得分:1)

检查连接

public boolean isInternetOn() {

    // get Connectivity Manager object to check connection
    ConnectivityManager connec =
            (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);


    // Check for network connections
    if ( connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTED ||
            connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTING ||
            connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTING ||
            connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTED ) {

                   return true;

    } else if (
            connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.DISCONNECTED ||
                    connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.DISCONNECTED  ) {


        return false;
    }
    return false;
}

在您的代码中使用

    if (isInternetOn()) {
                    if (edtuserid.getText().toString().length > 0 && edtpass.getText().toString().length > 0){
                        DoLogin doLogin = new DoLogin();
                        doLogin.execute("");
                        Log.e("Login", "logining");
                    }else {
                        Log.e("Login","empty");
                        edtuserid.setError("Invalid UserId!");
edtpass.setError("Invalid Password!");
                    }
                }else {
                    Log.e("Login", "no network");
                }