Android - Http request not working after boot completed receiver

48 Views Asked by At

I'm debugging my Android app on a Xiaomi Mi 9T Android 10 API 29 and the following scenario happens:

My application consumes service from my java backend using the okhttp library and everything works fine. Next I restart the device and the receiver below is triggered:

<receiver android:enabled="true" android:exported="true" android:name="com.mysomain.receiver.BootCompletedReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
        <action android:name="android.intent.action.LOCKED_BOOT_COMPLETED" />
        <action android:name="android.intent.action.QUICKBOOT_POWERON"/>
        <action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>
        <action android:name="android.intent.action.MY_PACKAGE_REPLACED"/>
    </intent-filter>
</receiver>

So far so good, my service comes back to life and starts working normally, but the server calls don't work anymore. The error below occurs:

java.net.SocketTimeoutException: failed to connect to mydomain/XXX.XXX.238.40 (port 80) from /192.168.1.9 (port XXXXXX) after 20000ms

My BootCompletedReceiver class triggers a timer to wait for internet connection to be ready before starting jobs

public class BootCompletedReceiver extends BroadcastReceiver {
    private static final String TAG = BootCompletedReceiver.class.getSimpleName();
    private static long TIMER_INTERVAL = 10000;
    private Timer timer = new Timer();

    public void onReceive(Context context, Intent intent) {
        timerStart(context);
    }

    private void timerStart(Context context) {
        this.timer.scheduleAtFixedRate(new TimerTask() {
            public void run() {
                ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
                NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
                boolean connected = activeNetworkInfo != null && activeNetworkInfo.isConnected();
                if (connected) {
                    timer.cancel();
                    context.startService(new Intent(context, StartWorksService.class));
                }
            }
        }, 0, TIMER_INTERVAL);
    }

}

What changes after the restart, since before it the requests worked? How can I fix this problem?

Thanks! Any help is greatly appreciated

0

There are 0 best solutions below