2

I've an app that has a receiver of android.intent.action.BOOT_COMPLETED and start a private VPN. But when I receive a android.intent.action.BOOT_COMPLETED the network isn't ready yet. What can I do to start a android.intent.action.BOOT_COMPLETEDand wait to have a network status? for example I can have a android.intent.action.BOOT_COMPLETED but the phone has no internet connection, or I can have a android.intent.action.BOOT_COMPLETED and 5 seconds later a internet Access.

Alessio Crestani
  • 1,602
  • 4
  • 17
  • 38
  • You must not wait in a broadcast receiver, but you may use one of the acceptable methods to schedule something to run a second or so later to check again, repeatedly. – Chris Stratton Apr 22 '15 at 14:47

2 Answers2

3

You could register a broadcast receiver listening on the android.net.conn.CONNECTIVITY_CHANGE action.

then check if you are connected like this:

public boolean isOnline(Context context) {

ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
//should check null because in air plan mode it will be null
return (netInfo != null && netInfo.isConnected());

}

source and more information: Broadcast receiver for checking internet connection in android app

Community
  • 1
  • 1
Chris K.
  • 1,060
  • 8
  • 10
  • "check if you are connected" isn't exactly what I'm lookinf for. If I'm inside the onBoot method triggered by BOOT_COMPLETED, i will wait the initial network boot and get the result of "have internet" or "not have internet" and continue with the correct method. If i only lookup con network change, i will wait until internet is active, so I can potentially wait infinite time until the internet will come. I just want a network ready-> internet yes/no -> do the right thing, because at BOOT_COMPLETED the 3g signal isn't ready. – Alessio Crestani Apr 22 '15 at 14:42
  • then call isOnline() at BOOT_COMPLETED. if it is true do the stuff you want to do if internet is available. if it returns false, do the other stuff. – Chris K. Apr 22 '15 at 14:47
  • on boot_complete the network boot isn't setupped yet by the phone, so It mathematically choose the option isOnline's off. – Alessio Crestani Apr 22 '15 at 14:59
2

You can always listen to multiple items on one receiver:

<receiver android:name=".ReceiverName" >
    <intent-filter >
        <action android:name="android.net.wifi.STATE_CHANGE" />
        <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

So after boot you just wait for connectivity change and do your stuff.

Klawikowski
  • 605
  • 3
  • 11
  • "wait for connectivity change" isn't exactly what I'm lookinf for. If I'm inside the onBoot method triggered by BOOT_COMPLETED, i will wait the initial network boot and get the result of "have internet" or "not have internet" and continue with the correct method. If i only lookup con network change, i will wait until internet is active, so I can potentially wait infinite time until the internet will come. I just want a network ready-> internet yes/no -> do the right thing, because at BOOT_COMPLETED the 3g signal isn't ready. – Alessio Crestani Apr 22 '15 at 14:42
  • Receiver will trigger 3 callbacks on Boot on ConnectivityChange and / or Wifi Stage Change ... When you will receive onConnectivityChange - check if is online (snippet below) and thats it. – Klawikowski Apr 22 '15 at 15:43