安卓wifi热点编程代码的若干注释

现附上一段代码,这段代码参考了很多人的并且进行了自己的补充和注释

public class MainActivity extends Activity {

	private TextView netWork;
	private TextView wifi;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		netWork=(TextView)this.findViewById(R.id.netWorkAvaliable);
		wifi=(TextView)this.findViewById(R.id.wifiAvaliable);
		
			testConnectivityManager();
		
	}
	
	public void testConnectivityManager() {  
	    Context context = this.getApplicationContext();//获取应用上下文  
	  //  ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);//获取系统的连接服务  
	    //NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();//获取网络的连接情况  
	    WifiManager wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
	    if (wifiManager.isWifiEnabled()==true)
	    {
	    	wifi.setText("wifi closing");
	    	toggleWiFi(false);
	    	
	    }
	    stratWifiAp(wifiManager);
	    //if(activeNetInfo.getType()==ConnectivityManager.TYPE_WIFI){  
	    //判断WIFI网  
	    //	wifi.setText("wifi setting");
	    //	toggleWiFi(false);
	   // }
	    
		    
	}  
	
	public void toggleWiFi(boolean status) {
        WifiManager wifiManager = (WifiManager) this
                .getSystemService(Context.WIFI_SERVICE);
        if (status == true && !wifiManager.isWifiEnabled()) {
            wifiManager.setWifiEnabled(true);
        } else if (status == false && wifiManager.isWifiEnabled()) {
            wifiManager.setWifiEnabled(false);
        }
    }
	
	
	
	public void stratWifiAp(WifiManager wifiManager) { 
        Method method1 = null;  
        try {  
        	method1 = wifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class); 
            WifiConfiguration netConfig = new WifiConfiguration();  
            //wifi热点名字  
            netConfig.SSID = "Hehyu";  
            netConfig.allowedAuthAlgorithms  
                    .set(WifiConfiguration.AuthAlgorithm.OPEN);  
            netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);  
            netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);  
            netConfig.allowedKeyManagement  
                    .set(WifiConfiguration.KeyMgmt.WPA_PSK);  
            netConfig.allowedPairwiseCiphers  
                    .set(WifiConfiguration.PairwiseCipher.CCMP);  
            netConfig.allowedPairwiseCiphers  
                    .set(WifiConfiguration.PairwiseCipher.TKIP);  
            netConfig.allowedGroupCiphers  
                    .set(WifiConfiguration.GroupCipher.CCMP);  
            netConfig.allowedGroupCiphers  
                    .set(WifiConfiguration.GroupCipher.TKIP);  
            //密码  
            netConfig.preSharedKey = "19921214";  
  
            method1.invoke(wifiManager, netConfig, true);  
        } catch (IllegalArgumentException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        } catch (IllegalAccessException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        } catch (InvocationTargetException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        } catch (SecurityException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        } catch (NoSuchMethodException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
  
    }  


}

因为时间比较晚了,明天再进行代码的补充和注释吧

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值