-1

I want to check if wifi is connected or not when the button is clicked as well as show a pop up message so i have written a separate class for it :

public class Pop extends Activity {

@Override

protected void onCreate(Bundle savedInstanceSate) {

    super.onCreate(savedInstanceSate);

    setContentView(R.layout.popup);

    DisplayMetrics dm = new DisplayMetrics() ;
    getWindowManager().getDefaultDisplay().getMetrics(dm);

    int width = dm.widthPixels ;
    int height = dm.heightPixels ;

    getWindow().setLayout( (int)(width*.6),(int)(height*.4) ) ;

}

So what should be the method for checking if wifi is connected and it keeps on checking after every 5 seconds even if the app is closed ?

2 Answers2

0

You can check if you have internet access by calling this method:

private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager =
            (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();

    boolean isAvailable = false;
    if (networkInfo != null && networkInfo.isConnected()) {
        isAvailable = true;
    }
    return isAvailable;
}

Remember to ask for the required permissions as well:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Charles Li
  • 1,835
  • 3
  • 24
  • 40
0

Try this

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

in your MainActivity.java

ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

if (mWifi.isConnected()) {
        Intent networkInfoServiceIntent = new Intent(MainActivity.this,NetworkInfoService.class);
        startService(networkInfoServiceIntent);
}

Create a service name as NetworkInfoService and register in manifest

Now implement NetworkInfoService as,

    /**
     * Function for check network connectivity after every 5 seconds
     */
    private void checkNetworkConnectivity() {
        // handler will run after 5 seconds
        new Handler().postDelayed(new Runnable() {
                                      @Override
                                      public void run() {
                                         ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
                                         NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
                                          if (mWifi.isConnected()){
                                              checkNetworkConnectivity();
                                          }else {
                                              // display alert (notification)
                                          }

                                      }
                                  },5000
        );
    }
Pramod Waghmare
  • 1,273
  • 13
  • 21