ask user to connect to internet or quit app (android)

被刻印的时光 ゝ 提交于 2019-12-21 12:15:22

问题


i am working on an image gallery app, in which application is retrieving images from internet.

so i want to prompt a dialog-box to ask user to connect to internet or quit application.

show user both WiFi and Carrier network option.


回答1:


this checks for both wifi and mobile data..run tis code on splash or your main activity to check network connection.popup the dialog if the net is not connected and finish the activity.It's that 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;
}



回答2:


Try this:

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();
}



回答3:


First you should check if they are already connected or not (tons of examples how to do this online)

If not, then 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();



回答4:


//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;
    }



回答5:


This is the way it is done.

This class checks for a valid 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 :)



来源:https://stackoverflow.com/questions/25685755/ask-user-to-connect-to-internet-or-quit-app-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!