【问题标题】:In android how to check the internet connection at regular intervals [duplicate]在android中如何定期检查互联网连接[重复]
【发布时间】:2014-06-02 07:17:23
【问题描述】:

我需要的正是我的代码应该定期检查互联网状态(比如每 30 或 40 秒)。如何做到这一点..我应该使用恶魔线程或 android 中可用的任何其他组件。希望很清楚。

【问题讨论】:

  • 你可以使用定时器...设置定时器间隔为 30 秒并调用函数检查互联网连接..

标签: android android-layout android-intent android-fragments androidhttpclient


【解决方案1】:

试试这个来检查互联网连接的可用性

public boolean isNetworkAvailable(Context context) {
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;
}

使用这个每 40 秒检查一次

Timer  networkTimer = new Timer();
NetWorkTimerTask networkTimerTask = new NetWorkTimerTask();
networkTimer.schedule(networkTimerTask, 0,40*1000);
public class NetWorkTimerTask extends TimerTask {

    @Override
    public void run() {
        networkHandler.sendEmptyMessage(0);
    }
}; 
@SuppressLint("HandlerLeak")
Handler networkHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        isOnline = isNetworkAvailable(getApplicationContext());
    }
};

在 AndroidManifest.xml 中添加权限

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

【讨论】:

  • 我做了一些改变,试试看。
【解决方案2】:
public boolean isOnline() {
    ConnectivityManager cm =
        (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
        return true;
    }
    return false;
}

并添加这个

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

【讨论】:

  • 这段代码向我显示了互​​联网连接的状态,比如它是真还是假。我已经实现了这个。我需要的是每隔 10 或 15 秒定期检查一次互联网连接。它应该在后台运行,定期检查网络连接..如何定期检查后台..
【解决方案3】:
public boolean isOnline() {
        ConnectivityManager conMgr = (ConnectivityManager) getApplicationContext()
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = conMgr.getActiveNetworkInfo();

        if (netInfo == null || !netInfo.isConnected() || !netInfo.isAvailable()) {
            /*
             * Toast.makeText(getActivity(), "No Internet connection!",
             * Toast.LENGTH_LONG).show();
             */
            return false;
        }
        return true;
    }

然后这样称呼它

if (isOnline()) {
//code if net available
}
else
{
Toast.makeText(MainActivity.this,"No Internet connection available  ",
                            Toast.LENGTH_SHORT).show();
}

如果您想检查 Interval,则将 if 条件放入线程中

【讨论】:

  • 谢谢朋友知道了..
猜你喜欢
  • 2017-11-30
  • 2012-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多