如何检查android手机是否未连接到互联网?

如何检查android手机是否未连接到互联网?,android,Android,我正在用android开发一个从数据库获取数据的应用程序。我开发了代码,以确定WIFI或移动数据是打开还是关闭,如下所示: protected boolean isNetworkAvailable() { boolean connection=true; ConnectivityManager connectivityManager=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); N

我正在用android开发一个从数据库获取数据的应用程序。我开发了代码,以确定WIFI或移动数据是打开还是关闭,如下所示:

protected boolean isNetworkAvailable()
{
    boolean connection=true;
    ConnectivityManager connectivityManager=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo[] activeNetworkInfo=connectivityManager.getAllNetworkInfo();
    for(NetworkInfo ni:activeNetworkInfo)
    {
        if(ni.getTypeName().equalsIgnoreCase("WIFI"))
            if(ni.isConnected())    
                haveConnectedWifi=true;
        if(ni.getTypeName().equalsIgnoreCase("MOBILE"))
            if(ni.isConnected())
                haveConnectedMobile=true;

    }
    if(haveConnectedWifi==false && haveConnectedMobile==false)
    {

    connection=false;
    }
        return connection;

    }
现在假设我的WIFI或移动数据网络已打开,但我根本没有连接到internet(WIFI未连接到任何接入点)。我如何在android中显示这一点

谢谢,

试试这个

ConnectivityManager connec = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
        try {
            android.net.NetworkInfo wifi = connec.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

        android.net.NetworkInfo mobile = connec.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

        if (wifi.isConnected()||mobile.isConnected())  
            return true;
        else if (wifi.isConnected() && mobile.isConnected())  
            return true;
        else  
            return false;

    } catch (NullPointerException e) {
        Log.d("ConStatus", "No Active Connection");
        return false;
    }
试试这个

ConnectivityManager connec = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
        try {
            android.net.NetworkInfo wifi = connec.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

        android.net.NetworkInfo mobile = connec.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

        if (wifi.isConnected()||mobile.isConnected())  
            return true;
        else if (wifi.isConnected() && mobile.isConnected())  
            return true;
        else  
            return false;

    } catch (NullPointerException e) {
        Log.d("ConStatus", "No Active Connection");
        return false;
    }

这段代码可能会对您有所帮助

ConnectivityManager cn= (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);


    NetworkInfo networkInfo = cn.getNetworkInfo(ConnectivityManager.TYPE_WIFI); 
    testwifi=networkInfo.isConnected();
    if(!testwifi)
        tv1.setText("wifi is off");
    else
        tv1.setText("wifi is on");

这段代码可能会对您有所帮助

ConnectivityManager cn= (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);


    NetworkInfo networkInfo = cn.getNetworkInfo(ConnectivityManager.TYPE_WIFI); 
    testwifi=networkInfo.isConnected();
    if(!testwifi)
        tv1.setText("wifi is off");
    else
        tv1.setText("wifi is on");
试试这个:

public static void isNetworkAvailable(Context context){
    HttpGet httpGet = new HttpGet("http://www.google.com");
    HttpParams httpParameters = new BasicHttpParams();
    // Set the timeout in milliseconds until a connection is established.
    // The default value is zero, that means the timeout is not used.
    int timeoutConnection = 3000;
    HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
    // Set the default socket timeout (SO_TIMEOUT)
    // in milliseconds which is the timeout for waiting for data.
    int timeoutSocket = 5000;
    HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

    DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
    try{
        Log.d(TAG, "Checking network connection...");
        httpClient.execute(httpGet);
        Log.d(TAG, "Connection OK");
        return;
    }
    catch(ClientProtocolException e){
        e.printStackTrace();
    }
    catch(IOException e){
        e.printStackTrace();
    }

    Log.d(TAG, "Connection unavailable");
}
来源:

试试这个:

public static void isNetworkAvailable(Context context){
    HttpGet httpGet = new HttpGet("http://www.google.com");
    HttpParams httpParameters = new BasicHttpParams();
    // Set the timeout in milliseconds until a connection is established.
    // The default value is zero, that means the timeout is not used.
    int timeoutConnection = 3000;
    HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
    // Set the default socket timeout (SO_TIMEOUT)
    // in milliseconds which is the timeout for waiting for data.
    int timeoutSocket = 5000;
    HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

    DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
    try{
        Log.d(TAG, "Checking network connection...");
        httpClient.execute(httpGet);
        Log.d(TAG, "Connection OK");
        return;
    }
    catch(ClientProtocolException e){
        e.printStackTrace();
    }
    catch(IOException e){
        e.printStackTrace();
    }

    Log.d(TAG, "Connection unavailable");
}

来源:

您可以简单地使用:

public boolean isNetworkAvailable() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = cm.getActiveNetworkInfo();
    // if no network is available networkInfo will be null
    // otherwise check if we are connected
    if (networkInfo != null && networkInfo.isConnected()) {
        return true;
    }
    return false;
}

您可以简单地使用:

public boolean isNetworkAvailable() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = cm.getActiveNetworkInfo();
    // if no network is available networkInfo will be null
    // otherwise check if we are connected
    if (networkInfo != null && networkInfo.isConnected()) {
        return true;
    }
    return false;
}

