Unsuccessful attempts to send data to an external server when receiving an alarm (only in some terminals)

1

Good morning, I would appreciate any idea that may give light to the following issue: I made an application (not published) in which the user programs one or several alarms that upon receiving a query to an external server.

The process works well in most terminals, however there are some that do not. In these only works if the application is open or recently defocused.

At first I thought it was the manufacturer's personalization, so I set the app as protected in memory, allowed it to start with the system and all the permissions that can be given. None of this has served.

At first I used a BroadcastReceiver to receive the alarms. After doing some brief checks, it launched an asynchronous process (AsyncTask) that made the connection in a classic way:

HttpURLConnection urlConnection = null;
try {
        URL url = new URL(Constantes.APIURL + codeApp);
        urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestMethod("GET");
        urlConnection.connect();  // <-- Aquí se produce una excepción
        // e: "java.net.ConnectException: failed to connect to servidor.com/123.456.789.111 (port 80): connect failed: ECONNREFUSED (connection refused)"
        ....
}

Apparently the phone goes into sleep mode and does not let the application use the network. Pass with Wifi and mobile data (this is just a guess, what is clear is that the app normally closes and starts with the receiver that causes the AlarmManager).

Later I used the Volley library to make the connection. I removed the AsyncTask because the library itself is responsible for that work and the result has been the same: if the app is open or recently closed everything works fine, but if the terminal is unused the same error occurs (alarms are always received , the call to the webservice does not).

Then I decided to extend the WakefulBroadcastReceiver receiver:

public class RecibidorAlarma extends WakefulBroadcastReceiver {  // De la doc oficial: WakefulBroadcastReceiver -> creating and managing a PARTIAL_WAKE_LOCK for your app
    Log.d("ALARMA ", "Alarma recibida");
    int idAlarmaBD = Integer.valueOf(intent.getStringExtra("id"));
    Intent servicio = new Intent(context, ComprobarHijoService.class);
    servicio.putExtra("idalarma", idAlarmaBD);
    startWakefulService(context, servicio);
}

public class ComprobarHijoService extends IntentService {
    @Override
    protected void onHandleIntent(Intent intent){
        .....
        /* Procesos y conexión con Volley */
    RecibidorAlarma.completeWakefulIntent(intent);
    }
}

In this way the same results were produced. I thought that as Volley creates a new thread this would not be protected by the Wakelock and the system could close it. So I forced Volley to run in the same thread; as I read there one way to get it is like this:

String url = Constantes.APIURL + codeApp;
RequestQueue requestQueue = Volley.newRequestQueue(contexto);
RequestFuture<String> future = RequestFuture.newFuture();
StringRequest request = new StringRequest(Request.Method.GET, url, future, future);
request.setRetryPolicy(new DefaultRetryPolicy(
    6000,  // 6 seg de espera al servidor externo antes de fallar
    6,  // 6 intentos de conexión --    DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
    DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));  // 1f
requestQueue.add(request);

try {
    String respuesta = future.get(40, TimeUnit.SECONDS); // Esperamos 40 seg antes de enviar excepción
    Gson gson = new Gson();
    ......
}

Now the log indicates the following:

