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>