當前位置: 首頁>>代碼示例>>Java>>正文


Java WifiManager.setWifiEnabled方法代碼示例

本文整理匯總了Java中android.net.wifi.WifiManager.setWifiEnabled方法的典型用法代碼示例。如果您正苦於以下問題:Java WifiManager.setWifiEnabled方法的具體用法?Java WifiManager.setWifiEnabled怎麽用?Java WifiManager.setWifiEnabled使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在android.net.wifi.WifiManager的用法示例。


在下文中一共展示了WifiManager.setWifiEnabled方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: setWifiEnabled

import android.net.wifi.WifiManager; //導入方法依賴的package包/類
/**
 * 打開或關閉wifi
 * <p>需添加權限 {@code <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>}</p>
 *
 * @param enabled {@code true}: 打開<br>{@code false}: 關閉
 */
public static void setWifiEnabled(boolean enabled) {
    @SuppressLint("WifiManagerLeak")
    WifiManager wifiManager = (WifiManager) Utils.getContext().getSystemService(Context.WIFI_SERVICE);
    if (enabled) {
        if (!wifiManager.isWifiEnabled()) {
            wifiManager.setWifiEnabled(true);
        }
    } else {
        if (wifiManager.isWifiEnabled()) {
            wifiManager.setWifiEnabled(false);
        }
    }
}
 
開發者ID:hoangkien0705,項目名稱:Android-UtilCode,代碼行數:20,代碼來源:NetworkUtils.java

示例2: turnOnWifi

import android.net.wifi.WifiManager; //導入方法依賴的package包/類
static void turnOnWifi(Context context) {
    WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);

    if (wifiManager == null || isConnectedToWifi(context)) {
        context.startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
        return;
    }

    wifiManager.setWifiEnabled(true);
}
 
開發者ID:appwise-labs,項目名稱:NoInternetDialog,代碼行數:11,代碼來源:NoInternetUtils.java

示例3: setWifiEnabled

import android.net.wifi.WifiManager; //導入方法依賴的package包/類
/**
 * 打開或關閉wifi
 * <p>需添加權限 {@code <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>}</p>
 *
 * @param enabled {@code true}: 打開<br>{@code false}: 關閉
 */
public static void setWifiEnabled(boolean enabled) {
    WifiManager wifiManager = (WifiManager) ToolCache.getContext().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    if (enabled) {
        if (!wifiManager.isWifiEnabled()) {
            wifiManager.setWifiEnabled(true);
        }
    } else {
        if (wifiManager.isWifiEnabled()) {
            wifiManager.setWifiEnabled(false);
        }
    }
}
 
開發者ID:StickyTolt,項目名稱:ForeverLibrary,代碼行數:19,代碼來源:NetworkUtils.java

示例4: doInBackground

import android.net.wifi.WifiManager; //導入方法依賴的package包/類
@Override
protected Void doInBackground(Void... params) {

    WifiConfiguration conf = new WifiConfiguration();
    conf.SSID = "\"" + networkSSID + "\"";
    conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);

    WifiManager wifiManager = (WifiManager) getBaseContext().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    wifiManager.addNetwork(conf);
    if (!wifiManager.isWifiEnabled()) {
        wifiManager.setWifiEnabled(true);
        wifiManager.startScan();
    }

    int netId = wifiManager.addNetwork(conf);
    wifiManager.disconnect();
    wifiManager.enableNetwork(netId, true);
    wifiManager.reconnect();

    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(getApplicationContext(), "Joined to " + networkSSID, Toast.LENGTH_SHORT).show();
            System.out.println("SUCCESS ");
        }
    });
    return null;
}
 
開發者ID:yuvaraj119,項目名稱:WifiChatSharing,代碼行數:29,代碼來源:MainActivity.java

示例5: setWifiEnabled

import android.net.wifi.WifiManager; //導入方法依賴的package包/類
/**
 * 打開或關閉wifi
 * <p>需添加權限 {@code <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>}</p>
 *
 * @param enabled {@code true}: 打開<br>{@code false}: 關閉
 */
@SuppressLint("WifiManagerLeak")
public static void setWifiEnabled(boolean enabled) {
    WifiManager wifiManager = (WifiManager) Utils.getContext().getSystemService(Context.WIFI_SERVICE);
    if (enabled) {
        if (!wifiManager.isWifiEnabled()) {
            wifiManager.setWifiEnabled(true);
        }
    } else {
        if (wifiManager.isWifiEnabled()) {
            wifiManager.setWifiEnabled(false);
        }
    }
}
 
