2013-10-27 21 views
7

सेवा के माध्यम से http अनुरोध कैसे भेजें मुझे हर 10 सेकंड में सर्वर से स्थिति प्राप्त करने की आवश्यकता है।एंड्रॉइड: प्रत्येक 10 सेकंड

मैंने सेवा के माध्यम से http अनुरोध भेजकर ऐसा करने की कोशिश की।

समस्या यह है कि मेरा कोड केवल एक बार निष्पादित होता है।

public class ServiceStatusUpdate extends Service { 

@Override 
public IBinder onBind(Intent intent) { 
    // TODO Auto-generated method stub 
    return null; 
} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    while(true) 
    { 
     try { 
      Thread.sleep(10000); 
     } catch (InterruptedException e) { 
      new DoBackgroundTask().execute(Utilities.QUERYstatus); 
      e.printStackTrace(); 
     } 
     return START_STICKY;    
    } 
} 

private class DoBackgroundTask extends AsyncTask<String, String, String> { 

    @Override 
    protected String doInBackground(String... params) { 
     String response = ""; 
     String dataToSend = params[0]; 
     Log.i("FROM STATS SERVICE DoBackgroundTask", dataToSend); 
     HttpClient httpClient = new DefaultHttpClient(); 
     HttpPost httpPost = new HttpPost(Utilities.AGENT_URL); 

     try { 
      httpPost.setEntity(new StringEntity(dataToSend, "UTF-8")); 

      // Set up the header types needed to properly transfer JSON 
      httpPost.setHeader("Content-Type", "application/json"); 
      httpPost.setHeader("Accept-Encoding", "application/json"); 
      httpPost.setHeader("Accept-Language", "en-US"); 

      // Execute POST 
      HttpResponse httpResponse = httpClient.execute(httpPost); 
      HttpEntity responseEntity = httpResponse.getEntity(); 
      if (responseEntity != null) { 
       response = EntityUtils.toString(responseEntity); 
      } else { 
       response = "{\"NO DATA:\"NO DATA\"}"; 
      } 
     } catch (ClientProtocolException e) { 
      response = "{\"ERROR\":" + e.getMessage().toString() + "}"; 
     } catch (IOException e) { 
      response = "{\"ERROR\":" + e.getMessage().toString() + "}"; 
     } 
     return response; 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     Utilities.STATUS = result; 
     Log.i("FROM STATUS SERVICE: STATUS IS:", Utilities.STATUS); 
     super.onPostExecute(result); 
    } 
} 
} 

धन्यवाद के बहुत एवी

उत्तर

15

onPostExecute अंदर एक हैंडलर, हैंडलर फिर से और इतने पर और इतने पर ..

रखो 10 सेकेंड

new Handler().postDelayed(new Runnable() { 
     public void run() { 
      requestHttp(); 
     } 
    }, 10000); 

के बाद एक http अनुरोध भेजने के लिए 10 सेकेंड doInBackground फिर से निष्पादित किया जाएगा उसके बाद फिर से onPostExecute के बाद,

+0

बहुत बहुत धन्यवाद यह था। –

1

आप बस एंड्रॉयड में CountDownTimer वर्ग का उपयोग कर सकते हैं:

यह मेरी सेवा के लिए कोड है।

public class AlertSessionCountDownTimer extends CountDownTimer { 

    public AlertSessionCountDownTimer(long startTime, long interval) { 
     super(startTime, interval); 
    } 

    @Override 
    public void onFinish() { 
     //YOUR LOGIC ON FINISH 10 SECONDS 
    } 

    @Override 
    public void onTick(long millisUntilFinished) { 
     //YOUR LOGIC ON TICK 
    } 
} 

आशा है कि इससे आपकी मदद मिलेगी।

+0

ऑनफिनिश() आप किसी भी कक्षा में प्रसारण संदेश भेज सकते हैं जो वेब-सेवा कॉल करेगी या कुछ सामान करेगी और डेटा अपडेट करेगी .. यह आपकी आवश्यकता के अनुसार है कि आप इसे कैसे प्राप्त करना चाहते हैं । :-) –

0

कोड केवल एक बार चलता है क्योंकि आपके टाइमर लूप में return है। हालांकि, मैं सलाह देता हूं कि लूप को अपने पृष्ठभूमि कार्य में ले जाएं या onStartCommand() में लूप को लागू करने के बजाय यहां दिए गए अन्य उत्तरों में से किसी एक का उपयोग करें जो कभी वापस नहीं आएगा।

0
Handler handler_proc = new Handler(); 

final Runnable runnable_proc = new Runnable() { 
    public void run() { 
     requestHttp(new OnFinish() { 
      @Override 
      public void onSuccess() { 
       // pause should be considered since the end of the previous http request. 
       // Otherwise, new requests will occur before the previous one is complete, 
       // if http_timeout > delay 
       handler_proc.postDelayed(runnable_proc, 10000); 
      } 
     }); 
    } 
} 

handler_proc.post(runnable_proc); 
0

वापस लौटें START_STICKY; के बाद, और सेवा ठीक से काम करेगी

+0

क्या आप यह और अधिक विशिष्ट हो सकते हैं कि यह क्या करेगा, और एप्लिकेशन पर इसका असर होगा। – JamesENL

संबंधित मुद्दे