Android run a task every 4 second
<p>Hi i need to call a method every 4 seconds, even when the device is sleeping, i use alarm manager with service Start_stick, the service name is TransactionService. the code works well when the device is active and the method is called every exact 4 second, but when the screen is locked and device sleep the calling becomes inaccurate. so the method is now called every 2 seconds, sometimes every 1 sec,5 ....</p>

<p>this is how i run the thread to call method every 4 seconds</p>

<pre><code>    AlarmManager mgr = (AlarmManager) getApplicationContext().getSystemService(
            Context.ALARM_SERVICE);
        Intent notificationIntent = new Intent(getApplicationContext(),
                TransactionService.class);
        PendingIntent pendingIntent = PendingIntent.getService(
                getApplicationContext(), 0, notificationIntent, 0);
        mgr.setRepeating(AlarmManager.RTC_WAKEUP,
                System.currentTimeMillis(), 4000, pendingIntent);
</code></pre>

<p>this is the log of calling the method when device is active and screen is on</p>

<pre><code>12-30 13:23:00.565 17397-17479/com.ids.simcardrefill D/url: calling
 12-30 13:23:04.565 17397-17537/com.ids.simcardrefill D/url:calling
 12-30 13:23:08.565 17397-17411/com.ids.simcardrefill D/url:calling
 12-30 13:23:12.565 17397-17655/com.ids.simcardrefill D/url:calling
</code></pre>

<p>and this is how the method is calling when device is sleeping</p>

<pre><code>12-30 13:09:12.565 17397-17655/com.ids.simcardrefill D/url:calling
12-30 13:09:17.785 17397-17598/com.ids.simcardrefill D/url:calling
12-30 13:09:20.565 17397-17479/com.ids.simcardrefill D/url:calling
12-30 13:09:25.775 17397-17537/com.ids.simcardrefill D/url:calling
12-30 13:09:28.565 17397-17411/com.ids.simcardrefill D/url:calling
</code></pre>

<p>here the difference between calling is inaccurate: 2 seconds, 5 seconds, 3 seconds</p>

<p>this is how the service look like :</p>

<pre><code>public int onStartCommand(Intent intent, int flags, int startId) {


    mshared = PreferenceManager
            .getDefaultSharedPreferences(getApplicationContext());
    edit = mshared.edit();
    hostname = mshared.getString(
            getApplicationContext().getString(R.string.hostname), "0");
    contin = true;
    cost
   = mshared.getString(getString(R.string.test), "0.09");
            if (contin) {
                getTransactions get = new    getTransactions(getApplicationContext());
                get.execute(hostname);
            }

    return START_STICKY;
}
</code></pre>

<p>`</p>

<p>any solution ??</p>