4

我必须开发一个 android 应用程序。

在这里,我必须开发一个 twitter 集成 android 应用程序。

我使用以下代码:

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;
}

这里出现以下错误:

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

请帮助我...我该如何解决这些错误????

4

3 回答 3

7
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;
}
于 2013-04-30T08:02:36.377 回答
7
 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;
}

然后这样做

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

            } else {
                //do something
            }
于 2013-04-30T08:31:32.480 回答
0

我使用此代码并且工作得很好,从我的应用程序中复制。希望这能解决你的问题

    private boolean isNetworkAvailable() {
        ConnectivityManager connectivityManager
                = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null && activeNetworkInfo.isConnected();
    }
于 2021-09-25T10:27:25.717 回答