At the last Toast, It will print blank Toast.Why? I am passing local variable to a global variable and thus printed it. Why Java does not allow this? And What is the Solution Of this?
Here is the code...
String sb_temp=" "; //global variable
public void getsortedsignal()
{
final WifiManager wm=(WifiManager)getApplicationContext().getSystemService(Context.WIFI_SERVICE);
if(wm.isWifiEnabled()==false) {
Toast.makeText(getApplicationContext(), "Wifi Is Disabled.. Please Enable it", Toast.LENGTH_LONG).show();
wm.setWifiEnabled(true);
}
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
StringBuilder sb = new StringBuilder();
wm.startScan();
scanResults = wm.getScanResults();
//Making it in Asencending Order
Comparator<ScanResult> comparator = new Comparator<ScanResult>() {
@Override
public int compare(ScanResult lhs, ScanResult rhs) {
return (lhs.level > rhs.level ? -1 : (lhs.level == rhs.level ? 0 : 1));
}
};
Collections.sort(scanResults, comparator);
//Exact 5 Ap's Values
for (int i = 0; i < 5; i++) {
sb.append(scanResults.get(i).SSID + "--").toString();
sb.append(scanResults.get(i).BSSID + "--").toString();
sb.append((-1 * scanResults.get(i).level) + "\n").toString();
}
sb_temp=sb.toString();
//Toast.makeText(getApplicationContext(), sb_temp, Toast.LENGTH_SHORT).show();
}
}, new IntentFilter(WifiManager.RSSI_CHANGED_ACTION));
Toast.makeText(getApplicationContext(), sb_temp, Toast.LENGTH_SHORT).show(); //Problem is Here!!!!!!!!
}