欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

wifi开启关闭 博客分类: android androidwifi 

程序员文章站 2024-03-14 22:31:11
...

wifi的5中状态

WIFI_STATE_DISABLED:   无线网卡不可用

WIFI_STATE_DISABLING:无线网卡正在关闭

WIFI_STATE_ENABLED : 无线网卡可用

WIFI_STATE_ENABLING:l无线网卡正在打开

WIFI_STATE_UNKNOWN:未知网卡状态

 

 

 

Wifi权限

<!--wifi 权限 -->
               //Mainfest.permission 类中(可以查找权限)
	<uses-permission  android:name="android.permission.CHANGE_NETWORK_STATE"/>
	<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
	<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
	<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

 

WifiActivity

package com.example.wifi;

import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class WifiActivity extends Activity {

	Button btnOpen;
	Button btnClose;
	Button btnState;
	WifiManager wifi;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wifi);
        
        btnOpen =  (Button) findViewById(R.id.open); 
        btnClose =  (Button) findViewById(R.id.close);
        btnState =  (Button) findViewById(R.id.state);
        
        btnOpen.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				//获得wifi对象,Context.getSystemService(Context.WIFI_SERVICE);
				wifi = (WifiManager) WifiActivity.this.getSystemService(Context.WIFI_SERVICE);
				wifi.setWifiEnabled(true);//设置wifi可用
				Toast.makeText(WifiActivity.this, wifi.getWifiState(), Toast.LENGTH_SHORT).show();
				
			}
		});
        
        btnClose.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				wifi = (WifiManager) WifiActivity.this.getSystemService(Context.WIFI_SERVICE);
				wifi.setWifiEnabled(false);//设置wifi不可用,但是wifi关闭需要时间
				Toast.makeText(WifiActivity.this, wifi.getWifiState(), Toast.LENGTH_SHORT).show();
				
			}
		});
        
        btnState.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				wifi = (WifiManager) WifiActivity.this.getSystemService(Context.WIFI_SERVICE);
				//wifi.setWifiEnabled(false);
				Toast.makeText(WifiActivity.this, wifi.getWifiState(), Toast.LENGTH_SHORT).show();
				
			}
		}); 
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_wifi, menu);
        return true;
    }
}

 

  • wifi开启关闭
            
    
    博客分类: android androidwifi 
  • 大小: 26.4 KB
相关标签: android wifi