0

I am developing the android app which runs my web service at app startup. My app is basically the home app of the device. So that's mean that When user will reboot the device, My app will come in front of him.

So far that is great. But I have noticed it that on app start the device takes 5 to 10 seconds to get the internet connected. whereas my web service gets run before the internet is established thus throwing an error of "An error occurred please try again later."

What I Want: Though I am showing that message I am really not satisfied as I really do want to show the user a Message. That message should be "Device is connecting to the internet"

but unfortunately, I am unable to do so. I am using the following code

    public static boolean isNetworkAvailable(Context context) {
    ConnectivityManager connectivityManager
            = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnectedOrConnecting();
}

but it really indicates me if the net is connected or not.

So is not there the case where I can show a message like "Please wait device is connecting to the internet"

Or how can I get that device is restarted not the only app. ... Please help

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
A.s.ALI
  • 1,992
  • 3
  • 22
  • 54
  • please check the answer for [this question](https://stackoverflow.com/questions/2190126/is-there-any-way-to-receive-a-notification-when-the-user-powers-off-the-device/2190184#2190184) – ilya Oct 25 '18 at 11:31
  • I do not need power off sort of thing – A.s.ALI Oct 25 '18 at 11:51
  • in your question you asked "Or how can I get that device is restarted", and in the link I sent you the answer covers the shutdown but also mentioned the restart as the same concept. "another option would be to use the [ACTION_BOOT_COMPLETED] (https://developer.android.com/reference/android/content/Intent?hl=de#ACTION_BOOT_COMPLETED) Intent which is sent when the phone is restarted." – ilya Oct 25 '18 at 13:32

1 Answers1

0

Register a local (or Manifest-declared if you want) broadcast receiver in your activity (documentation here https://developer.android.com/guide/components/broadcasts), listening to connectivity changes, using this intent filter: IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION). Then check if is the device has internet connection with the code you posted. Remember to register your broadcast receiver inside onResume() method and to unregister it inside onPause() method

  • From api 24 and above:

you can use registerDefaultNetworkCallback https://developer.android.com/reference/android/net/ConnectivityManager.html#registerDefaultNetworkCallback(android.net.ConnectivityManager.NetworkCallback)

The callback has a public method called "onAvailable" https://developer.android.com/reference/android/net/ConnectivityManager.NetworkCallback.html#onAvailable(android.net.Network) that is called when a new network is ready to be used.

Nicola Gallazzi
  • 7,897
  • 6
  • 45
  • 64