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

时间:2016-09-22 12:45:28

标签: android crash android-internet

在我的主要活动中我有

public async void DoSomethingAsync()
{
//implementation
}

然后在下面我有方法

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

在清单中,当我运行应用程序时,它总是停止,杀死应用程序出错了

2 个答案:

答案 0 :(得分:1)

创建一个类并正确添加方法:

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

获取状态,在您使用的活动中:

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

答案 1 :(得分:0)

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