Home > Java > javaTutorial > body text

How to Detect and Handle Network Connectivity Changes in Android?

Barbara Streisand
Release: 2024-10-24 19:22:02
Original
372 people have browsed it

How to Detect and Handle Network Connectivity Changes in Android?

Monitoring Network Connectivity in Android

Problem:

How can you monitor changes in network connectivity on an Android device and capture network outage events?

Answer:

To capture network outage events in Android, follow these steps:

1. Create a Broadcast Receiver:

Create a new Java class that extends the BroadcastReceiver class:

<code class="java">public class ConnectionChangeReceiver extends BroadcastReceiver
{
  @Override
  public void onReceive( Context context, Intent intent )
  {
    // Network connectivity change handling code goes here
  }
}</code>
Copy after login

2. Register the Broadcast Receiver:

Add the following XML code to your AndroidManifest.xml file within the element:

<code class="xml"><receiver android:name="com.blackboard.androidtest.receiver.ConnectionChangeReceiver"
          android:label="NetworkConnection">
  <intent-filter>
    <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
  </intent-filter>
</receiver></code>
Copy after login

3. Grant Permission:

Add the following permission to your AndroidManifest.xml file within the element:

<code class="xml"><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/></code>
Copy after login

4. Handle Network Connectivity Changes:

Within the onReceive method of your ConnectionChangeReceiver class, you can handle network connectivity changes. For example, you can check the active and mobile network info using:

<code class="java">ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService( Context.CONNECTIVITY_SERVICE );
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE );</code>
Copy after login

Example Code:

<code class="java">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();
    }
  }
}</code>
Copy after login

The above is the detailed content of How to Detect and Handle Network Connectivity Changes in Android?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!