0
  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().

私は新しいので、この質問をしました。誰でも私を助けてください。

ここで、与えられた答えは実行されますが、コードを印刷できません

4

3 に答える 3

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

    }
于 2014-02-15T06:07:21.337 に答える
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

于 2014-02-15T08:27:31.260 に答える
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);

お役に立てれば..

于 2014-02-15T06:18:33.530 に答える