簡體   English   中英

Android 4.0 - 檢查與WiFi的連接

[英]Android 4.0 - check connection to WiFi

我使用下面的功能來檢查WiFi熱點點的連接:

public boolean IsWiFiConnected(){
 List<WifiConfiguration> wifiConfigList = wifiManager.getConfiguredNetworks();
 boolean retVal=false;
    for(WifiConfiguration wifiConf : wifiConfigList){               
        if(wifiConf.status==WifiConfiguration.Status.CURRENT){
            retVal=true;
            break;
        }
    }
 return retVal;
}

在android 4.0中,它總是返回false。 它在以前的版本上運行良好。 謝謝

有一種比你更快更可靠的方法。

public boolean IsWiFiConnected() {
    ConnectivityManager manager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    return netInfo.isConnected();
}

只是為了擴展gabriel的答案,因為可能值得檢查用戶是否有任何數據連接,即; WiFi或數據。 這還將顯示一個對話框,詢問用戶是否要使用Intent打開WiFi。 希望這可以幫助你!

    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;

    }
    if (haveConnectedWifi == false && haveConnectedMobile == false) {
        Log.d("Network State", "false");
        AlertDialog.Builder alert = new AlertDialog.Builder(YourActivity.this);                 
        alert.setTitle("No Data Connection!");  
        alert.setMessage("You have no data connection.");   
            alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int whichButton) {
                    // TODO Auto-generated method stub
                    final Intent intent = new Intent(Intent.ACTION_MAIN, null);
                    intent.addCategory(Intent.CATEGORY_LAUNCHER);
                    final ComponentName cn = new ComponentName("com.android.settings", "com.android.settings.wifi.WifiSettings");
                    intent.setComponent(cn);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity( intent);
                }

            });
            alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    return;   
                }
            });
            alert.show();

    }else{
        //do something else
    }
    return haveConnectedWifi || haveConnectedMobile;
}

檢查wifi狀態:

ConnectivityManager conMgr;
NetworkInfo netInfo;
WifiManager wifiMgr;

conMgr=(ConnectivityManager)getSystemService(context.WIFI_Service);
netInfo=conMgr.getActiveNetworkInfo();
if(!(netInfo==null))
{
if(WifiMgr.isWifiEnabled())
{
//wifi  enabled
}
else
{
//wifi disabled i.e not available
}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM