1
  1. I searched and it turns out that Broadcast Receiver is not recommended for the target over N.

  2. I found out GCMNetworkManager. And it is also depreciated.

  3. I also checked JobScheduler. However, its behaviour is not something that I want to implement.

The only way that I can implement among the three is checking the internet status periodically.

However, I don't want it. What I want is detecting the event when user turns them off or on. And then, I'd like to show a dialog to make sure the user turn it on again.

My college who makes iOS says, he uses Observer for his iOS app. And said maybe Android is the same.

Is there any way to make it possible?

c-an
  • 3,543
  • 5
  • 35
  • 82
  • Starting from Nougat (7.x) there are JobScheduler and JobService: https://stackoverflow.com/questions/48527171/detect-connectivity-change-in-android-7-and-above-when-app-is-killed-in-backgrou Sincerily I never heard that "a developer don't want to implement a Class"...it's funny – emandt Jun 21 '19 at 09:07
  • @emandt, What do you mean? I just don't want to keep checking the status periodically. But detecting the event when the user turn it off or turn it on. The two things are different. – c-an Jun 21 '19 at 09:10
  • Infact JobScheduler does exactly the thing you need: internally it checks for "registered events" and then it will raise YOUR event when YOUR particolar condition occurs. It will free the developer from doing loops, timers, polling actions, etc... – emandt Jun 21 '19 at 09:13
  • Hmm I see. So, you mean, it doesn't run 'periodically' but detects particular event 'instantly'. How? – c-an Jun 22 '19 at 13:00
  • 1
    Why do not read official guide? https://developer.android.com/reference/android/app/job/JobScheduler You will find this: "When the criteria declared are met, the system will execute this job on your application's JobService". And a quick search on Google reveals this link: https://stackoverflow.com/questions/37217283/detect-network-state-change-using-jobschedulers-in-android (all this has taken 1 minute instead of a full weekend of delay/wait) – emandt Jun 24 '19 at 08:03

1 Answers1

0

You could consider those in Manifest.

        <receiver android:name="receiver.NetworkChangeReceiver">
            <intent-filter>
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
                <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
            </intent-filter>
        </receiver>

However, those are deprecated.

Instead, you can declare it programmatically.

  1. Create a receiver class.
class NetworkChangeReceiver : BroadcastReceiver() {
    var dialog: Dialog? = null

    override fun onReceive(context: Context, intent: Intent) {
        val isConnected = NetworkUtil.getConnectivityStatusString(context)

        Toast.makeText(context, "Intere State Changed: $isConnected", Toast.LENGTH_SHORT).show()
    }
}
  1. In your Activity.
// Internet Check Receiver
val intentFilter = IntentFilter("android.net.conn.CONNECTIVITY_CHANGE")
this.registerReceiver(NetworkChangeReceiver(), intentFilter)
c-an
  • 3,543
  • 5
  • 35
  • 82