安卓:询问用户启用WiFi或退出应用程序 [英] Android: Ask user to either enable wifi or quit app

查看:140
本文介绍了安卓:询问用户启用WiFi或退出应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个简单的应用程序需要网络连接的功能。当应用程序启动时,如果没有Wi-Fi连接,我想有两个选项来显示一个模式对话框的用户:
1)启用WiFi或2)退出应用程序。

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.

问题是,我无法找到一个Android方法可以被称为如果单击关闭程序按钮来关闭应用程序。

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?

推荐答案

要检查 Internet连接使用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;
             }
}

和为退出应用程序上的按钮点击使用此code

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

试试吧..

这篇关于安卓:询问用户启用WiFi或退出应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