Wednesday, 21 February 2018

Notify When Network Data is Enable Or Disable

Hi friends, In this Post we'll learn how to get notify when Mobile Data or Wifi is Enable or Disable.
In this post we'll create a BroadcastReceiver to get notify is Data or wifi enable or disable. When the network state will change it will notify us.

Permissions Required

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

To create a BroadcastReceiver we have to create NetworkChangeReceiver.java class. In which we have to extends BroadcastReceiver class and Override it's method onReceive(). All the work will be done in this method.

Creating Receiver 

Open NetworkChangeReceiver.java and do the following changes
package com.minixithub.notifymobiledatatutorial;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.widget.Toast;

/** * Created by Vishnu Kumar Soni on 22/01/2018. */
public class NetworkChangeReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(final Context context, final Intent intent) {

        Handler handler =new Handler(Looper.getMainLooper());
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                ConnectivityManager connMgr = (ConnectivityManager) 
                    context.getSystemService(Context.CONNECTIVITY_SERVICE);

                android.net.NetworkInfo wifi =
connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

                android.net.NetworkInfo mobile = 
connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
                if (wifi.isConnected() || mobile.isConnected()) {
                    // Do something
                    Toast.makeText(context,"Network Data is Enable",
Toast.LENGTH_LONG).show();
                    Log.d("Network Available ", "Network Data Enable");
                }else
                    Toast.makeText(context,"Network Data is Disable",
Toast.LENGTH_LONG).show();
            }
        },10000);
    }
}

Here we used Handler Object and display a Toast after 10 sec of broadcast receive. Because sometimes it takes time to Enable Network Data or Wifi after pressing button of Network data or Wifi.

Declaring Receiver in Manifest

Do the following changes in manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.minixithub.notifymobiledatatutorial">
    
    <!-- Adding Permission -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
<!-- Notify When Mobile Data or Wifi state change -->
        <receiver android:name=".NetworkChangeReceiver">
            <intent-filter>
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
                <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
            </intent-filter>
        </receiver>
</application>

</manifest>


No comments:

Post a Comment