I am getting warning of deprecated declaration of Broadcast Receiver.
<!-- NETWORK RECEIVER... -->
<receiver android:name=".utils.NetworkUtils" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
WARNING:
Declaring a broadcastreceiver for android.net.conn.CONNECTIVITY_CHANGE is deprecated for apps targeting N and higher. In general, apps should not rely on this broadcast and instead use JobScheduler or GCMNetworkManager.
Is there any other way to use it without deprecated methods?
Actually, the easiest and right way to resolve this problem is to just register the receiver in your main activity and remove the
<receiver/>
code in your Manifest file.Register in Main Activity:
The warning seems to contain the clue that you need to use the job scheduler. The intent here is that the applications don't communicate the intentions to the system regarding the actions they want to do when the application receives connectivity. Job scheduler obviously avoids these problems and that allows Android to batch the requests, defer them, and so on.
The caveat is that you will have to implement doing the work both ways since the job scheduler is only available since Android 5.0. Maybe you can use the libraries which will switch to the native implementation of the job scheduler, another answer listed one of them.
Official advice from Google is to switch to
JobScheduler
. Since this one is available only from API level 21 and higher, it is a no-go for older devices.Luckily, folks from Evernote create a backward compatibility version of that: https://github.com/evernote/android-job
I have created a much easy solution which handles all the OS Version till now. Check Detailed Answer here -> https://stackoverflow.com/a/61082387/8240915
NetworkConnectionLiveData
class will return aLiveData
so we can use easilyI had the same problem, i did something like that. It worked for me, i hope it helps.
Don't add the receiver in the manifest so that it only lives in this activity.
we need to handle old and new methods of connectivity manager like this :