WifiManager

wifi管理类

package com.example.lab5;

import java.util.List;

import android.content.Context;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.widget.Toast;

public class WifiAdmin {

    //wifi管理器
    WifiManager wifiManager;
    //当前连接wifi的信息
    WifiInfo wifiInfo;

    //扫描附近的wifi列表
    List<ScanResult> wifiList;
    //已配置好的列表  曾经连接过的wifi
    List<WifiConfiguration> wificonfList;
    public WifiAdmin(Context context)
    {
        wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
        wifiInfo = wifiManager.getConnectionInfo();
    }

    //打开wifi
    public void openWifi()
    {
        if(!wifiManager.isWifiEnabled())
        {
            wifiManager.setWifiEnabled(true);
        }
    }

    //关闭wifi
    public void closeWifi()
    {
        if(wifiManager.isWifiEnabled())
        {
            wifiManager.setWifiEnabled(false);
        }
    }

    //扫描wifi列表
    public void scanWifiList()
    {
        wifiManager.startScan();
        wifiList = wifiManager.getScanResults();
        wificonfList = wifiManager.getConfiguredNetworks();
    }
    //连接指定的wifi
    public void connectWifi(int index)
    {
        if(index>wifiList.size())
        {
            return;
        }
        wifiManager.enableNetwork(wificonfList.get(index).networkId, true);

    }

    //断开连接的wifi
    public void disconnectWifi()
    {
        //先得到连接的id
        int netid = wifiInfo.getNetworkId();
        wifiManager.disableNetwork(netid);
        wifiManager.disconnect();

    }

    //得到wifi列表
    public List<ScanResult> getWifiList() {
        return wifiList;
    }


}

一些测试

package com.example.lab5;

import android.os.Bundle;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.net.wifi.ScanResult;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class MainActivity extends Activity {
    Button openbtn;
    Button closebtn;
    Button scanbtn;
    WifiAdmin wifiadmin;
    ListView list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Context ctx = (MainActivity.this).getApplicationContext();
        wifiadmin = new WifiAdmin(ctx);

        getWidget();
        registeListener();

    }

    public void getWidget()
    {
        openbtn = (Button)findViewById(R.id.openbtn);
        closebtn = (Button)findViewById(R.id.closebtn);
        scanbtn = (Button)findViewById(R.id.scanbtn);
    }


    public Button.OnClickListener opendwifi = new Button.OnClickListener()
    {

        @Override
        public void onClick(View v) 
        {
            wifiadmin.openWifi();
            Toast.makeText(MainActivity.this, "wifi已经打开", Toast.LENGTH_SHORT).show();
        }

    };


    public Button.OnClickListener closewifi = new Button.OnClickListener()
    {

        @Override
        public void onClick(View v) {
            wifiadmin.closeWifi();
            if(null!=list)
            {
                View view = ((ViewGroup)findViewById(android.R.id.content)).getChildAt(0);
                RelativeLayout layout = (RelativeLayout)view;
                layout.removeView(list);
            }
            Toast.makeText(MainActivity.this, "wifi已经关闭", Toast.LENGTH_SHORT).show();
        }

    };

    public Button.OnClickListener scanwifi = new Button.OnClickListener()
    {

        @Override
        public void onClick(View v) {
            wifiadmin.openWifi();
            wifiadmin.scanWifiList();
            List<ScanResult> listwifi = wifiadmin.getWifiList();
            list = (ListView)findViewById(R.id.listview);
            ArrayAdapter<ScanResult> adapter = new ArrayAdapter<ScanResult>(MainActivity.this, R.layout.array_item,listwifi);
            list.setAdapter(adapter);
            list.setOnItemClickListener(connectwifi);

        }

    };

    //listview监听事件
    public ListView.OnItemClickListener connectwifi= new ListView.OnItemClickListener()
    {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            wifiadmin.disconnectWifi();
            wifiadmin.connectWifi(position);

        }

    };


    public void registeListener()
    {
        openbtn.setOnClickListener(opendwifi);
        closebtn.setOnClickListener(closewifi);
        scanbtn.setOnClickListener(scanwifi);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

listview

<?xml version="1.0" encoding="utf-8"?>  
<TextView xmlns:android="http://schemas.android.com/apk/res/android"  
    android:id="@+id/tv_item1"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:textSize="20sp"  
    android:padding="10dp"  
    >   
</TextView> 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值