Android handler.postDelayed不';不执行

Android handler.postDelayed不';不执行,android,android-wifi,android-handler,wifimanager,Android,Android Wifi,Android Handler,Wifimanager,我想在Android上制作一个每5秒扫描所有网络的应用程序。为了能够重复使用所有网络,我使用handler.postdayed。应执行task()和wi()函数任务()。知道为什么吗 public class MainActivity extends Activity { TextView tv , tv2; int i = 0 ; Timer t; Thread time; Handler handler; WifiManager wifi

我想在Android上制作一个每5秒扫描所有网络的应用程序。为了能够重复使用所有网络,我使用handler.postdayed。应执行
task()
wi()
函数<代码>任务()。知道为什么吗

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());
          }
       }
}
输出:
值1

我不完全确定问题是什么,但如果您想让处理程序持续运行,应该尝试以下方法:

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

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

您可能需要将这些标记添加到清单文件:

<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" />


每隔5秒我就在任务中调用wi()函数,但wi()没有显示任何thing@UsmanMustafa您是否包含了正确的权限?查看并检查对我的回答的编辑是的,我在清单文件中添加了这些权限,当我不使用处理程序时,它工作正常,但任务中的调用wi()不起作用