代码每隔几秒钟就不会刷新

时间:2014-02-15 06:01:53

标签: android timer android-timepicker

  String data ="";
        @Override   
        protected void onCreate(Bundle savedInstanceState)
        {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

                        WifiManager mainWifiObj;
                        mainWifiObj = (WifiManager) getSystemService(Context.WIFI_SERVICE); 
                            class WifiScanReceiver extends BroadcastReceiver
                            {
                                public void onReceive(Context c, Intent intent)
                                {
                                }
                            }       

                        WifiScanReceiver wifiReciever = new WifiScanReceiver();
                        registerReceiver(wifiReciever, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));

                        List<ScanResult> wifiScanList = mainWifiObj.getScanResults(); int signalLevel = 

    0; StringBuilder sb = new StringBuilder();
                            data = wifiScanList.get(8).toString();

      TextView tv = new TextView(this);
                tv.setText(sb); 
                setContentView(tv); 
}
     handler.post(runnable);




I want to add my timer such that this code should run 5 times and it should run every 2 seconds. I am new to android. I found the timer code from the internet, but whenever and whichever code i try to implement, it gives me error. Basically, I think I am not adding the code at proper place.

但是,我不知道,我应该在哪里保留它。在oncreate方法中,onCreate方法应位于run()

之间

我很新,所以问了这个问题。任何人都可以帮助我。

这里,给出了答案,但我无法打印代码

3 个答案:

答案 0 :(得分:0)

    private Handler handler; 

   @Override
   protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        handler = new Handler();
        Runnable runnable = new Runnable() {
              @Override
              public void run() {
                 handler.postDelayed(this, 2000); //2 seconds
                 //////Process to be executed every 2 seconds ////


              WifiManager mainWifiObj;
              mainWifiObj = (WifiManager) getSystemService(Context.WIFI_SERVICE); 
              class WifiScanReceiver extends BroadcastReceiver {
                   public void onReceive(Context c, Intent intent) {
                   }
                }

                WifiScanReceiver wifiReciever = new WifiScanReceiver();


                   //////////
              } 
         };

        //start it with:
         handler.post(runnable);

    }

答案 1 :(得分:0)

做这样的事情

public class MainActivity extends Activity {
    @Override   
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

         Runnable runnable = new Runnable() {
  @Override
  public void run() {
     handler.postDelayed(this, 2000); //2 seconds

    WifiManager mainWifiObj;
            mainWifiObj = (WifiManager) getSystemService(Context.WIFI_SERVICE); 
            class WifiScanReceiver extends BroadcastReceiver {
                   public void onReceive(Context c, Intent intent) {
                   }
                }

                WifiScanReceiver wifiReciever = new WifiScanReceiver();

  } 
          }; }}

          //start it with:
           handler.post(runnable);

希望这会有所帮助..

答案 2 :(得分:0)

您可以创建一个处理wifi连接更改的BroadcastReceiver。

更确切地说,你需要创建一个类 - 比如说NetWatcher:

public class NetWatcher extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    //here, check that the network connection is available. If yes, start your service. If not, stop your service.
   ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
   NetworkInfo info = cm.getActiveNetworkInfo();
   if (info != null) {
       if (info.isConnected()) {
           //start service
           Intent intent = new Intent(this, MyService.class);
           startService(intent);
       }
       else {
           //stop service
           Intent intent = new Intent(this, MyService.class);
           stopService(intent);
       }
   }
}}

此外,在AndroidManifest中,您需要添加以下行:

<receiver android:name="Yourpakege name.NetWatcher">
 <intent-filter>
      <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
 </intent-filter>

还有更多信息:http://android-er.blogspot.in/2011/01/monitor-wifi-status-and-information.html

相关问题