我正在编写一个应用程序来扫描可用的无线网络,但是,当我运行我的应用程序时,它甚至在显示主要活动之前就崩溃了。我也在寻找如何使用扫描结果,但我不确定我使用它的方式是否正确。我阅读了有关广播接收器的信息,但没有找到任何有关如何使用它的示例。这是我的代码,非常感谢任何帮助
package com.example.wizer2;
import java.util.List;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Wizer extends Activity {
WifiManager Wifi;
BroadcastReceiver receiver;
List<ScanResult> results;
Button bt1; //Current status
TextView t1; //for current status result
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wizer);
//GUI
bt1=(Button)findViewById(R.id.b1);
t1=(TextView)findViewById(R.id.t1);
// to enable wifi service
Wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
bt1.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
WifiInfo info = Wifi.getConnectionInfo();
if (info.getBSSID()==null)
{
t1.setText("You are currently not connected to any wireless network.\n");
}
else
{
t1.append("Current Status:\n");
t1.append("Network Name :"+info.getSSID().toString()+"\n");
t1.append("RSSI :"+info.getRssi()+"\n");
}
}
});
Wifi.startScan();
// Register Broadcast Receiver
if (receiver == null)
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
results =Wifi.getScanResults();
// Intent I1 =new Intent();
}
};
registerReceiver(receiver, new IntentFilter( WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_wizer, menu);
return true;
}
public void onStop() {
unregisterReceiver(receiver);
}
}