当前位置:首页 > 开发 > 编程语言 > 编程 > 正文

android wifi打开

发表于: 2014-06-16   作者:baoyao   来源:转载   浏览:
摘要: MainActivity.java package com.wifi; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.content.Context; import android.net.wifi.WifiConfigurat
MainActivity.java
package com.wifi;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.Menu;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

public class MainActivity extends Activity {

	private List<WifiConfiguration> list=new ArrayList<WifiConfiguration>();
	private WifiManager manager;
	private WifiInfo info;
	
	private CheckBox checkBox;
	private TextView textView;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        checkBox=(CheckBox) findViewById(R.id.checkBox1);
        textView=(TextView) findViewById(R.id.textView1);
        
        manager=(WifiManager) getSystemService(Context.WIFI_SERVICE);
        if(manager.isWifiEnabled()){
        	checkBox.setChecked(true);
        	textView.setText("WiFi已打开");
        }else{
        	checkBox.setChecked(false);
        	textView.setText("WiFi已关闭");
        }
        checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				if(isChecked==false){
					manager.setWifiEnabled(false);
					textView.setText("未打开WiFi");
				}else{
					manager.setWifiEnabled(true);
					list=manager.getConfiguredNetworks();
					if(list!=null&&list.size()>0){
						int networkid=list.get(0).networkId;
						
						manager.enableNetwork(networkid, true);
						textView.setText("打开wifi");
					}else{
						textView.setText("未搜索到wifi");
					}
				}
			}
		});
        
        
    }
}



manifest.xml

    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WAKE_LOCK"/>

android wifi打开

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号