Connectivitymanager.connectivity_action Get Network Disconected From In Api >= 14?
I need to get the network from which device was disconnected. Now I use: NetworkInfo ni =intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO); And check: ni.isConnect
Solution 1:
You can use the following
ConnectivityManagerconnectivityManager= (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
intnetworkType= intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_TYPE);
booleanisWiFi= networkType == ConnectivityManager.TYPE_WIFI;
booleanisMobile= networkType == ConnectivityManager.TYPE_MOBILE;
NetworkInfonetworkInfo= connectivityManager.getNetworkInfo(networkType);
booleanisConnected= networkInfo.isConnected();
if (isWiFi) {
if (isConnected) {
Log.i("APP_TAG", "Wi-Fi - CONNECTED");
} else {
Log.i("APP_TAG", "Wi-Fi - DISCONNECTED");
}
} elseif (isMobile) {
if (isConnected) {
Log.i("APP_TAG", "Mobile - CONNECTED");
} else {
Log.i("APP_TAG", "Mobile - DISCONNECTED");
}
} else {
if (isConnected) {
Log.i("APP_TAG", networkInfo.getTypeName() + " - CONNECTED");
} else {
Log.i("APP_TAG", networkInfo.getTypeName() + " - DISCONNECTED");
}
}
Solution 2:
You can get the instance of NetworkInfo through the Context.
ConnectivityManagerconnectivityManager= ((ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE));
NetworkInfocurrentNetworkInfo= connectivityManager.getActiveNetworkInfo();
if(currentNetworkInfo != null && currentNetworkInfo.isConnected()){
// Your logic goes in here
}
Solution 3:
Working non deprecated code
/**Receiver*/publicclassNetworkStateReceiverextendsBroadcastReceiver {
/*
* @see android.content.BroadcastReceiver#onReceive(android.content.Context,
* android.content.Intent)
*/@OverridepublicvoidonReceive(Context context, Intent intent) {
ConnectivityManagerconnectivityManager= (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
intnetworkType= intent.getExtras().getInt(ConnectivityManager.EXTRA_NETWORK_TYPE);
booleanisWiFi= networkType == ConnectivityManager.TYPE_WIFI;
booleanisMobile= networkType == ConnectivityManager.TYPE_MOBILE;
NetworkInfonetworkInfo= connectivityManager.getNetworkInfo(networkType);
booleanisConnected= networkInfo.isConnected();
if (isWiFi) {
if (isConnected) {
Log.i("APP_TAG", "Wi-Fi - CONNECTED");
} else {
Log.i("APP_TAG", "Wi-Fi - DISCONNECTED");
}
} elseif (isMobile) {
if (isConnected) {
Log.i("APP_TAG", "Mobile - CONNECTED");
} else {
Log.i("APP_TAG", "Mobile - DISCONNECTED");
}
} else {
if (isConnected) {
Log.i("APP_TAG", networkInfo.getTypeName() + " - CONNECTED");
} else {
Log.i("APP_TAG", networkInfo.getTypeName() + " - DISCONNECTED");
}
}
}
}
And in manifest
<!-- Receiver registration in manifest --><receiverandroid:name="com.xxx.yyy.NetworkStateReceiver" ><intent-filter><actionandroid:name="android.net.conn.CONNECTIVITY_CHANGE" /></intent-filter></receiver>
and
<!-- Internet permission for network comunication --><uses-permissionandroid:name="android.permission.ACCESS_NETWORK_STATE" />
Post a Comment for "Connectivitymanager.connectivity_action Get Network Disconected From In Api >= 14?"