我正在尝试在android中编写一个代码,它将显示以MYNET开头的可用wifi网络列表。我能够显示所有可用网络的列表,但是当我过滤列表时,我的应用程序崩溃了。请帮我解决一下这个。 我的代码如下。
package com.mynet.wifilist;
import android.annotation.SuppressLint;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.gesture.Prediction;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
public class MainActivity extends ListActivity {
WifiManager wfmanager;
WifiScanReceiver wifiReciever;
ListView list;
String all_wifi[];
String wifilist[];
Switch wfswitch;
EditText pass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wfswitch = (Switch)findViewById(R.id.switch1);
list=getListView();
wfmanager = (WifiManager)
getApplicationContext().getSystemService(Context.WIFI_SERVICE);
wifiReciever = new WifiScanReceiver();
ConnectivityManager connectivityManager = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo =
connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if(networkInfo.isConnected()){
wfswitch.setChecked(true);
}
else{
wfswitch.setChecked(false);
}
wfswitch.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean
isChecked) {
if(isChecked && !wfmanager.isWifiEnabled()){
Toast.makeText(getApplicationContext(),"Turning WiFi
On.......",Toast.LENGTH_LONG).show();
wfmanager.setWifiEnabled(true);
wfmanager.startScan();
}
else if(!isChecked && wfmanager.isWifiEnabled()){
Toast.makeText(getApplicationContext(),"Turning WiFi
Off...",Toast.LENGTH_LONG).show();
//unregisterReceiver(wifiReciever);
//list.setAdapter(null);
wfmanager.setWifiEnabled(false);
}
}
});
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int
position, long id) {
// selected item
String ssid = ((TextView) view).getText().toString();
connectToWifi(ssid);
Toast.makeText(MainActivity.this,"Wifi SSID :
"+ssid,Toast.LENGTH_SHORT).show();
}
});
}
protected void onPause() {
unregisterReceiver(wifiReciever);
super.onPause();
}
protected void onResume() {
registerReceiver(wifiReciever, new
IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
super.onResume();
}
class WifiScanReceiver extends BroadcastReceiver {
@SuppressLint("UseValueOf")
public void onReceive(Context c, Intent intent) {
List<ScanResult> wifiScanList = wfmanager.getScanResults();
all_wifi = new String[wifiScanList.size()];
wifilist = new String[wifiScanList.size()];
for(int i = 0; i < wifiScanList.size(); i++){
all_wifi[i] = (wifiScanList.get(i)).toString();
/*if(all_wifi[i].startsWith("MYNET")){
wifilist[i] = wifiScanList.get(i).toString();
}*/
}
String wf_filter_list[] = new String[wifiScanList.size()];
int counter = 0;
for (String eachWifi : wifilist) {
String[] temp = eachWifi.split(",");
wf_filter_list[counter] = temp[0].substring(5).trim();
counter++;
}
list.setAdapter(new ArrayAdapter<String>
(getApplicationContext(),R.layout.list_item,R.id.label,
wf_filter_list));
}
}
private void finallyConnect(String networkPass, String networkSSID) {
WifiConfiguration wifiConfig = new WifiConfiguration();
wifiConfig.SSID = String.format("\"%s\"", networkSSID);
wifiConfig.preSharedKey = String.format("\"%s\"", networkPass);
int netId = wfmanager.addNetwork(wifiConfig);
wfmanager.disconnect();
wfmanager.enableNetwork(netId, true);
wfmanager.reconnect();
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"\"" + networkSSID + "\"\"";
conf.preSharedKey = "\"" + networkPass + "\"";
wfmanager.addNetwork(conf);
}
private void connectToWifi(final String wifiSSID) {
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.connect);
dialog.setTitle("Connect to Network");
TextView textSSID = (TextView) dialog.findViewById(R.id.textSSID1);
Button dialogButton = (Button) dialog.findViewById(R.id.okButton);
pass = (EditText) dialog.findViewById(R.id.textPassword);
textSSID.setText(wifiSSID);
dialogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String checkPassword = pass.getText().toString();
finallyConnect(checkPassword, wifiSSID);
dialog.dismiss();
}
});
dialog.show();
}
}
我的过滤列表逻辑如下,在代码中有注释。取消注释并运行应用程序将导致应用程序崩溃。
if(all_wifi[i].startsWith("MYNET")) {
wifilist[i] = wifiScanList.get(i).toString();
}
提前致谢。