Android 自动检查互联网连接

Android check Internet connection automatically

Hello world 如何始终自动检测我是否已连接或断开与 Internet 的连接。 示例:当我连接并关闭 3G 或 wifi 时,toast 说我已断开连接,在我激活 3G 或 wifi 3 秒后,toast 告诉我我已连接。

我使用此方法,但它会在应用程序启动时告诉我连接状态,但在应用程序导航期间不会。

 ConnectivityManager manager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);

//For 3G check
    boolean is3g = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
            .isConnectedOrConnecting();
//For WiFi Check
    boolean isWifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
            .isConnectedOrConnecting();


    if (!is3g && !isWifi)
    {
        Toast.makeText(getApplicationContext(),"Network Connection is OFF", Toast.LENGTH_LONG).show();
    }
    else
    {
        Toast.makeText(getApplicationContext(),"Network Connection is ON", Toast.LENGTH_LONG).show();

    }

请帮帮我

您可以为此使用 utility class 和 BroadcastReceiver。下面 link 显示了一步一步的过程

http://viralpatel.net/blogs/android-internet-connection-status-network-change/

您可以使用 Timer 的时间表来 运行 一个线程。请注意最后一个 3000,它将每 3 秒变为 运行。

Timer timer = new Timer();
timer.schedule(new TimerTask()
{
    @Override
    public void run()
    {
        ConnectivityManager manager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);

        //For 3G check
        boolean is3g = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
            .isConnectedOrConnecting();
        //For WiFi Check
        boolean isWifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
            .isConnectedOrConnecting();


        if (!is3g && !isWifi)
        {
            Toast.makeText(getApplicationContext(),"Network Connection is OFF", Toast.LENGTH_LONG).show();
        }
        else
        {
            Toast.makeText(getApplicationContext(),"Network Connection is ON", Toast.LENGTH_LONG).show();

        }
    }
}, 0, 3000);

答案是BroadcastReceiver! 每次您的 network connectivity 从 WIFI/3g/ON/OFF..

更改时您都会收到通知

看这里:http://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html

这里:http://www.grokkingandroid.com/android-getting-notified-of-connectivity-changes/

您可以在任何地方使用此方法:)

 public static boolean isDeviceOnline(Context context) {

    ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
    boolean isOnline = (networkInfo != null && networkInfo.isConnected());
    if(!isOnline)
        Toast.makeText(context, " No internet Connection ", Toast.LENGTH_SHORT).show();

    return isOnline;
}

在您的 activity 中使用此代码:

activity=this;
if(isDeviceOnline(activity))
 {
 //you are online
 }
 else
 {
 //you are offline
 } 
private boolean checknet(){

        ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo activeNetworkinfo = connectivityManager.getActiveNetworkInfo();

        return activeNetworkinfo != null && activeNetworkinfo.isConnected();
    }

//write below methodcall in every button click event wherever u want to check internet connection


 if(!checknet())
        {

            Toast.makeText(getApplicationContext(), "No Internet Connecction.", Toast.LENGTH_SHORT).show();

        }