開發者ID:angcyo,項目名稱:RLibrary,代碼行數:20,代碼來源:NetworkUtils.java

示例6: connectToAP

import android.net.wifi.WifiManager; //導入方法依賴的package包/類
public int connectToAP(Context context, String networkSSID, String networkPasskey) {
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    for (ScanResult result : wifiManager.getScanResults()) {
        if (result.SSID.equals(networkSSID)) {
            String securityMode = getScanResultSecurity(result);
            WifiConfiguration wifiConfiguration = createAPConfiguration(networkSSID, networkPasskey, securityMode);
            int res = wifiManager.addNetwork(wifiConfiguration);
            Log.d(TAG, "# addNetwork returned " + res);
            boolean b = wifiManager.enableNetwork(res, true);
            Log.d(TAG, "# enableNetwork returned " + b);
            wifiManager.setWifiEnabled(true);
            boolean changeHappen = wifiManager.saveConfiguration();
            if (res != -1 && changeHappen) {
                Log.d(TAG, "# Change happen: " + networkSSID);
            } else {
                Log.d(TAG, "# Change NOT happen");
            }
            return res;
        }
    }
    return -1;
}
 
開發者ID:dmitrykolesnikovich,項目名稱:featurea,代碼行數:23,代碼來源:WifiUtil.java

示例7: setWifiEnabled

import android.net.wifi.WifiManager; //導入方法依賴的package包/類
/**
 * 打開或關閉wifi
 * <p>需添加權限 {@code <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>}</p>
 *
 * @param enabled {@code true}: 打開<br>{@code false}: 關閉
 */
public static void setWifiEnabled(boolean enabled) {
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    if (enabled) {
        if (!wifiManager.isWifiEnabled()) {
            wifiManager.setWifiEnabled(true);
        }
    } else {
        if (wifiManager.isWifiEnabled()) {
            wifiManager.setWifiEnabled(false);
        }
    }
}
 
開發者ID:youth5201314,項目名稱:XFrame,代碼行數:19,代碼來源:XNetworkUtils.java

示例8: startWifi

import android.net.wifi.WifiManager; //導入方法依賴的package包/類
void startWifi()
{

    WifiManager wifiManager = (WifiManager) this
            .getSystemService(Context.WIFI_SERVICE);
    if(!wifiManager.isWifiEnabled())
        wifiManager.setWifiEnabled(true);

}
 
開發者ID:SkylineLabs,項目名稱:FindX,代碼行數:10,代碼來源:digiPune.java

示例9: setWifiEnabled

import android.net.wifi.WifiManager; //導入方法依賴的package包/類
/**
 * 打開或關閉wifi
 * <p>需添加權限 {@code <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>}</p>
 *
 * @param enabled {@code true}: 打開<br>{@code false}: 關閉
 */
public static void setWifiEnabled(boolean enabled) {
    WifiManager wifiManager = (WifiManager) MyUtils.getContext().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    if (enabled) {
        if (wifiManager != null && !wifiManager.isWifiEnabled()) {
            wifiManager.setWifiEnabled(true);
        }
    } else {
        if (wifiManager != null && wifiManager.isWifiEnabled()) {
            wifiManager.setWifiEnabled(false);
        }
    }
}
 
開發者ID:ChunweiDu,項目名稱:Utils,代碼行數:19,代碼來源:NetworkUtil.java

示例10: setWifiEnabled

import android.net.wifi.WifiManager; //導入方法依賴的package包/類
/**
 * 打開或關閉wifi
 * <p>需添加權限 {@code <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>}</p>
 *
 * @param context 上下文
 * @param enabled {@code true}: 打開<br>{@code false}: 關閉
 */
@SuppressWarnings("MissingPermission")
public static void setWifiEnabled(Context context, boolean enabled) {
    WifiManager wifiManager = (WifiManager) context
            .getSystemService(Context.WIFI_SERVICE);
    if (enabled) {
        if (!wifiManager.isWifiEnabled()) {
            wifiManager.setWifiEnabled(true);
        }
    } else {
        if (wifiManager.isWifiEnabled()) {
            wifiManager.setWifiEnabled(false);
        }
    }
}
 
開發者ID:imliujun,項目名稱:LJFramework,代碼行數:22,代碼來源:NetworkUtils.java

示例11: getSelfIp

