CONNECTIVITY_CHANGE deprecated for Android N purposes

I get a warning about an outdated broadcast receiver declaration.

<!-- NETWORK RECEIVER... --> <receiver android:name=".utils.NetworkUtils" > <intent-filter> <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> </intent-filter> </receiver> 

Attention:

Broadcast Broadcast Ad for android.net.conn.CONNECTIVITY_CHANGE is deprecated for apps targeting N or higher. In general, applications should not rely on this translation and use JobScheduler or GCMNetworkManager instead.

Is there any other way to use it without obsolete methods?

+12
android deprecated android-networking broadcastreceiver intentfilter
source share
5 answers

I had the same problem, I did something similar. It worked for me, I hope this helps.

 public class NewActivity extends AppCompatActivity { final static String CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE"; IntentFilter intentFilter; MyReceiver receiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_new); intentFilter = new IntentFilter(); intentFilter.addAction(CONNECTIVITY_ACTION); receiver = new MyReceiver(); if(checkForInternet()){ loadData(); }else{ updateUI(); } } @Override protected void onResume() { super.onResume(); registerReceiver(receiver, intentFilter); } @Override protected void onPause() { super.onPause(); unregisterReceiver(receiver); } // Self explanatory method public boolean checkForInternet() { ConnectivityManager cm = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); return activeNetwork != null && activeNetwork.isConnectedOrConnecting(); } void loadData(){ // do sth } void updateUI(){ // No internet connection, update the ui and warn the user } private class MyReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String actionOfIntent = intent.getAction(); boolean isConnected = checkForInternet(); if(actionOfIntent.equals(CONNECTIVITY_ACTION)){ if(isConnected){ loadData(); }else{ updateUI(); } } } } } 

Do not add the receiver to the manifest so that it only works in this action.

+10
source share

Google’s official advice is to switch to JobScheduler . Since this access is only available from API level 21 and above, this does not work for older devices.

Fortunately, the Evernote people are creating a backward compatibility version: https://github.com/evernote/android-job

+1
source share

The warning seems to contain the key that the task scheduler needs to use. The goal here is that applications do not communicate system intentions regarding the actions they want to take when the application receives a connection. The Task Scheduler explicitly avoids these problems and allows Android to execute batch requests, defer them, etc.

The caveat is that you will have to do the work in both directions since the task scheduler is only available with Android 5.0. Perhaps you can use libraries that will switch to your own task scheduler implementation, another answer that lists one of them.

0
source share

In fact, the easiest and most correct way to solve this problem is to simply register the recipient in your main activity and delete the <receiver/> code in the manifest file.

Register in the main activity:

 IntentFilter intentFilter = new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE"); this.registerReceiver(new YourConnectionReceiverClass(), intentFilter); 
0
source share

we need to handle the old and new methods of the connection manager, like this:

 if (SDK_INT >= LOLLIPOP) { NetworkRequest.Builder builder = new NetworkRequest.Builder(); builder.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET); builder.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR); builder.addTransportType(NetworkCapabilities.TRANSPORT_WIFI); builder.addTransportType(NetworkCapabilities.TRANSPORT_VPN); ConnectivityManager.NetworkCallback callback = new ConnectivityManager.NetworkCallback() { @Override public void onAvailable(Network network) { super.onAvailable(network); publishSubject.onNext(isOnline()); } @Override public void onLost(Network network) { super.onLost(network); publishSubject.onNext(isOnline()); } }; connectivityManager.registerNetworkCallback(builder.build(), callback); } else { IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION); BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { publishSubject.onNext(isOnline()); } }; application.registerReceiver(receiver, filter); } 
0
source share

All Articles