i want to write an android app that polls a server. I assume that starting a network transmittion (when there is currently no nework traffic) costs extra battery. So it might be more energy efficient to do the polling when there is already network traffic.

My questions:

  • Is my assumtion "Saving battery to wait until there is already network traffic" correct?
  • Is there a way to find out in android that the right moment has come to do the polling?

What i know so far:

I can register a CONNECTIVITY_CHANGE-broadcastreceiver that informs me about network status change (enabled/disabled wlan/gprs)

        IntentFilter filterc = new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE"); 
        registerReceiver(ConnectivityCheckReceiver, filterc);

here is the broadcast receiver

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;
    }
};

Is there a way to find out if the network is currently in use?

有帮助吗?

解决方案

  • "Saving battery to wait until there is already network traffic" - correct on cellular networks, especially 3G - device energy state (and hence consumed energy) on these networks depends on your network traffic and network-configured inactivity timeouts and the more you send in a batch, the less you spend idling while consuming a lot of energy.

To be more precise, any data transmission above a certain small threshold (usually around 128 bytes) will trigger the device to switch to so called DCH state (Dedicated CHannel) and will stay there after the last bit has been transmitted for another 5-10 seconds until going to FACH state (shared channel - higher latency, low throughput) and in another 0 - 15 seconds will go back to IDLE. The variability here is not random - it depends on network configuration and the mobile device (e.g. does it support Fast Dormancy1?) and these are the values that can be commonly seen in different networks. The sum of both of these timers is the amount of time you waste energy after each transmission.

By the way, the radio in this dedicated channel state uses a comparable amount of power to that of the CPU, even while just waiting in the high energy states.

  • "Is there a way to find out in android that the right moment has come to do the polling?" - not in the current systems, no. You can try and create some clever heuristics, but there is no really good method as the system does not report such information.

In fact, it is barely ever efficient to do polling from energy perspective - try to do push whenever you can instead, either over some long-lived connection that you keep alive at a low (!) periodicity, or using system-native push mechanisms - their systems are quite well optimised and the cost of having a link up is amortised across all apps using the service.

  • Extra bit: there are no APIs for finding out the timers in a particular network. If you really want that, however, a very simple way of estimating them in your current network scenario would be playing around with ping: if you ping with a high enough frequency (e.g. 0.2 seconds), you should see three very different latencies for the packets: slightly above 2000ms, then a few hundred ms and finally 60-80ms. Above 2000ms it means that you are in an idle state - that's how long it takes to do all the signalling to set up a 3G link. The hundreds of ms means that you are in a shared state (FACH) and the lowest band means you are already in the dedicated, high bandwidth state (DCH).

Now yo should be able to figure out an algorithm to compute the estimate with ping, but obviously don't use ping to check what state you are in to decide if now is a good time to send.

edit:

Although it still does not give you information about current network traffic, you may also want to take a look at Android's SyncAdapter - it gives control over to the framework to schedule when Sync operations happen.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top