连接状态 - 如何从另一个类调用方法

时间:2014-12-16 21:37:11

标签: java android connection

我编写的应用程序必须检查状态,如果互联网连接,我想在另一个类中调用此方法,但我不知道该怎么做。

以下是负责检查互联网连接的代码:

public class NetworkUtil {

public static int TYPE_WIFI = 1;
public static int TYPE_MOBILE = 2;
public static int TYPE_NOT_CONNECTED = 0;


public static int getConnectivityStatus(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    if (null != activeNetwork) {
        if(activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)
            return TYPE_WIFI;

        if(activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)
            return TYPE_MOBILE;
    } 
    return TYPE_NOT_CONNECTED;
}

public static String getConnectivityStatusString(Context context) {
    int conn = NetworkUtil.getConnectivityStatus(context);
    String status = null;
    if (conn == NetworkUtil.TYPE_WIFI) {
        status = "Wifi enabled";
    } else if (conn == NetworkUtil.TYPE_MOBILE) {
        status = "Mobile data enabled";
    } else if (conn == NetworkUtil.TYPE_NOT_CONNECTED) {
        status = "Not connected to Internet";
    }
    return status;
}
}

和广播班:

public class NetworkChangeReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(final Context context, final Intent intent) {

        String status = NetworkUtil.getConnectivityStatusString(context);

        Toast.makeText(context, status, Toast.LENGTH_LONG).show();
    }
}

我想检查另一个类,或者目前没有连接(getConnectivityStatus返回TYPE_NOT_CONNECTED)并执行这样的代码:

if (connection is missing - i don't know what should i put in this place) {
    // other code
}
else {
}

你能帮助我吗?

3 个答案:

答案 0 :(得分:0)

if (NetworkUtil.getConnectivityStatus(getApplicationContext()) == NetworkUtil.TYPE_NOT_CONNECTED)  {
  //NOT CONNECTED!
}else {
    //CONNECTED!
}

您还可以更改方法以检索布尔值:

public static boolean getConnectivityStatus(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    if (null != activeNetwork) {
        if(activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)
            return true;

        if(activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)
            return true;
    } 
    return false;
}

你的代码就像:

if (NetworkUtil.getConnectivityStatus(getApplicationContext()))  {
    //CONNECTED!
}else {
    //NOT CONNECTED!
}

注意: 如果您在片段内,则可以将您的方法称为:

NetworkUtil.getConnectivityStatus(getActivity().getApplicationContext())

答案 1 :(得分:0)

不确定你的问题是什么......

if (NetworkUtil.getConnectivityStatus(getApplicationContext()) == NetworkUtil.TYPE_NOT_CONNECTED)  {

}else {

}

希望有所帮助

答案 2 :(得分:0)

在这里我做了什么:

首先,我在我的活动中注册我的接收器:

   registerReceiver(
              new NetworkChangeReceiver(),
              new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)
    );

这是我的接收者:

public class NetworkChangeReceiver extends BroadcastReceiver {
    private static final String TAG = "NetworkStateReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.v(TAG, "[status]: " + NetworkUtils.getConnectivityStatus(context));
    }
}

最后我的NetworkUtils就像这样简单:

public enum NetworkStatus{
    NETWORK_STATUS_NOT_CONNECTED,
    NETWORK_STATUS_WIFI,
    NETWORK_STATUS_MOBILE
}

    public static NetworkStatus getConnectivityStatus(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        if (null != activeNetwork) {
            if(activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)
                return NetworkStatus.NETWORK_STATUS_WIFI;

            if(activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)
                return NetworkStatus.NETWORK_STATUS_MOBILE;
        }
        return NetworkStatus.NETWORK_STATUS_NOT_CONNECTED;
    }
}