Note that there are some explanatory texts on larger screens.

plurals
  1. POAsynTask scheduled in Android service executes properly only one time
    primarykey
    data
    text
    <p>I am trying to create simple Android program that pulls web server every 30*1000 milliseconds. I have dedicated service in my application - PollerService</p> <pre><code>Android Manifest.xml: &lt;application android:icon="@drawable/icon" android:label="@string/app_name"&gt; &lt;activity android:name=".PullServerAndListenActivivty" android:label="@string/app_name"&gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.MAIN" /&gt; &lt;category android:name="android.intent.category.LAUNCHER" /&gt; &lt;/intent-filter&gt; &lt;/activity&gt; &lt;service android:name=".PollerService" /&gt; &lt;/application&gt; </code></pre> <p>The class creates scheduled executor and registers its timer task to start calling the Engine which in order pulls the server.</p> <pre><code>PollerService.java: public class PollerService extends Service { public static final int INTERVAL = 20*1000; private ScheduledExecutorService executor; public void onCreate() { super.onCreate(); executor = Executors.newSingleThreadScheduledExecutor(); executor.scheduleAtFixedRate(new TimerTask() { public void run() { Engine.tick (); } }, 0, INTERVAL, TimeUnit.MILLISECONDS); } public void onDestroy() { executor.shutdownNow(); } public IBinder onBind(Intent arg0) { return null; }; } </code></pre> <p>The Engine just creates AsyncTask subclass instance in GUI thread and executes it. onPostExecute writes a line through a console which was assigned in Activity and calls UI components.</p> <pre><code>public class Engine { private static Console console; //Assigned in Activity onCreate private static Activity context;//Assigned in Activity onCreate ... public static void tick() { final String requestURL = String.format(urlFormat, serverURL); try { context.runOnUiThread( new Runnable () { public void run() { new RequestTask().execute(requestURL); }; } ); } catch (Throwable e) { Log.e("SBOX", e.getMessage(), e); } } public static void processCommand(String result) { try { final Command command = fromJSONToCommand(result); context.runOnUiThread(new Runnable() { public void run() { console.writeLine("Receieved command: " + command); } }); } catch (Exception e) { Log.e ("SBOX", e.getMessage(), e); } } </code></pre> <p>Yak, I started from simple RequestTask instance creation, I was under impression that it will run on UI thread by itself but trying to fix the problem I finally came to this approach, still useless. Just want to say that I tried a lot here.</p> <p>RequestTask looks like this, simple http call:</p> <pre><code>class RequestTask extends AsyncTask&lt;String, Void, String&gt;{ @Override protected String doInBackground(String... uri) { HttpClient httpclient = new DefaultHttpClient(); HttpResponse response; String responseString = null; try { response = httpclient.execute(new HttpGet(uri[0])); StatusLine statusLine = response.getStatusLine(); if(statusLine.getStatusCode() == HttpStatus.SC_OK){ ByteArrayOutputStream out = new ByteArrayOutputStream(); response.getEntity().writeTo(out); out.close(); responseString = out.toString(); } else{ response.getEntity().getContent().close(); throw new IOException(statusLine.getReasonPhrase()); } } catch (Exception e) { Log.e("SBOX", e.getMessage(), e); } return responseString; } @Override protected void onPostExecute(String result) { super.onPostExecute(result); Engine.processCommand (result); } } </code></pre> <p>So the problem is that it runs properly only once, timer schedules, calls first time the Engine, it runs RequestTask, it pulls server, gets the responce, prints it in the console. But second time it gets into the timer method and then no signs of life from the service. No exceptions in LogCat, logging in RequestTask is not printed.</p> <p>Of course its time to get to debug, but probably someone can save me from this pain and show errors in the code? The executor itself works perfectly continuous if to comment Engine.tick() call.</p> <p>Will be very greatful for clues on resolution.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload