在 android 中检查用户是否连接到网络

Checking if a user is connected to the network in android

在我的主要 activity 我有

 if (isNetworkAvailable() == true) {
        Log.i("testing", "available");

    } else {
        Log.i("test", "unaivalibale");
    }

然后下面我有

的方法
  private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager
            = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

我也设置了

 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

在清单中,但当我 运行 应用程序时,它总是停止,杀死应用程序,这是怎么回事

/*********************************************************
 * NetworkStatus helps us to check the Internet connection 
 * state of our handheld device.**************************
 ********************************************************/
package com.mynetwork.network;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
import android.widget.Toast;


/**
 * @author Aashutosh
 *
 */
public class NetworkStatus {

    private static NetworkStatus instance = new NetworkStatus();
    static Context context;
    ConnectivityManager connectivityManager;
    NetworkInfo wifiInfo, mobileInfo;
    boolean connected = false;

    public static NetworkStatus getInstance(Context ctx) {
        context = ctx;
        return instance;
    }

    public boolean isOnline(Context con) {
        try {
            connectivityManager = (ConnectivityManager) con.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
            connected = networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected();
            return connected;
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(context,"Check Connectivity Exception: " + e.getMessage(), Toast.LENGTH_SHORT).show();
            Log.v("connectivity", e.toString());
        }
        return connected;
    }

    /**
     * Checking for all possible Internet providers
     **/
    public boolean isConnectedToInternet(){
        connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivityManager != null){
            NetworkInfo[] info = connectivityManager.getAllNetworkInfo();
            if (info != null){
                for (int i = 0; i < info.length; i++){
                    if (info[i].getState() == NetworkInfo.State.CONNECTED){
                        return true;
                    }
                }
            }
        }
        return false;
    }
}

制作class并正确添加方法:

public class ConnectivityStatus extends ContextWrapper{
public ConnectivityStatus(Context base) {
    super(base);
}

public static boolean isConnected(Context context){

    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo connection = manager.getActiveNetworkInfo();
    if (connection != null && connection.isConnectedOrConnecting()){
        return true;
    }
    return false;
 }
}

获取状态,在 Activity 中您使用 as:

if(!ConnectivityStatus.isConnected(your_class_name.this)){
            //not connected
}else {
            //connected
}