如何在每10-15秒后连续检查Internet连接?

时间:2015-12-17 06:43:49

标签: java android eclipse android-intent

我正在开发一个具有以下要求的应用程序1)它应该在后台连续检查互联网连接2)当找到连接时,它应该自动发送电子邮件3)如果没有找到连接,它应该等待和每10或15秒后继续检查连接性。我已经搜索过,但无法在任何地方找到连接检查问题。我是android开发的新手。请帮我解决这个问题。谢谢。

4 个答案:

答案 0 :(得分:0)

为您的应用设置闹钟管理器,每隔15秒检查一次互联网连接。警报管理员广播你可以做你的事情的意图。

答案 1 :(得分:0)

您可以使用广播接收器来检查互联网连接,并且只要网络发生任何变化,就会被调用。

public class ConnectivityReceiver extends BroadcastReceiver {
private static boolean firstConnect = true;

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


    final ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    final NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();

    if (activeNetInfo != null) {
        boolean isConnected = activeNetInfo.isConnected();
        boolean isWiFi = activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI;
        boolean isMobile = activeNetInfo.getType() == ConnectivityManager.TYPE_MOBILE;

        if (firstConnect) {

            firstConnect = false;

            //checks for the type of network, device is connected to.
            if (isConnected && isWiFi) {
                if(!Utilities.isReleaseMode(context)){
                    Toast.makeText(context, "Connected to Internet", Toast.LENGTH_SHORT).show();
                }
                Log.i("Wifi Network connected", "wifi Network connected");

            } else if (isConnected && isMobile) {
                if(!Utilities.isReleaseMode(context)) {
                    Toast.makeText(context, "Connected to Internet", Toast.LENGTH_SHORT).show();
                }
                Log.i("Mobile Network ", "mobile Network connected");

            }
        }

    } else {
        firstConnect = true;
        //  Log.i("Network Disconnected", "Network Disconnected");
    }
}

答案 2 :(得分:0)

  Check internet connectivity:
   public static boolean isNetworkStatusAvialable (Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager)   context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivityManager != null) 
    {
        NetworkInfo netInfos = connectivityManager.getActiveNetworkInfo();
        if(netInfos != null)
            if(netInfos.isConnected()) 
                if (netInfos.isAvailable())
                    return true;
    }
    return false;

}

 if (isNetworkStatusAvialable(getApplicationContext()))
    {
   // your function
 }else{}

答案 3 :(得分:0)

使用alaram每隔10或15秒调用此函数。

public static boolean isOnline(Context context) {// requires network state
                                                    // access permisstion
    ConnectivityManager cm = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    return netInfo != null && netInfo.isConnectedOrConnecting();
}

 final Handler handler = new Handler();
    Timer timer = new Timer();
    TimerTask doAsynchronousTask = new TimerTask() {
        @Override
        public void run() {
            handler.post(new Runnable() {
                @SuppressWarnings("unchecked")
                public void run() {
                    try {
                        isOnline(getActivity());
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                    }
                }
            });
        }
    };
    timer.schedule(doAsynchronousTask, 0, 10000);