0

https://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html

ConnectivityManager.CONNECTIVITY_ACTION will not working statically in Android 7.0 as per Android Developer but why it is not working in older versions of android

User000
  • 31
  • 2
  • 1
    Most likely ConnectivityManager is being depreciated and won't work for newer models the same way it works for other models. A good example is a recycler viewer this works different depending on which sdk you use – MNM Dec 01 '16 at 05:08
  • I want to monitor Network connectivity for my app , even when app is not running , any suggestions ? – User000 Dec 01 '16 at 05:13
  • @MNM There is no indication that `ConnectivityManager` is being deprecated. Also, `RecyclerView` does not behave differently depending on which platform version you use because `RecyclerView` is not part of the platform. It is a separate library, which is compiled into your APK no matter which platform version it will eventually run on. – Karakuri Dec 01 '16 at 05:13
  • @User000 What makes you think it's not working statically on older platform levels? Please provide some basis for this conclusion. Even if that were the case, you could still use it dynamically (as is intended on 7.0 and later). Perhaps if you can describe _why_ you need monitor network connectivity when your app is not running, someone can suggest an alternative solution to your problem (e.g., if you need to make network calls at some future time, you should look at `JobScheduler` instead). – Karakuri Dec 01 '16 at 05:17
  • when internet is connected I have to upload data to the sever which user has made in offline mode even though when app is not running , so for that network connection monitoring is required . – User000 Dec 01 '16 at 05:35
  • @Karakuri @ MNM when internet is connected I have to upload data to the sever which user has made in offline mode even though when app is not running , so for that network connection monitoring is required .Any Suggestions for this scenario ? – User000 Dec 01 '16 at 06:54
  • @Karakuri : I have checked in targetSdkVersion 22 ,with below code My app is not receiving the BR. – User000 Dec 01 '16 at 09:45
  • @User000 you should edit your post to add more details and code, not post them in comment. – Karakuri Dec 02 '16 at 01:12

1 Answers1

1

You do not need to monitor network connectivity at all times for this use case. When the user is using your app, then you can check network as per the link you provided. If you are not connected, you can use the JobScheduler to schedule a job and use jobInfoBuilder.setRequiredNetworkType(NETWORK_TYPE_ANY), and the system will run your job when there is network connectivity even if your app has since gone to the background. The job should simply start up a Service that uploads whatever changes are pending.

Alternatively, if it fits your use case, you can write a SyncAdapter and simply request a sync, and the system's SyncManager will run your SyncAdapter when appropriate.

Karakuri
  • 38,365
  • 12
  • 84
  • 104
  • Thank you for your suggestion . I want something Event driven like Broadcast receiver , where in my app I can get the notification whenever Internet is connected (app is not in background) . Using Job scheduler app cannot notification of Internet connectivity ,it schedules the job on time basis whereas i want it to be event driven. – User000 Dec 03 '16 at 15:55
  • The `JobScheduler` APIs do not suggest to me at all that the jobs are time based. Even so, if your app is in the background, it shouldn't matter; you don't _need_ to upload to the server the very instant there is connectivity, do you? (the fact that you're making the app work offline suggests the answer to that question is no) – Karakuri Dec 03 '16 at 20:34
  • Thank you , It worked for API >= 21 . but how to support for API < 21 – User000 Dec 06 '16 at 06:41
  • @User000 It seems `GcmNetworkManager` is the way to do that. More information here: http://stackoverflow.com/a/30532779/1207921 – Karakuri Dec 06 '16 at 18:43
  • @User000 going through the comments in that answer, it appears Firebase Job Dispatcher is actually better: https://github.com/firebase/firebase-jobdispatcher-android – Karakuri Dec 06 '16 at 18:44