03-24 09:46:31.376 9142-9142/com.myapps.aplicacion D/ALARMA: Alarma recibida
03-24 09:46:31.406 9142-30833/ocom.myapps.aplicacion D/ALARMA: Alarma 1 recibida. Se comprobará si debe ejecutarse el código...
03-24 09:46:59.776 9142-30833/com.myapps.aplicacion D/ALARMA: Error en alarma 1: 
03-24 09:47:00.786 9142-30833/com.myapps.aplicacion W/System.err: java.util.concurrent.ExecutionException: com.android.volley.NoConnectionError: java.net.ConnectException: failed to connect to servidor.com/123.456.789.123 (port 80) after 6000ms: isConnected failed: ECONNREFUSED (Connection refused)
03-24 09:47:00.786 9142-30833/com.myapps.aplicacion W/System.err:     at com.android.volley.toolbox.RequestFuture.doGet(RequestFuture.java:117)
03-24 09:47:00.796 9142-30833/com.myapps.aplicacion W/System.err:     at com.android.volley.toolbox.RequestFuture.get(RequestFuture.java:97)
03-24 09:47:00.796 9142-30833/com.myapps.aplicacion W/System.err:     at com.myapps.aplicacion.herramientas.ComprobarHijoService.onHandleIntent(ComprobarHijoService.java:93)
03-24 09:47:00.796 9142-30833/com.myapps.aplicacion W/System.err:     at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java)
03-24 09:47:00.796 9142-30833/com.myapps.aplicacion W/System.err:     at android.os.Handler.dispatchMessage(Handler.java)
03-24 09:47:00.796 9142-30833/com.myapps.aplicacion W/System.err:     at android.os.Looper.loop(Looper.java)
03-24 09:47:00.796 9142-30833/com.myapps.aplicacion W/System.err:     at android.os.HandlerThread.run(HandlerThread.java)
03-24 09:47:00.796 9142-30833/com.myapps.aplicacion W/System.err: Caused by: com.android.volley.NoConnectionError: java.net.ConnectException: failed to connect to servidor.com/123.456.789.123 (port 80) after 6000ms: isConnected failed: ECONNREFUSED (Connection refused)
03-24 09:47:00.806 9142-30833/com.myapps.aplicacion W/System.err:     at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:158)
03-24 09:47:00.806 9142-30833/com.myapps.aplicacion W/System.err:     at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:114)
03-24 09:47:00.806 9142-30833/com.myapps.aplicacion W/System.err: Caused by: java.net.ConnectException: failed to connect to servidor.com/123.456.789.123 (port 80) after 6000ms: isConnected failed: ECONNREFUSED (Connection refused)
03-24 09:47:00.816 9142-30833/com.myapps.aplicacion W/System.err:     at libcore.io.IoBridge.isConnected(IoBridge.java:223)
03-24 09:47:00.816 9142-30833/com.myapps.aplicacion W/System.err:     at libcore.io.IoBridge.connectErrno(IoBridge.java:161)
03-24 09:47:00.816 9142-30833/com.myapps.aplicacion W/System.err:     at libcore.io.IoBridge.connect(IoBridge.java:112)
03-24 09:47:00.816 9142-30833/com.myapps.aplicacion W/System.err:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
03-24 09:47:00.816 9142-30833/com.myapps.aplicacion W/System.err:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:460)
03-24 09:47:00.816 9142-30833/com.myapps.aplicacion W/System.err:     at java.net.Socket.connect(Socket.java:833)
03-24 09:47:00.826 9142-30833/com.myapps.aplicacion W/System.err:     at com.android.okhttp.internal.Platform.connectSocket(Platform.java:152)
03-24 09:47:00.826 9142-30833/com.myapps.aplicacion W/System.err:     at com.android.okhttp.Connection.connect(Connection.java:101)
03-24 09:47:00.826 9142-30833/com.myapps.aplicacion W/System.err:     at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:294)
03-24 09:47:00.826 9142-30833/com.myapps.aplicacion W/System.err:     at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
03-24 09:47:00.826 9142-30833/com.myapps.aplicacion W/System.err:     at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
03-24 09:47:00.826 9142-30833/com.myapps.aplicacion W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
03-24 09:47:00.826 9142-30833/com.myapps.aplicacion W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296)
03-24 09:47:00.836 9142-30833/com.myapps.aplicacion W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:503)
03-24 09:47:00.836 9142-30833/com.myapps.aplicacion W/System.err:     at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:110)
03-24 09:47:00.836 9142-30833/com.myapps.aplicacion W/System.err:     at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:97)
03-24 09:47:00.836 9142-30833/com.myapps.aplicacion W/System.err:   ... 1 more
03-24 09:47:00.836 9142-30833/com.myapps.aplicacion W/System.err: Caused by: libcore.io.ErrnoException: isConnected failed: ECONNREFUSED (Connection refused)
03-24 09:47:00.836 9142-30833/com.myapps.aplicacion W/System.err:     at libcore.io.IoBridge.isConnected(IoBridge.java:208)
03-24 09:47:00.846 9142-30833/com.myapps.aplicacion W/System.err:   ... 16 more

Well the thing is now like this .. Do you see any concept error? Is this the best way to solve the problem? How do you make calls to external servers from a broadcast receiver?

Thank you for having read it!

PD. I could not put more appropriate tags to the post for lack of Reputation: - (

    
asked by Victor C 24.03.2016 в 10:07
source

2 answers

0

Solved. The two terminals that gave problems used MIUI as Android personalization, as it turns out that in Settings - Others - Battery you can use specific profiles for each app. Well, it has been a matter of giving permission to connect to the network in the background and problem solved.

Thanks to those who have spent some time looking at the matter.

    
answered by 24.03.2016 / 22:09
source
0

The problem does not lie in the calls, maybe you have not noticed but the connection is denied, because simply the url does not have confidence, "trusted connection", (you do not know how to say it in Spanish), you should check that your urls can be visible.

In fact I notice a connection attempt with Volley and you also have the same error with the 6 seconds timeout you have defined:

Caused by: com.android.volley.NoConnectionError: java.net.ConnectException: failed to connect to servidor.com/123.456.789.123 (port 80) after 6000ms: isConnected failed: ECONNREFUSED (Connection refused)

Is this really the url of the server you want to access?:

servidor.com/123.456.789.123

This may be the cause of your problem.

  • As an important note, if you are using an emulator remember that the host should be 10.0.2.2 and NO 127.0.0.1 .
answered by 24.03.2016 в 18:46