1

I used ConnectivityManager.NetworkCallback to take the connected wifi network SSID for the android devices. I need to implement this application for Android 9.0 and above. Hence I not used the Broadcast Receivers (because they are deprecated to the Android 9.0). Instead of I used ConnectivityManager.NetworkCallback. as follows.

    package com.example.myapplication;

    import android.content.Context;
    import android.net.ConnectivityManager;
    import android.net.Network;
    import android.net.NetworkRequest;
    import android.net.wifi.WifiConfiguration;
    import android.net.wifi.WifiInfo;
    import android.net.wifi.WifiManager;
    import android.os.Build;
    import android.os.Bundle;
    import android.widget.Toast;

    import androidx.annotation.RequiresApi;
    import androidx.appcompat.app.AppCompatActivity;

    import java.util.List;

    public class MainActivity extends AppCompatActivity {
        ConnectivityManager connectivityManager;
        ConnectivityManager.NetworkCallback networkCallback;

        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkRequest networkRequest = new NetworkRequest.Builder().build();

            networkCallback = new ConnectivityManager.NetworkCallback(){
                @Override
                public void onAvailable(Network network) {

                    WifiManager wifiManager;
                    String connectedSsid = null;
                    wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
                    WifiInfo info = wifiManager.getConnectionInfo();
                    connectedSsid = info.getSSID();
                    if (connectedSsid != null) {
                        Toast.makeText(getApplicationContext(), "*******" + connectedSsid + "**********", Toast.LENGTH_LONG).show();
                    }
                }

                @Override
                public void onLost(Network network){
                    Toast.makeText(getApplicationContext(), "*******DIS!! connected**********", Toast.LENGTH_LONG).show();
                }
            };
            connectivityManager.registerNetworkCallback(networkRequest,networkCallback);
        }

        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        @Override
        public void onStop(){
            super.onStop();
            connectivityManager.unregisterNetworkCallback(networkCallback);
        }
    }

I have added the required permissions in the AndroidManifest as follow,

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

I registered this inside the onCreate method as,

connectivityManager.registerNetworkCallback(networkRequest,networkCallback);

and unregistered inside the onStop method as,

connectivityManager.unregisterNetworkCallback(networkCallback);

This is working when I on and off the wifi connection. But If I change the connection from one network to other networks, These Toasts are not working. So what should I do for that?

Hilton
  • 337
  • 6
  • 19

1 Answers1

0

Use this code,maybe it will help you.

 public boolean isConnectingToInternet() {
    ConnectivityManager connectivity = (ConnectivityManager) MainActivity.this.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity != null) {
        NetworkInfo[] info = connectivity.getAllNetworkInfo();
        if (info != null)
            for (int i = 0; i < info.length; i++)
                if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                    return true;
                }

    }
    return false;
}

Add write this code in OnCreate()

 if (!isConnectingToInternet()) {
        LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
        View view1 = inflater.inflate(R.layout.internet_error, null);
        final AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this)
                .setView(view1).create();
        Button cancel = view1.findViewById(R.id.btn_cancel);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
            }
        });
        Toast.makeText(this, "Not Connected To Internet..", Toast.LENGTH_SHORT).show();
        alertDialog.show();
    }
Manish Stark
  • 110
  • 6