网络检查android系统中的功能? [英] Network checking functionality in android?

查看:96
本文介绍了网络检查android系统中的功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的code,检查网络可用性,同时击中远程服务或访问任何网页,为我会打的Web服务之前,每次调用此下面的方法,但我需要的任何监视器,它定期监视在后台的Web服务,并抛出一个警报网络不可用的,如果网络是恢复它的净抛出一个警报网络恢复时,我不`吨知道如何去实现它。

 公共布尔isOnline(上下文的背景下)
{
    布尔状态= FALSE;
    ConnectivityManager厘米=(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
    的NetworkInfo wifiNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    如果(wifiNetwork!= NULL){
       状态= wifiNetwork.isConnectedOrConnecting();
    }    的NetworkInfo mobileNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    如果(mobileNetwork!= NULL){
       状态= mobileNetwork.isConnectedOrConnecting();
    }    的NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    如果(activeNetwork!= NULL){
       状态= activeNetwork.isConnectedOrConnecting();
    }    返回状态;
}


解决方案

对于您需要实现广播接收器,它会检查在后台的网络连接,并给予警告,当网络已连接或没有。

请尝试以下code。

1)让一个类这将延长brodcast接收器。

 公共类CheckInternetConnectionChangeReceiver扩展广播接收器
{
公共静态布尔connectionStatus = FALSE;@覆盖
公共无效的onReceive(上下文的背景下,意图意图)
{
    connectionStatus = CheckInternetConnection(上下文);
    如果(connectionStatus)
        Toast.makeText(背景下,Internet连接可用,Toast.LENGTH_LONG).show();
    其他
        Toast.makeText(背景下,Internet连接不可用,Toast.LENGTH_LONG).show();
}公共布尔CheckInternetConnection(上下文的背景下)
{
    ConnectivityManager CONNEC =(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
    android.net.NetworkInfo无线= connec.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    android.net.NetworkInfo移动= connec.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);    如果(wifi.isConnected())
    {
        返回true;
    }
    否则如果(!mobile.isConnected())
    {
        返回false;
    }
    否则如果(mobile.isConnected())
    {
        返回true;
    }    返回false;
}

}

//这里connectionStatus是根据网络,存储true或false值一个布尔变量。如果它是可将存储的真正价值其他明智的将存储值为false。

现在下面粘贴code在你的Andr​​oid清单文件。

 <接收机器人:名字=。CheckInternetConnectionChangeReceiver>
    &所述;意图滤光器>
        <作用机器人:名字=android.net.conn.CONNECTIVITY_CHANGE/>
    &所述; /意图滤光器>
 < /接收器>

现在当过连接改变它会调用brodcast接收并存储在connnectionStatus变量相应的值。

I use the following code to check the network availability while hitting remote service or accessing any web pages, for that i will call this below method every time before hitting web service, but i need any monitor which periodically monitor the web service at background and throw an alert network is not available, and if network is resume it net to throw an alert network is resumed , i don`t know how to achieve it.

public boolean isOnline(Context context) 
{
    boolean state=false;
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo wifiNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (wifiNetwork != null) {
       state=wifiNetwork.isConnectedOrConnecting();
    }

    NetworkInfo mobileNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    if (mobileNetwork != null) {
       state=mobileNetwork.isConnectedOrConnecting();
    }

    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    if (activeNetwork != null) {
       state=activeNetwork.isConnectedOrConnecting();
    }

    return state;
}

解决方案

For that you need to implement the broadcast receiver, it will check the network connection in background and give alert when network has connectivity or not.

Please try below code.

1) Make one class which will extend brodcast receiver.

public class CheckInternetConnectionChangeReceiver extends BroadcastReceiver 
{
public static boolean connectionStatus = false;

@Override
public void onReceive(Context context, Intent intent) 
{
    connectionStatus = CheckInternetConnection(context);
    if(connectionStatus)
        Toast.makeText(context, "Internet Connection Available", Toast.LENGTH_LONG).show();
    else
        Toast.makeText(context, "Internet Connection Not Available", Toast.LENGTH_LONG).show();
}

public boolean CheckInternetConnection(Context context)
{
    ConnectivityManager connec = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    android.net.NetworkInfo wifi = connec.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    android.net.NetworkInfo mobile = connec.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

    if (wifi.isConnected()) 
    {
        return true;
    } 
    else if (!mobile.isConnected()) 
    {
        return false;
    } 
    else if (mobile.isConnected()) 
    {
        return true;
    }

    return false;
}

}

// Here connectionStatus is a one boolean variable which stored the true or false value according to network . If it is available it will stored true value other wise it will stored false value.

Now Paste below code in your android manifest file.

 <receiver android:name=".CheckInternetConnectionChangeReceiver">
    <intent-filter>
        <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
    </intent-filter>
 </receiver>

Now when ever connection change it will call the brodcast receiver and store the appropriate value in the connnectionStatus variable.

这篇关于网络检查android系统中的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