【问题标题】:onstartcommand method of service class is called repeatedally when startservice once trigred from activity class服务类的onstartcommand方法在活动类触发一次startservice时被重复调用
【发布时间】:2015-01-02 00:43:55
【问题描述】:

我正在关注教程链接http://www.coderzheaven.com/2012/07/14/ http request -call-repeatedly-service-android 但我对以下代码感到困惑。开始();已完成或由于 onstart() 方法中的标志检查。因此,一旦启动线程完成,就会重复 onstart() 方法,或者当线程启动完成时返回到 onstart()。请让我澄清一下。提前谢谢。

以下是代码->>

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

    }

}

【问题讨论】:

  • 使用`isRunning = false;`得到服务器响应后停止线程;
  • 您好先生,我的查询是 mythred.start(),它是从服务的 onstart() 方法触发的,该方法被称为线程,即每 3 秒延迟一次的 run() 方法。由于标志检查或一旦线程完成,它会返回到 onstart() 方法并再次启动线程或 onstart 方法本身在线程完成后返回,我没有得到那个..

标签: android service


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2014-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多