Ask the user to connect to the Internet or close the application (Android)

I am working on an image gallery application in which the application retrieves images from the Internet.

therefore, I want to offer a dialog box to ask the user to connect to the Internet or exit the application.

show the user both WiFi and the Carrier network option.

+5
source share
5 answers

it checks both Wi-Fi and mobile data. Use this code for a burst or basic action to test your network connection. If you are not connected to the network, complete the operation. It's simple

private boolean haveNetworkConnection() { boolean haveConnectedWifi = false; boolean haveConnectedMobile = false; ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo[] netInfo = cm.getAllNetworkInfo(); for (NetworkInfo ni : netInfo) { if (ni.getTypeName().equalsIgnoreCase("WIFI")) if (ni.isConnected()) haveConnectedWifi = true; if (ni.getTypeName().equalsIgnoreCase("MOBILE")) if (ni.isConnected()) haveConnectedMobile = true; } return haveConnectedWifi || haveConnectedMobile; } 
+5
source

Try the following:

 public static boolean isConnected(Context context){ ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo wifiInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); NetworkInfo mobileInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); if ((wifiInfo != null && wifiInfo.isConnected()) || (mobileInfo != null && mobileInfo.isConnected())) { return true; }else{ showDialog(); return false; } private void showDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage("Connect to wifi or quit") .setCancelable(false) .setPositiveButton("Connect to WIFI", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS)); } }) .setNegativeButton("Quit", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { this.finish(); } }); AlertDialog alert = builder.create(); alert.show(); } 
+4
source

You must first check if they are already connected or not (tons of examples on how to do this online)

If not, use this code

 AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage("Connect to wifi or quit") .setCancelable(false) .setPositiveButton("Connect to WIFI", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS)); } }) .setNegativeButton("Quit", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { this.finish(); } }); AlertDialog alert = builder.create(); alert.show(); 
+2
source
 //Checking For Internet Connection ConnectionDetector cd = new ConnectionDetector(getApplicationContext()); // Check if Internet present if (!cd.isConnectingToInternet()) { // Internet Connection is not present AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage("Connect to wifi or quit") .setCancelable(false) .setPositiveButton("Connect to WIFI", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS)); } }) .setNegativeButton("Quit", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { this.finish(); } }); AlertDialog alert = builder.create(); alert.show(); return; } 
+1
source

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 :)

0
source

Source: https://habr.com/ru/post/1201897/


All Articles