Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Network listener Android

I want to check when the network of phone in Android goes off. Can I capture that event?

I am not getting the proper API or any example which would explain the same. If anyone had done or any example links would be really helpful.

like image 556
Sam97305421562 Avatar asked Nov 23 '09 13:11

Sam97305421562


People also ask

How do I check permissions on Android internet?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − To find the internet status we have to add network state permission to AndroidManifest. xml file as shown below.

What is network manager in Android?

Network Manager is a useful app that lets you quickly and easily manage your networks and devices. And as an added bonus, it comes with tons of additional features that are sure to come in handy. Network Manager is incredibly intuitive to use.


2 Answers

New java class:

public class ConnectionChangeReceiver extends BroadcastReceiver {   @Override   public void onReceive( Context context, Intent intent )   {     ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService( Context.CONNECTIVITY_SERVICE );     NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();     NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo(     ConnectivityManager.TYPE_MOBILE );     if ( activeNetInfo != null )     {       Toast.makeText( context, "Active Network Type : " + activeNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show();     }     if( mobNetInfo != null )     {       Toast.makeText( context, "Mobile Network Type : " + mobNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show();     }   } } 

New xml in your AndroidManifest.xml under the "manifest" element:

<!-- Needed to check when the network connection changes --> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 

New xml in your AndroidManifest.xml under the "application" element:

<receiver android:name="com.blackboard.androidtest.receiver.ConnectionChangeReceiver"           android:label="NetworkConnection">   <intent-filter>     <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>   </intent-filter> </receiver> 
like image 131
Eric Avatar answered Sep 25 '22 09:09

Eric


I have been using a small setup to check the bandwidth for determining how to scale things, such as images.

Under the activity, in AndroidManifest:

<intent-filter> ...     <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/> </intent-filter> 

In the activity where the checks are being performed:

boolean network; int bandwidth;  @Override public void onCreate(Bundle savedInstanceState) {     ...     network = isDataConnected();     bandwidth = isHighBandwidth();     registerReceiver(new BroadcastReceiver() {         public void onReceive(Context context, Intent intent) {             network = isDataConnected();             bandwidth = isHighBandwidth();         }     }, new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE"));     ... } ... private boolean isDataConnected() {     try {         ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);         return cm.getActiveNetworkInfo().isConnectedOrConnecting();     } catch (Exception e) {         return false;     } }  private int isHighBandwidth() {     ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);     NetworkInfo info = cm.getActiveNetworkInfo();     if (info.getType() == ConnectivityManager.TYPE_WIFI) {         WifiManager wm = (WifiManager) getSystemService(Context.WIFI_SERVICE);         return wm.getConnectionInfo().getLinkSpeed();     } else if (info.getType() == ConnectivityManager.TYPE_MOBILE) {         TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);         return tm.getNetworkType();     }     return 0; } 

An example usage would then be:

if (network) {     if (bandwidth > 16) {         // Code for large items     } else if (bandwidth <= 16 && bandwidth > 8) {         // Code for medium items     } else {         //Code for small items     } } else {     //Code for disconnected } 

It's not the prettiest, but it allows enough flexibility that I can change the bandwidth cutoff for items depending on what they are and my requirements for them.

like image 21
Abandoned Cart Avatar answered Sep 25 '22 09:09

Abandoned Cart