Android.net.conn.CONNECTIVITY_CHANGE broadcast transmitter does not work in JellyBean to connect and disconnect VPN

This is the manifest part

<receiver android:name="my.com.app.ConnectivityModifiedReceiver" android:label="NetworkConnection" > <intent-filter> <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> </intent-filter> </receiver> 

This is java code

 public class my.com.app.ConnectivityModifiedReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { context.sendBroadcast(new Intent("ConnectivityModified")); } } 

ConnectivityModifiedReceiver will send intentions according to the change in network connection. In my case, connecting and disconnecting a VPN.

I get intentions at Lollipop, but not at JellyBean.

Help Plz

+2
source share
2 answers

You can try on onReceive if this helps:

 @Override public void onReceive(Context context, Intent intent) { final ConnectivityManager connMgr = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo netInfo = connMgr.getActiveNetworkInfo(); // should check null because in air plan mode it will be null if (netInfo != null && netInfo.isConnected()) { System.out.println("INTERNET IS AVAILABLE"); } else { System.out.println("INTERNET UNAVAILABLE"); } } 

You can place your logic inside an if statement.

+2
source

Still in my conclusions

At lollipop

android.net.conn.CONNECTIVITY_CHANGE is only performed if the VPN is connected or disconnected.

Thus, you can use the following code snippet along with the logic that you use for devices with pre-treatment.

 @Override public void onReceive(Context context, Intent intent) { Bundle bundle = intent.getExtras(); String KeyNI="networkInfo"; android.net.NetworkInfo bundleNetworkInfo=(android.net.NetworkInfo)bundle.get(KeyNI); if(!bundleNetworkInfo.getTypeName().toString().equalsIgnoreCase("VPN")) { context.sendBroadcast(new Intent("ConnectivityModified")); } } 

Hope this helps.

+2
source

Source: https://habr.com/ru/post/905917/


All Articles