Android N not sending broadcast android.net.conn.CONNECTIVITY_CHANGE?

I defined the receiver in the Android Android app for the sandbox:

<receiver android:exported="true" android:name="com.sandboxapplication.NetworkReceiver"> <intent-filter> <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> </intent-filter> </receiver> 

It is pretty simple:

 public class NetworkReceiver extends BroadcastReceiver { private static final String TAG = NetworkReceiver.class.getName(); @Override public void onReceive(Context context, Intent intent) { Log.i(TAG, "Received Network Change event."); } } 

This receiver works fine if my targetSdkVersion is 23 in my build.gradle file. However, if I set myTestVVersion to 24, the receiver will never receive anything. In fact, if I put a debug breakpoint in my receiver, Android Studio gives me a visual indication that it looks like the class never loads into memory.

Am I missing something very simple in the Android N documentation? Is there a new way to detect communication change events?

+15
android android-7.0-nougat
Aug 22 '16 at 10:16
source share
3 answers

Android N (Nougat) apps do not receive CONNECTIVITY_ACTION broadcasts, even if they have manifest entries to request notification of these events. Applications that run can still listen on CONNECTIVITY_CHANGE in their main thread if they request a notification using BroadcastReceiver .

To find out what has changed in Android N (Nougat) . See the link below. Android N Behavior Changes

+21
Aug 22 '16 at 10:18
source share

Use this code to register the recipient in your Activity or in the Application class

 IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTI‌​ON); registerReceiver(new NetworkConnectionReceiver(), intentFilter); 

Where NetworkConnectionReceiver is a class extended by BroadcastReceiver . Just add this class to your application and execute the action in onReceive(Context context, Intent intent) .

Note. If you register this recipient in the " Activity " section, do not forget to unregister it.

+23
Apr 19 '17 at 10:33 on
source share

Meanwhile, ConnectivityManager.CONNECTIVITY_ACTI‌​ON deprecated:

 @deprecated apps should use the more versatile {@link #requestNetwork}, {@link #registerNetworkCallback} or {@link #registerDefaultNetworkCallback} functions instead for faster and more detailed updates about the network changes they care about. 

Therefore, registerDefaultNetworkCallback should be used:

 ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); cm.registerDefaultNetworkCallback(new ConnectivityManager.NetworkCallback(){ @Override public void onAvailable(Network network) { doOnNetworkConnected(); } }); 
0
Dec 14 '18 at 15:09
source share



All Articles