调用此类的isConnectingToInternet函数检查要检查internet连接的internet连接

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class ConnectionDetector {

private Context _context;

public ConnectionDetector(Context context) {
    this._context = context;
}

/**
 * Checking for all possible internet providers
 * **/
public boolean isConnectingToInternet() {
    ConnectivityManager connectivity = (ConnectivityManager) _context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity != null) {
        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;
}
}
导入android.content.Context;
导入android.net.ConnectivityManager;
导入android.net.NetworkInfo;
公共类连接检测器{
私人语境(private Context)(私人语境);;
公共连接检测器(上下文){
这._context=context;
}
/**
*检查所有可能的互联网提供商
* **/
公共布尔值未连接到Internet(){
ConnectionManager连接=(ConnectionManager)\u上下文
.getSystemService(Context.CONNECTIVITY\u服务);
if(连接性!=null){
NetworkInfo[]info=connectivity.getAllNetworkInfo();
如果(信息!=null)
对于(int i=0;i
调用此类的isConnectingToInternet函数,检查要检查internet连接的internet连接

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class ConnectionDetector {

private Context _context;

public ConnectionDetector(Context context) {
    this._context = context;
}

/**
 * Checking for all possible internet providers
 * **/
public boolean isConnectingToInternet() {
    ConnectivityManager connectivity = (ConnectivityManager) _context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity != null) {
        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;
}
}
导入android.content.Context;
导入android.net.ConnectivityManager;
导入android.net.NetworkInfo;
公共类连接检测器{
私人语境(private Context)(私人语境);;
公共连接检测器(上下文){
这._context=context;
}
/**
*检查所有可能的互联网提供商
* **/
公共布尔值未连接到Internet(){
ConnectionManager连接=(ConnectionManager)\u上下文
.getSystemService(Context.CONNECTIVITY\u服务);
if(连接性!=null){
NetworkInfo[]info=connectivity.getAllNetworkInfo();
如果(信息!=null)
对于(int i=0;i
这门课相当完整:

public class ConnexionDetector extends BroadcastReceiver {
    /**
     * ATTRIBUTES
     */
    private Context         _context;
    private boolean         _noConnectivity;
    private String          _reason;
    private boolean         _isFailover;
    private static boolean  _isConnected        = false;
    private static boolean  _isConnectivityGood = true;

    public ConnexionDetector(Context context) {

        this._context = context;
    }

public void registerReceivers() {

    _context.registerReceiver(this, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
}

public boolean isConnectingToInternet() {

    ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity != null) {
        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;
}

public static NetworkInfo getNetworkInfo(Context context) {

    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    return cm.getActiveNetworkInfo();
}

public static boolean isConnected(Context context) {

    NetworkInfo info = getNetworkInfo(context);
    return (info != null && info.isConnected());
}

public static boolean isConnectedWifi(Context context) {

    NetworkInfo info = getNetworkInfo(context);
    return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_WIFI);
}

public static boolean isConnectedMobile(Context context) {

    NetworkInfo info = getNetworkInfo(context);
    return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_MOBILE);
}

public static boolean isConnectedFast(Context context) {

    NetworkInfo info = getNetworkInfo(context);
    return (info != null && info.isConnected() && isConnectionFast(info.getType(), info.getSubtype()));
}

private static boolean isConnectionFast(int type, int subType) {

    if (type == ConnectivityManager.TYPE_WIFI) {
        return true;
    }
    else if (type == ConnectivityManager.TYPE_MOBILE) {
        switch (subType) {
            case TelephonyManager.NETWORK_TYPE_1xRTT:
                return false; // ~ 50-100 kbps
            case TelephonyManager.NETWORK_TYPE_CDMA:
                return false; // ~ 14-64 kbps
            case TelephonyManager.NETWORK_TYPE_EDGE:
                return false; // ~ 50-100 kbps
            case TelephonyManager.NETWORK_TYPE_EVDO_0:
                return true; // ~ 400-1000 kbps
            case TelephonyManager.NETWORK_TYPE_EVDO_A:
                return true; // ~ 600-1400 kbps
            case TelephonyManager.NETWORK_TYPE_GPRS:
                return false; // ~ 100 kbps
            case TelephonyManager.NETWORK_TYPE_HSDPA:
                return true; // ~ 2-14 Mbps
            case TelephonyManager.NETWORK_TYPE_HSPA:
                return true; // ~ 700-1700 kbps
            case TelephonyManager.NETWORK_TYPE_HSUPA:
                return true; // ~ 1-23 Mbps
            case TelephonyManager.NETWORK_TYPE_UMTS:
                return true; // ~ 400-7000 kbps
            case TelephonyManager.NETWORK_TYPE_UNKNOWN:
            default:
                return false;
        }
    }
    else {
        return false;
    }
}

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

    _noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
    _reason = intent.getStringExtra(ConnectivityManager.EXTRA_REASON);
    _isFailover = intent.getBooleanExtra(ConnectivityManager.EXTRA_IS_FAILOVER, false);
    //
    if (_noConnectivity) {
        _isConnected = false;
    }
    else {
        if (isConnectedFast(_context)) {
            _isConnectivityGood = true;
        }
        else {
            _isConnectivityGood = false;
        }
        _isConnected = true;
    }
}
}
公共类ConnexionDetector扩展了BroadcastReceiver{
/**
*属性
*/
私人语境(private Context)(私人语境);;
私有布尔无连通性;
私有字符串\u原因;
私有布尔函数;
私有静态布尔值_isConnected=false;
私有静态布尔值_isConnectivityGood=true;
公共连接检测器(上下文){
这._context=context;
}
公共无效注册表接收者(){
_registerReceiver(这是新的IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
}
公共布尔值未连接到Internet(){
ConnectivityManager connectivity=(ConnectivityManager)_context.getSystemService(context.connectivity_SERVICE);
if(连接性!=null){
NetworkInfo[]info=connectivity.getAllNetworkInfo();
如果(信息!=null)
对于(int i=0;i