Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check network available in android

I have to develop one android application.

Here i have to develop one twitter integration android application.

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 ????

like image 946
user2218667 Avatar asked Dec 09 '22 15:12

user2218667


1 Answers

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;
}
like image 157
Parag Chauhan Avatar answered Dec 11 '22 11:12

Parag Chauhan