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 {
WifiManager wifiManager;
WifiInfo wifiInfo;
List<ScanResult> wifiList;
List<WifiConfiguration> wificonfList;
public WifiAdmin(Context context)
{
wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
wifiInfo = wifiManager.getConnectionInfo();
}
public void openWifi()
{
if(!wifiManager.isWifiEnabled())
{
wifiManager.setWifiEnabled(true);
}
}
public void closeWifi()
{
if(wifiManager.isWifiEnabled())
{
wifiManager.setWifiEnabled(false);
}
}
public void scanWifiList()
{
wifiManager.startScan();
wifiList = wifiManager.getScanResults();
wificonfList = wifiManager.getConfiguredNetworks();
}
public void connectWifi(int index)
{
if(index>wifiList.size())
{
return;
}
wifiManager.enableNetwork(wificonfList.get(index).networkId, true);
}
public void disconnectWifi()
{
int netid = wifiInfo.getNetworkId();
wifiManager.disableNetwork(netid);
wifiManager.disconnect();
}
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);
}
};
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) {
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>