Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CONNECTIVITY_CHANGE deprecated in target of Android N [duplicate]

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?

like image 713
Pratik Butani Avatar asked Nov 11 '16 09:11

Pratik Butani


People also ask

Is broadcast receiver deprecated in Android?

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.

What is ConnectivityManager in Android?

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.)

What is the role of the onReceive () method in the Broadcastreceiver?

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.

What is dynamic broadcast receiver in Android?

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.


1 Answers

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.

like image 199
Tahir Ferli Avatar answered Sep 20 '22 12:09

Tahir Ferli