Android: connection problems in the background thread after an alarm when "always on" is turned off

Description of the problem:

In my Android application, I am experiencing connection problems with the remote HTTP protocol ("polling") from AsyncTask, which was launched after the alarm went off.

The search works very well when the standard Android installation “Enable always turned on mobile data” (Settings → Wireless and networks → Mobile networks) to “on” .

Solution that works: alarm, Android "Service" receives the intent of the alarm, starts the background thread (AsyncTask). The new thread receives a partial trace lock, establishes a connection (polling), notifies the user and releases the trace lock.

So far so good. The problem is that when it is always turned off, “off” , polling does not take place most of the time if the phone has been in standby mode for some time (> 30 minutes).

Since the polling stream sends a notification, I directly receive feedback on a failed polling attempt.

Motivation:

Many users turn "always on" to reduce battery drain. Thus, it is likely that application users are facing problems. I want to handle or prevent the "errors" that users encounter.

Decision:

I experimented a lot without any major breakthroughs:

  • several attempts and intermediate dreams to give the phone some time to establish a connection.
  • http parameters (timeouts, etc.).
  • another HttpClient (Apache)

Questions:

  • What does it mean , the value of the parameter "always-on" means and what should developers consider?
  • I wonder if it is possible implement an interrogation mechanism based on an alarm that can establish a data connection, even if it is "always on" off.
  • ( C2DM)?

:

, Android " ". , , , .

+3
2

, :

:

  • " ". Verizon DroidX, . .

  • , , , , . , (, ), Android, , . , , , HTTP- Keep-Alive, , .

  • Socket IOException, EOFException , , . - NetworkInterface - :

.

private OnCheckNetworkConnectivity networkConnectivityCallback = new OnCheckNetworkConnectivity() {
        String ipAddress;

        public boolean isConnected() {
            String newIpAddress = getLocalIpAddress();
            if(newIpAddress != null) {
            if(ipAddress == null) {
                ipAddress = newIpAddress;
                return true;
            }
            if(!newIpAddress.equals(ipAddress)) {
                ipAddress = newIpAddress;
                return false;
            }

            // still the same IP address, we should still have the same connection
            return true;
            }

            ipAddress = null;
            return false;
        }

        public String getLocalIpAddress() {
            try {
                for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
                    NetworkInterface intf = en.nextElement();
                    for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                        InetAddress inetAddress = enumIpAddr.nextElement();
                        if (!inetAddress.isLoopbackAddress()) {
                            return inetAddress.getHostAddress();
                        }
                    }
                }
            } catch (SocketException ex) {
            return null;
            }
            return null;
        }
    };
0

AlarmManager, .

, , .

:

AlarmManager. , :

AlarmManager . :

  • CONNECTIVITY_CHANGE
  • ( ).
  • , , .

CONNECTIVITY_CHANGE.

0

All Articles