And so it is done.
This class checks for the correct 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; } }
The following method opens the wi-fi panel if there is no valid Internet connection:
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."); } }
The following method opens the 3G panel if there is no valid Internet connection:
public void addListenerOn3GButton() { Button btnThreeGee = (Button)findViewById(R.id.btn3G); iia = new ConnectionStatus(getApplicationContext()); isConnected = iia.isConnectionAvailable(); if (!isConnected) { btnThreeGee.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(Settings.ACTION_DATA_ROAMING_SETTINGS)); Toast.makeText(getBaseContext(), "Please check 'Data enabled' option", Toast.LENGTH_SHORT).show(); } }); } else { btnThreeGee.setVisibility(4); cont.setVisibility(View.VISIBLE); warning.setText("This app may use your mobile data to update events and get their details."); } }
Hope this helps :)
source share