ask user to connect to internet or quit app (andro

2020-07-10 06:27发布

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.

5条回答
我命由我不由天
2楼-- · 2020-07-10 06:45

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;
}
查看更多
▲ chillily
3楼-- · 2020-07-10 06:48

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楼-- · 2020-07-10 06:56

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

查看更多
聊天终结者
5楼-- · 2020-07-10 07:00

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();
}
查看更多
叛逆
6楼-- · 2020-07-10 07:02
//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;
    }
查看更多
登录 后发表回答