-1

I am designing a gaming app, that requires the users to be on a good wifi connection. And i also need both users to know if the other user is on wifi or not. Android platform will be used for this gaming app development.

Lefteris
  • 14,550
  • 2
  • 56
  • 95
Deepu
  • 1
  • 1
  • possible duplicate of [Detect network connection type on Android](http://stackoverflow.com/questions/2802472/detect-network-connection-type-on-android) – Alan B Nov 03 '14 at 08:00

1 Answers1

0

Just create the following class which checks for an internet connection:

public class ConnectionStatus {

    private Context _context;

    public ConnectionStatus(Context context) {
        this._context = context;
    }

    public boolean isConnectionAvailable() {
        ConnectivityManager connectivity = (ConnectivityManager) _context
                .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;
    }
}

This class simply contains a method which returns the boolean value of the connection status. Therefore in simple terms, if the method finds a valid connection to the Internet, the return value is true, otherwise false if no valid connection is found.

The following method in the MainActivity then calls the result from the method previously described, and prompts the user to act accordingly:

public void addListenerOnWifiButton() {
        Button btnWifi = (Button)findViewById(R.id.btnWifi);

        iia = new ConnectionStatus(getApplicationContext());

        isConnected = iia.isConnectionAvailable();
        if (!isConnected) {
            btnWifi.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
                    Toast.makeText(getBaseContext(), "Please connect to a hotspot",
                            Toast.LENGTH_SHORT).show();
                }
            });
        }
        else {
            btnWifi.setVisibility(4);
            warning.setText("This app may use your mobile data to update events and get their details.");
        }
    }

In the above code, if the result is false, (therefore there is no internet connection, the user is taken to the Android wi-fi panel, where he is prompted to connect to a wi-fi hotspot.

Michele La Ferla
  • 6,775
  • 11
  • 53
  • 79
  • "And i also need both users to know if the other user is on wifi or not" .. How does one user know if the other is on wifi or not ? – Deepu Nov 03 '14 at 09:26
  • Not sure if you can get that easily. Came through some posts on SO which could help you like this: http://stackoverflow.com/a/7599847/1116216. But this only gives you the SSID (Wifi network name) for the network your device is connected to, and not the one of the peer you are palying against. – Michele La Ferla Nov 03 '14 at 14:52