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?
As per the provided link in the teacher's notes, https://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html#MonitorChanges declaring BroadcastReceivers in the manifest is deprecated from Android 7.0 and up.
android.net.ConnectivityManager. Class that answers queries about the state of network connectivity. It also notifies applications when network connectivity changes. The primary responsibilities of this class are to: Monitor network connections (Wi-Fi, GPRS, UMTS, etc.)
Retrieve the current result extra data, as set by the previous receiver. This can be called by an application in onReceive(Context, Intent) to allow it to keep the broadcast active after returning from that function.
1.1. A broadcast receiver (receiver) is an Android component which allows you to register for system or application events. All registered receivers for an event are notified by the Android runtime once this event happens.
I had the same problem, i did something like that. It worked for me, i hope it 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();
}
}
}
}
}
Don't add the receiver in the manifest so that it only lives in this activity.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With