How to Store in Global Variable from local variable?

1.4k Views Asked by At

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!!!!!!!!

}
2

There are 2 best solutions below

0
On BEST ANSWER

The problem is before your global variable gets updated the Toast is called. Receiving happens on a background thread whereas Toast is being displayed on main Thread. If you want to display toast then you can put Toast in the receiver itself.

Moreover there is a chance that your global variable is null if there is some error while receiving

Also you can update global variable using a function and then calling a toast inside it. Like:

    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();
    }


    updateGloablString(sb);

......

private void updateGlobalString(String sb){
 sb_temp=sb.toString();
Toast.makeText(getApplicationContext(), sb_temp, Toast.LENGTH_SHORT).show();

}
0
On

you are registering receiver the Toast will be called immediately but the receiver is not, it has to receive something in order for it to display the toast. remove the Toast to within the BroadcastReceiver.