-1

I want to make app in Android that scans all the networks every 5sec. To can all networks repeatedly I use handler.postDelayed. The task() and wi() function should be executed. task() seems to work but wi() doesn't. Any idea why?

public class MainActivity extends Activity  {

    TextView tv , tv2;
    int i = 0 ;
    Timer t;
    Thread  time;
    Handler handler;
    WifiManager wifi;
    WifiScanReceiver wifiReciever;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv  = (TextView) findViewById(R.id.textView1); 
        tv2 = (TextView) findViewById(R.id.textView2);
        handler = new Handler();
        handler.postDelayed(new Runnable() { public void run() { task();}}, 5000);  
   }
    public void task(){
        i++;
        tv.setText("Values "+i);
        wi();
    }
    public void wi(){
        wifi=(WifiManager)getSystemService(Context.WIFI_SERVICE);
        wifiReciever = new WifiScanReceiver();
        wifi.startScan();
    }
    protected void onPause() {
          unregisterReceiver(wifiReciever);
          super.onPause();
       }   
    protected void onResume() {
          registerReceiver(wifiReciever, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
          super.onResume();
       }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    private class WifiScanReceiver extends BroadcastReceiver{
        public void onReceive(Context c, Intent intent) {

            WifiInfo ws = wifi.getConnectionInfo();
            tv2.setText(ws.toString());
          }
       }
}

output : value 1

AKroell
  • 622
  • 4
  • 18
Usman Mustafa
  • 77
  • 1
  • 8

1 Answers1

0

I'm not completely sure what the question is, but if you want to make the handler run constantly, you should try something like this:

final Handler handler = new Handler(); 
Runnable runnable = new Runnable() { 

    @Override 
    public void run() { 
            task();
            handler.postDelayed(this, 5000); 
        }
    } 
}; 
handler.postDelayed(runnable, 5000); 

EDIT:

You may need to add these tags to the manifest file:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />