简体   繁体   English

检查android中可用的网络

[英]Check network available in android

I have to develop one android application.我必须开发一个 android 应用程序。

Here i have to develop one twitter integration android application.在这里,我必须开发一个 twitter 集成 android 应用程序。

i have using below code:我使用以下代码:

public void onClickTwitt() {
    if (isNetworkAvailable()) {
        Twitt twitt = new Twitt(getActivity(), consumer_key, secret_key);
        twitt.shareToTwitter(_Title);
    } else {
        showToast("No Network Connection Available !!!");
    }
}
public boolean isNetworkAvailable() {
     ConnectivityManager connectivity =(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

    if (connectivity == null) {
        return false;
    } else {
        NetworkInfo[] info = connectivity.getAllNetworkInfo();
        if (info != null) {
            for (int i = 0; i < info.length; i++) {
                if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                    return true;
                }
            }
        }
    }
    return false;
}

Here am getting below error :这里出现以下错误:

The method getSystemService(String) is undefined for the type 
 SubCate

Please help me...How can i resolve these error ????请帮帮我...我该如何解决这些错误????

public void onClickTwitt() {
    if (isNetworkAvailable(this)) {
        Twitt twitt = new Twitt(getActivity(), consumer_key, secret_key);
        twitt.shareToTwitter(_Title);
    } else {
        showToast("No Network Connection Available !!!");
    }
}
public boolean isNetworkAvailable(Context context) {
    ConnectivityManager connectivity =(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    if (connectivity == null) {
        return false;
    } else {
        NetworkInfo[] info = connectivity.getAllNetworkInfo();
        for (NetworkInfo networkInfo : info) {
            if (networkInfo.getState() == NetworkInfo.State.CONNECTED) {
                return true;
            }
        }
    }
    return false;
}
 public static  boolean isNetworkAvailable(Context context) {
    if(context == null) { return false; }
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    // if no network is available networkInfo will be null, otherwise check if we are connected
    try {
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        if (activeNetworkInfo != null && activeNetworkInfo.isConnected()) {
            return true;
        }
    } catch (Exception e) {
        AppLog.e(TAG, "isNetworkAvailable()" , e.getMessage());
    }
    return false;
}

and then do this然后这样做

if(isNetworkAvailable(TempOrderActivity.this)) {
                //do something

            } else {
                //do something
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM