安卓:如果无线网络启用和有效,发动意图 [英] ANDROID: if WiFi is enabled AND active, launch an intent

查看:109
本文介绍了安卓:如果无线网络启用和有效,发动意图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我想要做的:

=>如果无线网络启用和有效,发动意图(实际上它是一个web视图,获取其内容=>我的应用程序的网站上的说明)

=> IF WiFi is enabled AND active, launch an intent (in fact it's a WebView that gets its content=>the instructions of my app on the web)

=>如果没有,那么我会启动另一个意图,这样我就不会显示一个的WebView不可用的网页...在的 http://www.mywebsite.com 可能暂时无法连接,或者它可能已移动......

=> IF NOT, then I would launch another intent so that I don't show a WebView with "Web page not available ... The Web page at http://www.mywebsite.com might be temporarily down or it may have moved ..."

我因子评分开始使用

如果(wifi.isWifiEnabled())

if(wifi.isWifiEnabled())

但是,这并不说,如果WiFi连接处于活动状态。它说,只有用户已打开开关。该设备可能会或可能不会进行连接......这是正确的?

but that does not say if the Wifi connection is ACTIVE or not. It says only that the user has turned the switch on. The device may or may not be connected... Is this correct ?

然后我试图使用方法:

如果(wifi.getConnectionInfo()。getSSID()!= NULL)

if (wifi.getConnectionInfo().getSSID()!= null)

但我注意到,它返回即使连接已丢失或已被禁用的字符串...?

but I noticed that it returns a string even if the connection has been lost or has been disabled ... ?

我应该怎么办呢?

wifi = (WifiManager)getSystemService(Context.WIFI_SERVICE);
Intent intent_instructions;

        	if (wifi.getConnectionInfo().getSSID()!= null){
        		Log.i("Hub", "WiFi is enabled AND active !");
        		Log.i("Hub", "SSID = "+wifi.getConnectionInfo().getSSID());
        		intent_instructions = new Intent(this, Instructions.class);
    		}else{
    			Log.i("Hub", "NO WiFi");
    			intent_instructions = new Intent(this, Instructions_No_WiFi.class);
    		}
    		this.startActivity(intent_instructions);

有没有来测试设备是否有连接到互联网刚刚推出的意图之前,更普遍的方式是什么?无论是通过WIFI,3G,等...

Is there a more general way to test if the device has the connectivity to the internet just before launching an intent ? be it through Wifi, 3G, etc ...

在此先感谢您的帮助。

推荐答案

您可以使用下面的code,检查连接:

You can use the following code to check for connectivity:

private static boolean isConnected(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager)
        context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = null;
    if (connectivityManager != null) {
        networkInfo =
            connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    }
    return networkInfo == null ? false : networkInfo.isConnected();
}

请确保您已注册的android.net.conn.CONNECTIVITY_CHANGE意图在你的清单,否则,你将永远不会收到您在网上的通知。

Please make sure that you've registered the android.net.conn.CONNECTIVITY_CHANGE intent in your Manifest, or else, you'll never receive a notification that you're online.

我一直在努力奋斗着这个问题在过去的几天,我只是现在意识到我需要注册CONNECTIVITY_CHANGE不仅WIFI_STATE_CHANGED。

I've been struggling with this issue for the last couple of days and I just now realized that I needed to register CONNECTIVITY_CHANGE and not only WIFI_STATE_CHANGED.

这篇关于安卓:如果无线网络启用和有效,发动意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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