我写了一个运行的代码:
public class WifiActivity extends Activity {
private ListView listeViewWifi;
private List<WifiItem> listeWifiItem;
private WifiAdapter wifiAdapter;
private WifiManager wifiManager;
private WifiBroadcastReceiver broadcastReceiver;
private Timer timer;
private Timer2 timer2;
private boolean success;
private boolean broadcastFinish;
private Button buttonMeasure;
private Button buttonLocate;
private EditText coordX;
private EditText coordY;
private float[][][] radioMap = new float[100][100][5];
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wifi);
listeViewWifi = (ListView) findViewById(R.id.listViewWifi);
wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
if(wifiManager != null){
wifiManager.startScan();}
// On récupère le service WiFi d'Android
wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
// Gestion de la liste des AP WiFi (voir tuto sur les adapters et les
// listviews)
listeWifiItem = new ArrayList<WifiItem>();
wifiAdapter = new WifiAdapter(this, listeWifiItem);
listeViewWifi.setAdapter(wifiAdapter);
// Création du broadcast Receiver
broadcastReceiver = new WifiBroadcastReceiver();
// On attache le receiver au scan result
registerReceiver(broadcastReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
timer = new Timer(this);
timer.applyTimer();
[...]
public void tempsFini(){
broadcastFinish = WifiBroadcastReceiver.getBroadcastFinish();
Log.d("info3","broadcastFinish (activity) : " + broadcastFinish);
if(broadcastFinish == true){
success = false;
while(success==false){
success=wifiManager.startScan();
Log.d("info2","startScan : " + success);
}
}
}
然后这是计时器方法:
public class Timer {
private WifiActivity activity;
private Handler h = new Handler();
private Runnable run = new Runnable() {
@Override
public void run() {
Log.d("info3","timer");
activity.tempsFini(); //on met à jour le truc wifi
h.postDelayed(this, 200); //Donc revient toutes les 200ms
}
}
;
public Timer(WifiActivity wifiActivity){
activity = (WifiActivity) wifiActivity;
}
public void applyTimer() {
h.postDelayed(run, 0); //run est en fait vraiment instancié ici
}
public void cancelTimer(){
h.removeCallbacks(run);
}
}
该程序运行。我做了一些更改,因为我想在单击按钮时进行 AP 扫描。
buttonMeasure = (Button) findViewById(R.id.buttonMeasure);
buttonMeasure.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//Envoyer une moyenne de mesure pour chaque RSSI
Log.d("info7","onClick");
coordX = (EditText) findViewById(R.id.coordX);
coordY = (EditText) findViewById(R.id.coordY);
String xString = coordX.getText().toString();
final int xInt = Integer.parseInt(xString);
String yString = coordY.getText().toString();
final int yInt = Integer.parseInt(yString);
timer2 = new Timer2(this); //****
while(WifiBroadcastReceiver.getMeasureFinished()<=49){
Log.d("info7","mesure" + WifiBroadcastReceiver.getMeasureFinished());
timer2.applyTimer();
}
for(int i=0;i<=4;i++){
radioMap[xInt][yInt][i]=WifiBroadcastReceiver.getRSSI(i);
}
}
});
方法 timer2 与 timer 相同,我刚刚更改了:
public Timer2(OnClickListener onClickListener){
activity = (WifiActivity) onClickListener; //****
}
但是现在代码不起作用...... Logcat 在我评论* * 的行上显示错误。
你知道我该如何解决这个问题吗?
谢谢