Reputation: 2183
Main Purpose: Reading an access point's properties freshly, and use it.
I tried to add a custom BroadcastReceiver class to my activity.
If it is true; I learned that; onReceive
runs after intent action,
(here: WifiManager.SCAN_RESULTS_AVAILABLE_ACTION
).
But i think i must wait until onReceive
finishes running to get a fresh r11.
When i debug the method "useResults"; "use r11, modify r11" lines runs first, after a while onReceive
starts to run.
public class MainActivity extends ActionBarActivity implements OnTouchListener{
int r11=0;
public void useResults(){
mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
receiverWifi = new WifiReceiver();
registerReceiver(receiverWifi, new IntentFilter( WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
mainWifi.startScan();
//use r11
//modify r11
//do something with r11
}
@Override
protected void onPause() {
unregisterReceiver(receiverWifi);
super.onPause();
}
@Override
protected void onResume() {
registerReceiver(receiverWifi, new IntentFilter(
WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
super.onResume();
}
class WifiReceiver extends BroadcastReceiver {
public void onReceive(Context c, Intent intent) {
wifiList = mainWifi.getScanResults();
for (ScanResult result0:wifiList) {
String ssid0 = result0.SSID;
if(ssid0.compareTo("anID")==0){
r11=result0.level;
}
}
}
}
}
Upvotes: 0
Views: 247
Reputation: 39836
The BroadcastReceiver
is an inner class, there's nothing wrong or dirty in using it to call methods on the class. That's what inner classes are for.
Just create a separate method
void doR11Results(){
//use r11
//modify r11
//do something with r11
}
... and call it from onReceive
. Super simple!
Upvotes: 1