没有对点击事件做处理,但是在未搜索到Wifi信号时,default值是可以点击正常的,但是搜索到wifi信息后就不正常了,打印出来的log显示wifi的信息是完整的package com.android.dbs.wifidemo;import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;import android.app.Activity;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.SimpleAdapter;
import android.widget.SimpleExpandableListAdapter;
import android.widget.Toast;public class WifiDemoActivity extends Activity {
private final String tag = "WifiDemoActivity";
private Button btn_open, btn_close, btn_scan;
private ExpandableListView wifi_list_view;
private ArrayList<Map<String, Object>> wifi_result_group;
private ArrayList<ArrayList<Map<String, Object>>> wifi_result_child;
private List<ScanResult> wifi_list;
private List<WifiConfiguration> wifi_configuration;
private MyWifiManager mwm;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v(tag, "onCreate");
setContentView(R.layout.main);
}
@Override
public void onStart(){
super.onStart();
Log.v(tag, "onStart");
this.initViewItem();
this.prepareListener();
}
private void initViewItem(){
Log.v(tag, "initViewItem");
this.btn_close = (Button)this.findViewById(R.id.button_close);
this.btn_open = (Button)this.findViewById(R.id.button_open);
this.btn_scan = (Button)this.findViewById(R.id.button_scan);
this.wifi_list_view = (ExpandableListView)this.findViewById(R.id.expandableListView1);
this.wifi_result_group = new ArrayList<Map<String,Object>>();
this.wifi_result_child = new ArrayList<ArrayList<Map<String,Object>>>();
this.mwm = new MyWifiManager(this);
if(this.mwm.getWifiOpenState()){
this.btn_open.setClickable(false);
this.btn_close.setClickable(true);
}else{
this.btn_open.setClickable(true);
this.btn_close.setClickable(false);
}
this.listView();
}
private void prepareListener(){
Log.v(tag, "prepareListener");
this.btn_close.setOnClickListener(onClickListener);
this.btn_open.setOnClickListener(onClickListener);
this.btn_scan.setOnClickListener(onClickListener);
this.wifi_list_view.setOnGroupClickListener(new OnGroupClickListener(){ @Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
// TODO Auto-generated method stub
return false;
}
});
}
private void showDialog(String str){
Log.v(tag, "showDialog");
Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
}
private OnClickListener onClickListener = new OnClickListener(){ @Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.button_open:
openWifi();
break;
case R.id.button_close:
closeWifi();
break;
case R.id.button_scan:
scanWifi();
listView();
break;
}
}
};
private void openWifi(){
Log.v(tag, "openWifi");
int i = 10;
while(!this.mwm.getWifiOpenState() && i > 0){
this.mwm.openWifi();
i--;
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(i<=0){
this.showDialog("Open Wifi failed...");
return;
}else{
this.showDialog("Wifi Opened");
this.btn_open.setClickable(false);
this.btn_close.setClickable(true);
}
}
private void closeWifi(){
Log.v(tag, "closeWifi");
int i = 10;
while(this.mwm.getWifiOpenState() && i > 0){
this.mwm.closeWifi();
i--;
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(i<=0){
this.showDialog("Close Wifi failed...");
return;
}else{
this.showDialog("Wifi Closed");
this.btn_open.setClickable(true);
this.btn_close.setClickable(false);
}
}
private void scanWifi(){
Log.v(tag, "scanWifi");
this.mwm.startScan();
this.wifi_list = this.mwm.getScanResult();
this.wifi_configuration = this.mwm.getWifiConfiguration();
}
@SuppressWarnings("unchecked")
private void listView(){
Log.v(tag, "listView");
Map<String, Object> wifiItem;
ArrayList<Map<String, Object>> child;
ScanResult scanResult;
this.wifi_result_group.clear();
this.wifi_result_child.clear();
if(this.wifi_list != null){
child = new ArrayList<Map<String, Object>>();
for(int i = 0; i < this.wifi_list.size(); i++){
wifiItem = new HashMap<String, Object>();
scanResult = this.wifi_list.get(i);
wifiItem.put(WifiDefine.SSID, scanResult.SSID);
this.wifi_result_group.add(wifiItem);
child = new ArrayList<Map<String, Object>>();
wifiItem = new HashMap<String, Object>();
wifiItem.put(WifiDefine.BSSID, scanResult.BSSID);
wifiItem.put(WifiDefine.CAPAB, scanResult.capabilities);
wifiItem.put(WifiDefine.FREQ, scanResult.frequency);
wifiItem.put(WifiDefine.LEVEL, scanResult.level);
child.add(wifiItem);
this.wifi_result_child.add((ArrayList<Map<String, Object>>) child.clone());
}
}else{
wifiItem = new HashMap<String, Object>();
wifiItem.put(WifiDefine.SSID, this.getResources().getString(R.string.no_wifi));
this.wifi_result_group.add(wifiItem); child = new ArrayList<Map<String, Object>>();
wifiItem = new HashMap<String, Object>();
wifiItem.put(WifiDefine.BSSID, this.getResources().getString(R.string.no_wifi));
wifiItem.put(WifiDefine.CAPAB, this.getResources().getString(R.string.no_wifi));
wifiItem.put(WifiDefine.FREQ, this.getResources().getString(R.string.no_wifi));
wifiItem.put(WifiDefine.LEVEL, this.getResources().getString(R.string.no_wifi));
child.add(wifiItem);
this.wifi_result_child.add((ArrayList<Map<String, Object>>) child.clone());
}
this.showListView();
}
private void showListView(){
Log.v(tag, "showListView");
SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(
this,
wifi_result_group,
R.layout.grouplist,
new String[]{WifiDefine.SSID},
new int[]{R.id.wifi_ssid},
wifi_result_child,
R.layout.childlist,
new String[]{WifiDefine.BSSID, WifiDefine.CAPAB, WifiDefine.FREQ, WifiDefine.LEVEL},
new int[]{R.id.wifi_info_bssid, R.id.wifi_info_capa, R.id.wifi_info_freq, R.id.wifi_info_level});
this.wifi_list_view.setAdapter(adapter);
}
}
import java.util.HashMap;
import java.util.List;
import java.util.Map;import android.app.Activity;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.SimpleAdapter;
import android.widget.SimpleExpandableListAdapter;
import android.widget.Toast;public class WifiDemoActivity extends Activity {
private final String tag = "WifiDemoActivity";
private Button btn_open, btn_close, btn_scan;
private ExpandableListView wifi_list_view;
private ArrayList<Map<String, Object>> wifi_result_group;
private ArrayList<ArrayList<Map<String, Object>>> wifi_result_child;
private List<ScanResult> wifi_list;
private List<WifiConfiguration> wifi_configuration;
private MyWifiManager mwm;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v(tag, "onCreate");
setContentView(R.layout.main);
}
@Override
public void onStart(){
super.onStart();
Log.v(tag, "onStart");
this.initViewItem();
this.prepareListener();
}
private void initViewItem(){
Log.v(tag, "initViewItem");
this.btn_close = (Button)this.findViewById(R.id.button_close);
this.btn_open = (Button)this.findViewById(R.id.button_open);
this.btn_scan = (Button)this.findViewById(R.id.button_scan);
this.wifi_list_view = (ExpandableListView)this.findViewById(R.id.expandableListView1);
this.wifi_result_group = new ArrayList<Map<String,Object>>();
this.wifi_result_child = new ArrayList<ArrayList<Map<String,Object>>>();
this.mwm = new MyWifiManager(this);
if(this.mwm.getWifiOpenState()){
this.btn_open.setClickable(false);
this.btn_close.setClickable(true);
}else{
this.btn_open.setClickable(true);
this.btn_close.setClickable(false);
}
this.listView();
}
private void prepareListener(){
Log.v(tag, "prepareListener");
this.btn_close.setOnClickListener(onClickListener);
this.btn_open.setOnClickListener(onClickListener);
this.btn_scan.setOnClickListener(onClickListener);
this.wifi_list_view.setOnGroupClickListener(new OnGroupClickListener(){ @Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
// TODO Auto-generated method stub
return false;
}
});
}
private void showDialog(String str){
Log.v(tag, "showDialog");
Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
}
private OnClickListener onClickListener = new OnClickListener(){ @Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.button_open:
openWifi();
break;
case R.id.button_close:
closeWifi();
break;
case R.id.button_scan:
scanWifi();
listView();
break;
}
}
};
private void openWifi(){
Log.v(tag, "openWifi");
int i = 10;
while(!this.mwm.getWifiOpenState() && i > 0){
this.mwm.openWifi();
i--;
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(i<=0){
this.showDialog("Open Wifi failed...");
return;
}else{
this.showDialog("Wifi Opened");
this.btn_open.setClickable(false);
this.btn_close.setClickable(true);
}
}
private void closeWifi(){
Log.v(tag, "closeWifi");
int i = 10;
while(this.mwm.getWifiOpenState() && i > 0){
this.mwm.closeWifi();
i--;
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(i<=0){
this.showDialog("Close Wifi failed...");
return;
}else{
this.showDialog("Wifi Closed");
this.btn_open.setClickable(true);
this.btn_close.setClickable(false);
}
}
private void scanWifi(){
Log.v(tag, "scanWifi");
this.mwm.startScan();
this.wifi_list = this.mwm.getScanResult();
this.wifi_configuration = this.mwm.getWifiConfiguration();
}
@SuppressWarnings("unchecked")
private void listView(){
Log.v(tag, "listView");
Map<String, Object> wifiItem;
ArrayList<Map<String, Object>> child;
ScanResult scanResult;
this.wifi_result_group.clear();
this.wifi_result_child.clear();
if(this.wifi_list != null){
child = new ArrayList<Map<String, Object>>();
for(int i = 0; i < this.wifi_list.size(); i++){
wifiItem = new HashMap<String, Object>();
scanResult = this.wifi_list.get(i);
wifiItem.put(WifiDefine.SSID, scanResult.SSID);
this.wifi_result_group.add(wifiItem);
child = new ArrayList<Map<String, Object>>();
wifiItem = new HashMap<String, Object>();
wifiItem.put(WifiDefine.BSSID, scanResult.BSSID);
wifiItem.put(WifiDefine.CAPAB, scanResult.capabilities);
wifiItem.put(WifiDefine.FREQ, scanResult.frequency);
wifiItem.put(WifiDefine.LEVEL, scanResult.level);
child.add(wifiItem);
this.wifi_result_child.add((ArrayList<Map<String, Object>>) child.clone());
}
}else{
wifiItem = new HashMap<String, Object>();
wifiItem.put(WifiDefine.SSID, this.getResources().getString(R.string.no_wifi));
this.wifi_result_group.add(wifiItem); child = new ArrayList<Map<String, Object>>();
wifiItem = new HashMap<String, Object>();
wifiItem.put(WifiDefine.BSSID, this.getResources().getString(R.string.no_wifi));
wifiItem.put(WifiDefine.CAPAB, this.getResources().getString(R.string.no_wifi));
wifiItem.put(WifiDefine.FREQ, this.getResources().getString(R.string.no_wifi));
wifiItem.put(WifiDefine.LEVEL, this.getResources().getString(R.string.no_wifi));
child.add(wifiItem);
this.wifi_result_child.add((ArrayList<Map<String, Object>>) child.clone());
}
this.showListView();
}
private void showListView(){
Log.v(tag, "showListView");
SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(
this,
wifi_result_group,
R.layout.grouplist,
new String[]{WifiDefine.SSID},
new int[]{R.id.wifi_ssid},
wifi_result_child,
R.layout.childlist,
new String[]{WifiDefine.BSSID, WifiDefine.CAPAB, WifiDefine.FREQ, WifiDefine.LEVEL},
new int[]{R.id.wifi_info_bssid, R.id.wifi_info_capa, R.id.wifi_info_freq, R.id.wifi_info_level});
this.wifi_list_view.setAdapter(adapter);
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货