import android.net.wifi.WifiManager; //導入方法依賴的package包/類
public String getSelfIp(Context context) {
    WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    if (wifiManager == null) {
        return "Error";
    }
    if (!wifiManager.isWifiEnabled()) {
        wifiManager.setWifiEnabled(true);
    }
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    int ipAddress = wifiInfo.getIpAddress();
    return intToIp(ipAddress);
}
 
開發者ID:neocross,項目名稱:NeoSocket,代碼行數:13,代碼來源:NeoSocketServer.java

示例12: setWifiEnabled

import android.net.wifi.WifiManager; //導入方法依賴的package包/類
/**
 * 打開或關閉wifi
 * <p>需添加權限 {@code <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>}</p>
 *
 * @param enabled {@code true}: 打開<br>{@code false}: 關閉
 */
public static void setWifiEnabled(boolean enabled) {
    @SuppressLint("WifiManagerLeak")
    WifiManager wifiManager = (WifiManager) AvenueNet.getAppContext().getSystemService(Context.WIFI_SERVICE);
    if (enabled) {
        if (!wifiManager.isWifiEnabled()) {
            wifiManager.setWifiEnabled(true);
        }
    } else {
        if (wifiManager.isWifiEnabled()) {
            wifiManager.setWifiEnabled(false);
        }
    }
}
 
開發者ID:QuincySx,項目名稱:AvenueNet,代碼行數:20,代碼來源:NetworkUtils.java

示例13: setWifiEnabled

import android.net.wifi.WifiManager; //導入方法依賴的package包/類
/**
 * 打開或關閉wifi
 * <p>需添加權限 {@code <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>}</p>
 *
 * @param enabled {@code true}: 打開<br>{@code false}: 關閉
 */
public static void setWifiEnabled(boolean enabled) {
    WifiManager wifiManager = (WifiManager) Utils.getContext().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    if (enabled) {
        if (!wifiManager.isWifiEnabled()) {
            wifiManager.setWifiEnabled(true);
        }
    } else {
        if (wifiManager.isWifiEnabled()) {
            wifiManager.setWifiEnabled(false);
        }
    }
}
 
開發者ID:guiying712,項目名稱:AndroidModulePattern,代碼行數:19,代碼來源:NetworkUtils.java

示例14: scanWifi

import android.net.wifi.WifiManager; //導入方法依賴的package包/類
public List<ScanResult> scanWifi(){
    lstWifi.clear();
    final WifiManager wifi = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
    if (wifi.isWifiEnabled() == false)
    {
        wifi.setWifiEnabled(true);
    }

    mContext.registerReceiver(new BroadcastReceiver()
    {
        @Override
        public void onReceive(Context c, Intent intent)
        {
            lstWifi = wifi.getScanResults();;
        }
    }, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
    wifi.startScan();

    int i = 0;
    while(i < 10){
        i++;
        try {
            Thread.sleep(1000);
        }catch (Exception e){

        }
    }
    return lstWifi;
}
 
開發者ID:dmtan90,項目名稱:Sense-Hub-Android-Things,代碼行數:30,代碼來源:WifiSta.java

示例15: onRequestPermissionsResult

import android.net.wifi.WifiManager; //導入方法依賴的package包/類
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    if (requestCode == 0x12345) {
        for (int grantResult : grantResults) {
            if (grantResult != PackageManager.PERMISSION_GRANTED) {
                return;
            }
        }
        WifiManager myWifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        boolean wasEnabled = myWifiManager.isWifiEnabled();
        if (!wasEnabled)
            myWifiManager.setWifiEnabled(true);
        if (myWifiManager.isWifiEnabled()) {
            if (myWifiManager.startScan()) {
                // List available APs
                Log.d("pras", "this inside scan");
                List<ScanResult> scans = myWifiManager.getScanResults();
                Log.d("pras", "" + (scans == null));
                Log.d("pras", "" + scans.isEmpty());
                if (scans != null && !scans.isEmpty()) {
                    for (ScanResult scan : scans) {
                        int level = WifiManager.calculateSignalLevel(scan.level, 20);
                        //Other code
                        Log.d("wifi", level + "this");
                    }
                } else {
                    Log.d("pras", "this inside else");
                }
            }
        }

    }

}
 
開發者ID:PrasannaNatarajan,項目名稱:Interrobang,代碼行數:35,代碼來源:MainActivity.java


注:本文中的android.net.wifi.WifiManager.setWifiEnabled方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。