Android: Ask user to either enable wifi or quit app

为君一笑 提交于 2020-02-22 07:42:46

问题


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?


回答1:


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..




回答2:


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");

        }


    }
}

}




回答3:


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



回答4:


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



来源:https://stackoverflow.com/questions/18525924/android-ask-user-to-either-enable-wifi-or-quit-app

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