我想编写一个轮询服务器的 android 应用程序。我假设启动网络传输(当当前没有网络流量时)需要额外的电池。因此,当已经有网络流量时进行轮询可能会更节能。
我的问题:
- 我的假设“节省电池等到已经有网络流量”是否正确?
- 有没有办法在 android 中找出进行投票的正确时机?
到目前为止我所知道的:
我可以注册一个 CONNECTIVITY_CHANGE-broadcastreceiver,通知我网络状态更改(启用/禁用 wlan/gprs)
IntentFilter filterc = new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
registerReceiver(ConnectivityCheckReceiver, filterc);
这是广播接收器
public final BroadcastReceiver ConnectivityCheckReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
String type;
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo TestCo = connectivityManager.getActiveNetworkInfo();
if(TestCo == null)
isLanConnectedNewValue = false;
else
isLanConnectedNewValue = true;
NetworkInfo networkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
if(networkInfo.getType() == ConnectivityManager.TYPE_WIFI)
type = "Wifi";
else if(networkInfo.getType() == ConnectivityManager.TYPE_MOBILE)
type = "umts/3g";
else
type = "other";
if(isLanConnectedNewValue & !isLanConnected){
// good candidate to do polling
}
isLanConnected = isLanConnectedNewValue;
}
};
有没有办法找出网络当前是否正在使用?