服务 class 的 onstartcommand 方法在从 activity class 触发一次 startservice 时重复调用

onstartcommand method of service class is called repeatedally when startservice once trigred from activity class

我正在学习教程 link http://www.coderzheaven.com/2012/07/14/ http request -call-repeatedly-service-android 但我对以下重复进行 http 调用的代码有点困惑所以 onstart() 方法或 onstartcommand()一旦它的启动线程 mythread.start() 被重复调用;已完成或由于 onstart() method.so 中的标志检查,onstart() 方法在线程启动完成后重复,或者当线程启动完成时 return 到 onstart()。请让我提前clear.thanks

代码如下->>

package com.coderzheaven.pack;

import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class MyService extends Service{

    private static String TAG = MyService.class.getSimpleName();
    private MyThread mythread;
    public boolean isRunning = false;

    @Override
    public IBinder onBind(Intent arg0) {
        <span id="IL_AD9" class="IL_AD">return</span> null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "onCreate");     
        mythread  = new MyThread();
    }

    @Override
    public synchronized void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy");
        if(!isRunning){
            mythread.interrupt();
            mythread.stop();
        }       
    }

    @Override
    public synchronized void onStart(Intent intent, int startId) {
        super.onStart(intent, startId); 
        Log.d(TAG, "onStart");
        if(!isRunning){
            mythread.start();
            isRunning = true;
        }
    }

    public void readWebPage(){
          HttpClient client = new DefaultHttpClient();
          HttpGet request = new HttpGet("http://google.com");
          // Get the response
          ResponseHandler<String> responseHandler = new BasicResponseHandler();
          String response_str = null;
          try {
             response_str = client.execute(request, responseHandler);
             if(!response_str.equalsIgnoreCase("")){
                 Log.d(TAG, "Got Response");
             }
          } catch (Exception e) {
             e.printStackTrace();
          }
    }

    class MyThread extends Thread{
        static final long DELAY = 3000;
        @Override
        public void run(){          
            while(isRunning){
                Log.d(TAG,"Running");
                try {                   
                    readWebPage();
                    Thread.sleep(DELAY);
                } catch (InterruptedException e) {
                    isRunning = false;
                    e.printStackTrace();
                }
            }
        }

    }

}

onstart 方法被调用一次。 while循环重复那个(readWebPage())函数并在获得响应后停止线程服务器只需在readWebPage()方法中为isRunnble变量提供false值