Android: Ask user to either enable wifi or quit app

2k views Asked by At

I'm developing a simple app that requires network connectivity to function. When the app starts, and if there is no wi-fi connection, I want to display a modal dialog to the user with two options: 1) Enable wifi or 2) Quit the application.

The problem is that I can't find an android method that can be called to close the application if the 'close app' button is clicked.

Is there such a method that I haven't found yet? Or is there some better way to handle this entire use case?

4

There are 4 answers

0
Karl with K On

You should think about the Quit Opinion,

here is an good Answer about Quiting an App SO Answer

You can say that no WiFi is present.

And if you just make an AlerDialog like this:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("Hint"
                        .setMessage("No WiFi!"
                        .setPositiveButton("OK", null).show();

Or a Toast like:

Toast.makeText(yourActivity.this, "No WiFi!", Toast.LENGTH_LONG).show();

If you whant to check for an connection, you can use this snippet:

public boolean isOnline() {
        // check if an internet connection is present
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (!(netInfo != null && cm.getActiveNetworkInfo().isAvailable() && cm
                .getActiveNetworkInfo().isConnected())) {
            return false;
        }
        return true;
    }
1
Ritaban On

I had a simillar code for checking wifi speed . Please see whether this helps or not :

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    TextView text= (TextView) findViewById(R.id.TV);

    ConnectivityManager connectivityManager =(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = connectivityManager.getActiveNetworkInfo();

    if (info == null ||
            !connectivityManager.getBackgroundDataSetting()) {
        // No Network detected
        findViewById(R.id.quit).setVisibility(View.VISIBLE);
        findViewById(R.id.quit).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                finish();
            }
        });
        //return;
    }else {
        int netType = info.getType();
        int netSubtype = info.getSubtype();
        if (netType == ConnectivityManager.TYPE_WIFI) {
            //WIFI DETECTED
            WifiManager wifiManager = (WifiManager)this.getSystemService(Context.WIFI_SERVICE);
            WifiInfo wifiInfo = wifiManager.getConnectionInfo();

            int linkSpeed = wifiInfo.getLinkSpeed(); //measured using WifiInfo.LINK_SPEED_UNITS
            text.setText("Connection is wifi and the speed is "+Integer.toString(linkSpeed));


        } else if (netType == ConnectivityManager.TYPE_MOBILE
                && netSubtype >2) {
            text.setText("connection is 3g");  
        } else {
            text.setText("connection is 2g");

        }


    }
}

}

0
Amresh On

To check the Internet Connection use this code:

private boolean haveNetworkConnection() {
          final ConnectivityManager conMgr = (ConnectivityManager) getSystemService (Context.CONNECTIVITY_SERVICE);
             if (conMgr.getActiveNetworkInfo() != null && conMgr.getActiveNetworkInfo().isAvailable() &&    conMgr.getActiveNetworkInfo().isConnected()) {
                   return true;
             } else {
                   System.out.println("Internet Connection Not Present");
                 return false;
             }
}

And to Quit Application on Button click use this code :

        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        startActivity(intent);

Try it..

0
ahmed_khan_89 On

I understood that your problem is closing the application, right ? Did you try finish(); ?