ANDROID:如果 WiFi 已启用且处于活动状态,则启动一个意图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1811852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ANDROID: if WiFi is enabled AND active, launch an intent
提问by Hubert
This is what I would like to do :
这就是我想做的:
=> 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)
=> 如果 WiFi 已启用且处于活动状态,则启动一个意图(实际上它是一个获取其内容的 WebView => 我的应用程序在网络上的说明)
=> 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.commight be temporarily down or it may have moved ..."
=> 如果不是,那么我会启动另一个意图,这样我就不会显示带有“网页不可用的 WebView ... http://www.mywebsite.com上的网页可能暂时关闭,或者它可能有动了……”
I tought initially to use
我最初尝试使用
if(wifi.isWifiEnabled())
如果(wifi.isWifiEnabled())
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 连接是否处于活动状态。它仅表示用户已打开开关。该设备可能已连接,也可能未连接...这是正确的吗?
Then I tried to use :
然后我尝试使用:
if (wifi.getConnectionInfo().getSSID()!= null)
如果 (wifi.getConnectionInfo().getSSID()!= null)
but I noticed that it returns a string even if the connection has been lost or has been disabled ... ?
但我注意到,即使连接丢失或被禁用,它也会返回一个字符串......?
How should I do then ?
那我该怎么办?
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);
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 ...
在启动意图之前,是否有更通用的方法来测试设备是否可以连接到互联网?无论是通过Wifi,3G等...
Thanks in advance for your help.
在此先感谢您的帮助。
回答by Henrique Rocha
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();
}
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.
请确保您已在 Manifest 中注册了 android.net.conn.CONNECTIVITY_CHANGE 意图,否则,您将永远不会收到您在线的通知。
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.
过去几天我一直在努力解决这个问题,我现在才意识到我需要注册 CONNECTIVITY_CHANGE 而不仅仅是 WIFI_STATE_CHANGED。
回答by lencinhaus
Try android.net.ConnectivityManager.getActiveNetworkInfo()
: if it returns null
you have no connection; if it returns a NetworkInfo
object, you can check the connection's state with NetworkInfo.getState()
, and if it's NetworkInfo.State.CONNECTED
then you're connected, else you're not.
尝试android.net.ConnectivityManager.getActiveNetworkInfo()
:如果它返回null
你没有连接;如果它返回一个NetworkInfo
对象,您可以使用 来检查连接的状态NetworkInfo.getState()
,如果是,NetworkInfo.State.CONNECTED
则您已连接,否则您未连接。
回答by Donal Rafferty
You can do it as follows:
你可以这样做:
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action)){
Log.d("WIFI", "WIFI has changed");
int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, -1);
Log.d("WIFI", "WIFI State = " + wifiState);
setCurrentWifiState(wifiState);
}
You will get 0,1,2,3 depending on which state the Wifi is in, so for example 2 is connecting, you can check the rest in the documents
您将获得 0,1,2,3 取决于 Wifi 所处的状态,例如 2 正在连接,您可以在文档中查看其余部分
回答by razius
In your BroadcastReceiver class:
在您的 BroadcastReceiver 类中:
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION)){
boolean connected = intent.getBooleanExtra(WifiManager.EXTRA_SUPPLICANT_CONNECTED, false);
if (connected){
// start your service here
}
}
}
And in your AndroidManifest.xml make sure you register for the android.net.wifi.supplicant.CONNECTION_CHANGE broadcast intent.
并在您的 AndroidManifest.xml 中确保您注册了 android.net.wifi.supplicant.CONNECTION_CHANGE 广播意图。
<intent-filter >
<action android:name="android.net.wifi.supplicant.CONNECTION_CHANGE" />
</intent-filter>
回答by Agustin Gandara
isConnected()
doesnt work fully ok, research something else
isConnected()
不能完全正常工作,研究其他东西
final ConnectivityManager connMgr = (ConnectivityManager)
this.getSystemService(Context.CONNECTIVITY_SERVICE);
final android.net.NetworkInfo wifi =
connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
final android.net.NetworkInfo mobile =
connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if( wifi.isAvailable() && wifi.getDetailedState() == DetailedState.CONNECTED){
Toast.makeText(this, "Wifi" , Toast.LENGTH_LONG).show();
}
else if( mobile.isAvailable() && mobile.getDetailedState() == DetailedState.CONNECTED ){
Toast.makeText(this, "Mobile 3G " , Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(this, "No Network " , Toast.LENGTH_LONG).show();
}
this code check if you are with wifi or 3g or nothing , in the case of wifi on but not connected to a net or 3g have signal problem it detect this details, with DetailedStates
此代码检查您是否使用 wifi 或 3g 或什么都没有,如果 wifi 打开但未连接到网络或 3g 有信号问题,它会检测此详细信息, DetailedStates