要求用户连接到互联网或退出应用程序(android) [英] ask user to connect to internet or quit app (android)

查看:182
本文介绍了要求用户连接到互联网或退出应用程序(android)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个图像库应用程序,该应用程序正在从Internet检索图像。

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.

向用户显示WiFi和运营商网络选项。

show user both WiFi and Carrier network option.

推荐答案

这将同时检查wifi和移动数据。.在启动时或在您的主要活动中运行tis代码以检查网络连接。如果网络未连接,则弹出对话框并完成活动。就这么简单

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

这篇关于要求用户连接到互联网或退出应用程序(android)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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