Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/215.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 检查互联网状态的正确且最准确的方法。后台服务还是接收器?_Android_Android Intent_Broadcastreceiver_Intentservice_Background Service - Fatal编程技术网

Android 检查互联网状态的正确且最准确的方法。后台服务还是接收器?

Android 检查互联网状态的正确且最准确的方法。后台服务还是接收器?,android,android-intent,broadcastreceiver,intentservice,background-service,Android,Android Intent,Broadcastreceiver,Intentservice,Background Service,这是我清单文件中的内容 public class InternetChecker extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { ConnectivityManager cm = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_

这是我清单文件中的内容

    public class InternetChecker extends BroadcastReceiver {
     @Override
        public void onReceive(Context context, Intent intent) {
    ConnectivityManager cm = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE));
            if (cm == null)
                return;
            if (cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected()) {
    //execute my async activity 

    }else{
    //store the values in temp file
    }
  }
}

问题是,只有当互联网从关闭状态打开时,它才会被检测到 当数据连接断开并返回时,它不会对此作出反应。 例如,WiFi连接不稳定,然后再次稳定,后台服务无法相应地工作

物理上,当internet/WiFi从关闭状态打开时,此后台服务

我希望我清楚

<receiver android:name="com.company.InternetChecker" android:exported="false">
            <intent-filter>
                <action android:name="android.net.wifi.STATE_CHANGE" />
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            </intent-filter>
        </receiver>