开发WIFI功能首先要开启wifi的相关权限,在AndroidManifest.xml配置如下
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
WIFI工具类:
package com.wifi.connect;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.net.DhcpInfo;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiManager.MulticastLock;
import android.net.wifi.WifiManager.WifiLock;
import android.text.format.Formatter;
import android.widget.Toast;
public class WIFIAdmin {
// 定义WifiManager对象
private WifiManager mWifiManager;
// 定义WifiInfo对象
private WifiInfo mWifiInfo;
// 扫描出的网络连接列表
private List<ScanResult> mWifiList;
// 网络连接列表
private List<WifiConfiguration> mWifiConfiguration;
// 定义一个WifiLock,(默认时手机锁屏时2分钟后wifi会关闭)
WifiLock mWifiLock;
//定义一个组播锁
private MulticastLock wifiLock;
/**
* 构造器,获取wifi信息和管理对象
* @param context
*/
public WIFIAdmin(Context context){
//获取WifiManager对象
mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
//获取WifiInfo对象
mWifiInfo = mWifiManager.getConnectionInfo();
}
/**
* 打开Wifi
* @param context
*/
public void openWifi(Context context) {
//wifi是否打开
if (!mWifiManager.isWifiEnabled()) {
mWifiManager.setWifiEnabled(true);
/*
WIFI_STATE_DISABLING:Wi-Fi网卡正在关闭,用整型常量0表示。
WIFI_STATE_DISABLED:Wi-Fi网卡不可用,用整型常量1表示。
WIFI_STATE_ENABLING:Wi-Fi网卡正在打开,启动需要一段时间,用整型常量2表示。
WIFI_STATE_ENABLED:Wi-Fi网卡可用,用整型常量3表示。
WIFI_STATE_UNKNOWN:未知网卡状态,用整型常量4表示。
*/
}else if (mWifiManager.getWifiState() == 2) {
Toast.makeText(context,"亲,Wifi正在打开,请稍等...", Toast.LENGTH_SHORT).show();
}else if (mWifiManager.getWifiState() == 0) {
Toast.makeText(context,"亲,Wifi已经打开,请稍等...", Toast.LENGTH_SHORT).show();
}
}
/**
* 关闭WIFI
* @param context
*/
public void closeWifi(Context context) {
if (mWifiManager.isWifiEnabled()) {
mWifiManager.setWifiEnabled(false);
}else if(mWifiManager.getWifiState() == 1){
Toast.makeText(context,"亲,Wifi已经关闭,不用再关了", Toast.LENGTH_SHORT).show();
}else if (mWifiManager.getWifiState() == 0) {
Toast.makeText(context,"亲,Wifi正在关闭,不用再关了", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(context,"请重新关闭", Toast.LENGTH_SHORT).show();
}
}
public void checkState(Context context) {
if (mWifiManager.getWifiState() == 0) {
Toast.makeText(context,"Wifi正在关闭", Toast.LENGTH_SHORT).show();
} else if (mWifiManager.getWifiState() == 1) {
Toast.makeText(context,"Wifi已经关闭", Toast.LENGTH_SHORT).show();
} else if (mWifiManager.getWifiState() == 2) {
Toast.makeText(context,"Wifi正在开启", Toast.LENGTH_SHORT).show();
} else if (mWifiManager.getWifiState() == 3) {
Toast.makeText(context,"Wifi已经开启", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context,"没有获取到WiFi状态", Toast.LENGTH_SHORT).show();
}
}
/**
* 锁定WifiLock
*/
public void acquireWifiLock() {
mWifiLock.acquire();
}
/**
* 解锁WifiLock
*
*/
public void releaseWifiLock() {
// 判断时候锁定
if (mWifiLock.isHeld()) {
mWifiLock.release();
}
}
/**
* 创建一个WifiLock
*/
public void createWifiLock() {
mWifiLock = mWifiManager.createWifiLock("MyLock");
}
/**
* 打开组播
*/
public void acquireMulticastLock() {
wifiLock.acquire();
}
/**
* 解锁MulticastLock
*
*/
public void releaseMulticastLock() {
if (wifiLock.isHeld()) {
wifiLock.release();
}
}
/**
* 创建一个组播MuticastLock
*/
public void createMuticastLock(){
wifiLock = mWifiManager.createMulticastLock(getClass().getSimpleName());
wifiLock.setReferenceCounted(true);
}
/**
* 得到配置好的网络
* @return
*/
public List<WifiConfiguration> getConfiguration(){
return mWifiConfiguration;
}
/**
* 指定配置好的网络进行连接
* @param index
*/
public void connectConfiguration(int index){
// 索引大于配置好的网络索引返回
if (index > mWifiConfiguration.size()) {
return;
}
// 连接配置好的指定ID的网络
mWifiManager.enableNetwork(mWifiConfiguration.get(index).networkId,
true);
}
/**
* 扫描Wifi
* 1.有的ssid为"",也就是ssid != null,获取不到ssid
* 设置中并没有多余wifi,但这个热点点其它信息可以获取到,说明这个热点是存在的,
* 是该热点隐藏了,所以获取不到。这也就是手机设置中为什么会有添加网路的按钮了
* 2 wifi列表中有许多同名的wifi热点,也就是扫描的结果中有重合部分,并不是有多个同名的wifi热点
* 当附近wifi热点比较少时不会出现此问题,当附近wifi网络比较多时会出现此问题。这就需要将同
* 名的热点进行删除,但是如果真有两个ssid名相同的wifi,那就可以通过capabilities去区分吧,
* 如果capabilities也相同就没办法了,系统设置里面也不显示同名的
* @param context
*/
public void startScan(Context context){
//开始扫描
mWifiManager.startScan();
//得到扫描结果
List<ScanResult> results = mWifiManager.getScanResults();
//得到配置好的网络连接
mWifiConfiguration = mWifiManager.getConfiguredNetworks();
if (results==null) {
switch (mWifiManager.getWifiState()) {
case 2:
Toast.makeText(context,"WiFi正在开启,请稍后重新点击扫描", Toast.LENGTH_SHORT).show();
break;
case 3:
Toast.makeText(context,"当前区域没有无线网络", Toast.LENGTH_SHORT).show();
break;
default:
Toast.makeText(context,"WiFi没有开启,无法扫描", Toast.LENGTH_SHORT).show();
break;
}
}else {
mWifiList = new ArrayList<ScanResult>();
for(ScanResult result:results){
if(result.SSID==null||result.SSID.length()==0||result.capabilities.contains("[IBSS]")){
continue;
}
boolean found = false;
for(ScanResult item:mWifiList){
if(item.SSID.equals(result.SSID)&&item.capabilities.equals(result.capabilities)){
found = true;
break;
}
}
if(!found){
mWifiList.add(result);
}
}
}
}
/**
* 得到网络列表
* @return
*/
public List<ScanResult> getWifiList() {
return mWifiList;
}
/**
* 查看扫描结果
* @return
*/
public StringBuilder lookupScan() {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < mWifiList.size(); i++) {
stringBuilder.append("Index_" + new Integer(i + 1).toString()+":");
// 将ScanResult信息转换成一个字符串包
// 其中把包括:BSSID、SSID、capabilities、frequency、level
stringBuilder.append((mWifiList.get(i)).toString());
stringBuilder.append("/n");
}
return stringBuilder;
}
/**
* 得到Mac地址
* @return
*/
public String getMacAddress() {
return (mWifiInfo==null)?"NULL":mWifiInfo.getMacAddress();
}
/**
* 得到接入点的BSSID
* @return
*/
public String getBSSID() {
return (mWifiInfo == null) ? "NULL" : mWifiInfo.getBSSID();
}
/**
* 得到IP地址
* @return
*/
public int getIPAddress() {
return (mWifiInfo == null) ? 0 : mWifiInfo.getIpAddress();
}
/**
* 得到连接的ID
* @return
*/
public int getNetworkId() {
return (mWifiInfo == null) ? 0 : mWifiInfo.getNetworkId();
}
/**
* 得到WifiInfo的所有信息包
* @return
*/
public String getWifiInfo() {
return (mWifiInfo == null) ? "NULL" : mWifiInfo.toString();
}
/**
* 添加一个网络并连接
* @param wfc
*/
public void addNetwork(WifiConfiguration wfc) {
int wfcId = mWifiManager.addNetwork(wfc);
Boolean b = mWifiManager.enableNetwork(wfcId, true);
System.out.println("a--" + wfcId);
System.out.println("b--" + b);
}
/**
* 断开指定ID网络
* @param netId
*/
public void disconnectWifi(int netId) {
mWifiManager.disableNetwork(netId);
mWifiManager.disconnect();
}
/**
* 获取网络SSID
* @return
*/
public String getSSID() {
return mWifiInfo.getSSID();
}
/**
* 判断是否存在SSID
* @param SSID
* @return
*/
private WifiConfiguration IsExsits(String SSID)
{
List<WifiConfiguration> existingConfigs = mWifiManager.getConfiguredNetworks();
for (WifiConfiguration existingConfig : existingConfigs)
{
if (existingConfig.SSID.equals("\""+SSID+"\""))
{
return existingConfig;
}
}
return null;
}
/**
* 连接新wifi
* @param SSID
* @param passward
* @param type
* @return
*/
public WifiConfiguration createWifiInfo(String SSID,String password,int type){
WifiConfiguration config = new WifiConfiguration();
//清除之前的内容
config.allowedAuthAlgorithms.clear();
config.allowedGroupCiphers.clear();
config.allowedKeyManagement.clear();
config.allowedPairwiseCiphers.clear();
config.allowedProtocols.clear();
config.SSID = "\"" + SSID + "\"";
WifiConfiguration tempConfig = this.IsExsits(SSID);
if (tempConfig!=null) {
mWifiManager.removeNetwork(tempConfig.networkId);
}
if(type == 1) //WIFICIPHER_NOPASS
{
config.wepKeys[0] = "";
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
config.wepTxKeyIndex = 0;
}
if(type == 2) //WIFICIPHER_WEP
{
config.hiddenSSID = true;
config.wepKeys[0]= "\""+password+"\"";
config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
config.wepTxKeyIndex = 0;
}
if(type == 3) //WIFICIPHER_WPA
{
config.preSharedKey = "\""+password+"\"";
config.hiddenSSID = true;
config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
//config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
config.status = WifiConfiguration.Status.ENABLED;
}
return config;
}
/**
* 获取wifi的IP地址
* @return
*/
public String getWifiIP(){
DhcpInfo dhcpInfo = mWifiManager.getDhcpInfo();
String routeIP = Formatter.formatIpAddress(dhcpInfo.gateway);
return routeIP;
}
}
WIFIActivity:
package com.wifi.main;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
import com.example.jushi_blub.R;
import com.wifi.connect.WIFIAdmin;
import com.wifi.udp.UDPUtils;
import com.wifi.utils.WifiUtils;
public class WIFIActivity extends Activity{
public static final String TAG = "WIFIActivity";
//展示扫描的wifi信号
private ListView mlistView;
protected WIFIAdmin mWifiAdmin;
private List<ScanResult> mWifiList;
public int level;
protected String ssid;
private TextView wifi_info,wifi_SSID,wifi_password;
private RadioButton radio1,radio2;
private String SSID,Password;
private static UDPUtils udpUtils = new UDPUtils();
private String SERVER_IP;
private int SERVER_PORT;
private static final Thread thread = new Thread(udpUtils);
//定时器
private Timer timer = new Timer();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wificonfig);
mWifiAdmin = new WIFIAdmin(WIFIActivity.this);
initViews();
getSSID();
SERVER_IP = mWifiAdmin.getWifiIP();
SERVER_PORT = 9090;
IntentFilter filter = new IntentFilter(
WifiManager.NETWORK_STATE_CHANGED_ACTION);
//监听wifi变化
registerReceiver(mReceiver, filter);
mlistView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long id) {
AlertDialog.Builder alert = new AlertDialog.Builder(WIFIActivity.this);
ssid=mWifiList.get(position).SSID;
//SSID是初始时连接的名称,ssid是即将要连接的名称
SSID = ssid;
alert.setTitle(ssid);
alert.setMessage("输入密码");
final EditText et_password = new EditText(WIFIActivity.this);
final SharedPreferences preferences = getSharedPreferences("wifi_password", Context.MODE_PRIVATE);
et_password.setText(preferences.getString(ssid, ""));
alert.setView(et_password);
alert.setPositiveButton("连接", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
Password = et_password.getText().toString();
if(null == Password || Password.length() < 8){
Toast.makeText(WIFIActivity.this, "密码至少8位", Toast.LENGTH_SHORT).show();
return;
}
Editor editor=preferences.edit();
editor.putString(ssid, Password); //保存密码
editor.commit();
getSSID();
//如果未收到成功回复每隔1秒发送一次SSID和密码
timer.schedule(new Mytask(), 0, 1000);
}
});
alert.setNegativeButton("取消", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
//
//mWifiAdmin.removeWifi(mWifiAdmin.getNetworkId());
}
});
alert.create();
alert.show();
}
});
}
/**
* 时钟任务
* @author 17993
*
*/
class Mytask extends TimerTask{
public Mytask(){
}
@Override
public void run() {
Message msg = new Message();
Bundle bundle = new Bundle();
String receiveMessage = udpUtils.getMessage();
//把数据放到buddle中
bundle.putString("receive", receiveMessage);
//把buddle传递到message
msg.setData(bundle);
myHandler.sendMessage(msg);
}
}
/**
* 使用Handler传递数据,避免内部线程不能创建handler
*/
Handler myHandler = new Handler(){
int i = 0;
@Override
public void handleMessage(Message msg)
{
super.handleMessage(msg);
Bundle bundle=new Bundle();
//从传过来的message数据中取出传过来的绑定数据的bundle对象
bundle = msg.getData();
String receive = udpUtils.getMessage();
if (receive.equals("cfg successfull")){
Toast.makeText(WIFIActivity.this, "配置成功!", Toast.LENGTH_SHORT).show();
//连接到配置的WIFI上
mWifiAdmin.addNetwork(mWifiAdmin.createWifiInfo(ssid, Password, 3));
udpUtils.setMessage("test");
//取消定时
timer.cancel();
Intent intent = new Intent();
intent.setClass(WIFIActivity.this, IPActivity.class);
startActivity(intent);
udpUtils.setKeepRunning(false);
finish();
}else {
i++;
if (i>=3) {
udpUtils.setMessage("cfg successfull");
Toast.makeText(WIFIActivity.this, "正在配置,请稍等...", Toast.LENGTH_SHORT).show();
Toast.makeText(WIFIActivity.this, SERVER_IP, Toast.LENGTH_SHORT).show();
}
//防止在主线程中操作网络,出现异常
new Thread(){
@Override
public void run()
{
udpUtils.sendConfig(SERVER_IP, 9090, ssid,Password);
udpUtils.StartListen();
}
}.start();
//启动监听UDP消息线程
}
}
};
/**
* 获取SSID
*/
public void getSSID() {
SSID = mWifiAdmin.getSSID();
if (SSID!=""||SSID!=null) {
wifi_SSID.setText(SSID.replace("\"", ""));
wifi_password.setText("********");
}else {
wifi_SSID.setText("");
wifi_password.setText("");
}
}
/**
* 控件初始化
*/
public void initViews(){
mlistView=(ListView) findViewById(R.id.wifi_list);
wifi_info = (TextView) findViewById(R.id.wifi_info);
wifi_SSID = (TextView) findViewById(R.id.wifi_SSID);
wifi_password = (TextView) findViewById(R.id.wifi_password);
radio1 = (RadioButton) findViewById(R.id.radio1);
radio2 = (RadioButton) findViewById(R.id.radio2);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.wifi_config, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
switch (id) {
case R.id.check_wifi:
mWifiAdmin.checkState(WIFIActivity.this);
break;
case R.id.open_wifi:
mWifiAdmin.openWifi(WIFIActivity.this);
break;
case R.id.close_wifi:
mWifiAdmin.closeWifi(WIFIActivity.this);
break;
case R.id.scan_wifi:
mWifiAdmin.startScan(WIFIActivity.this);
mWifiList=mWifiAdmin.getWifiList();
if(mWifiList!=null){
mlistView.setAdapter(new MyAdapter(this,mWifiList));
new WifiUtils.Utility().setListViewHeightBasedOnChildren(mlistView);
}
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
/**
* 监听Back键按下事件,方法1:
* 注意:
* super.onBackPressed()会自动调用finish()方法,关闭
* 当前Activity.
*/
@Override
public void onBackPressed() {
Intent intent = new Intent();
intent.setClass(this, MainActivity.class);
startActivity(intent);
udpUtils.setKeepRunning(false);
finish();
}
public class MyAdapter extends BaseAdapter{
LayoutInflater inflater;
List<ScanResult> list;
public MyAdapter(Context context, List<ScanResult> list) {
this.inflater = LayoutInflater.from(context);
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
//忽略指定的警告
@SuppressLint({ "ViewHolder", "InflateParams" })
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//针对每一个数据(即每一个List ID)创建一个ListView实例,
View view = null;
view = inflater.inflate(R.layout.wifi_listitem, null);
ScanResult result = list.get(position);
TextView wifi_ssid=(TextView) view.findViewById(R.id.ssid);
ImageView wifi_level=(ImageView) view.findViewById(R.id.wifi_level);
wifi_ssid.setText(result.SSID);
Log.i(TAG, "scanResult.SSID="+result);
level=WifiManager.calculateSignalLevel(result.level,5);
//两种wifi图像
if(result.capabilities.contains("WEP")||result.capabilities.contains("PSK")||
result.capabilities.contains("EAP")){
wifi_level.setImageResource(R.drawable.wifilock);
}else{
wifi_level.setImageResource(R.drawable.wifi);
}
wifi_level.setImageLevel(level);
//判断信号强度,显示对应的指示图标
return view;
}
}
/**
* 设置listview的高度
* *//*
public class Utility {
public void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
}
}*/
/**
* 监听wifi状态
*/
private BroadcastReceiver mReceiver = new BroadcastReceiver (){
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifiInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if(wifiInfo.isConnected()){
WifiManager wifiManager = (WifiManager) context
.getSystemService(Context.WIFI_SERVICE);
String wifiSSID = wifiManager.getConnectionInfo()
.getSSID();
Toast.makeText(context, wifiSSID+"连接成功", 1).show();
}
}
};
@Override
protected void onDestroy() {
super.onDestroy();
//销毁线程
udpUtils.setKeepRunning(false);
}
}
显示WIFI的activity_wificonfig.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.wifi.main.WIFIActivity" >
<ListView
android:id="@+id/wifi_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="160dp"
android:text="@string/wifi_list" >
</ListView>
<TextView
android:id="@+id/wifi_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="WIFI信息" />
<TextView
android:id="@+id/wifi_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/wifi_SSID"
android:layout_below="@+id/wifi_SSID"
android:layout_marginTop="23dp"
android:text="WIFI信息" />
<TextView
android:id="@+id/wifi_SSID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/wifi_info"
android:layout_below="@+id/wifi_info"
android:layout_marginTop="61dp"
android:text="WIFI信息" />
<RadioButton
android:id="@+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/wifi_password"
android:layout_alignBottom="@+id/wifi_password"
android:layout_alignLeft="@+id/radio1"
android:checked="true" />
<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/wifi_SSID"
android:layout_alignBottom="@+id/wifi_SSID"
android:layout_toLeftOf="@+id/wifi_SSID"
android:checked="true" />
</RelativeLayout>
显示WIFI名称和图标的子布局wifi_listitem.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.wifi.main.MainActivity" >
<TextView
android:id="@+id/ssid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/open_wifi"
android:layout_marginTop="26dp"/>
<ImageView
android:id="@+id/wifi_level"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/ssid"
android:layout_alignRight="@+id/ssid"
android:layout_below="@+id/close_wifi"
android:layout_toRightOf="@+id/close_wifi"
/>
</RelativeLayout>