1

I want to write some kind of background-live-ticker app for sports-web-services... I would like my app to be able to call the TIME_TICK all the time.

Btw: I also tried to use the AlarmManager, but the problem is the same.

But now my problem...

I use a Receiver with a Service for the execution part. The Receiver is called every minute correctly after register. But every night the service is terminated and will never be called again.

On Android 2.x everything works fine but Android 4.x will stop the Receiver every day... Is there any posibility to keep the app alive on Android 4.x?

The Reveiver is registered in my Main-Activity:

registerReceiver(new MyReceiver(), new IntentFilter(Intent.ACTION_TIME_TICK));

Manifest-entries:

<service android:name="de.pepdev.MyService" />
<receiver android:name="de.pepdev.MyReceiver">
    <intent-filter>
        <action android:name="android.intent.action.TIME_TICK" />
    </intent-filter>
</receiver>

Receiver-class:

public class MyReceiver extends BroadcastReceiver
{
    public static   long        nextExecTime    = 0;
    public static   Calendar    currentTime     = Calendar.getInstance();

    @Override
    public void onReceive(Context context, Intent intent)
    {
        currentTime = Calendar.getInstance();

        if(nextExecTime <= currentTime.getTimeInMillis())
        {
            Intent service = new Intent(context, MyService.class);
            context.startService(service);
        }
    }
}
user2854483
  • 11
  • 1
  • 2
  • Can you post the logs here? Do you know why the service is being terminated? – Gaurav Arora Oct 07 '13 at 12:29
  • thanks for the help, but the logs doesn't say anything. the service is called for a couple of time, and by night logcat does not have any further entries of my app. the last cycle finished successfully. also system messages like alarmmanage didn't throw anything. – user2854483 Oct 07 '13 at 13:19

1 Answers1

1

I also tried to use the AlarmManager, but the problem is the same

AlarmManager is a far better answer than ACTION_TIME_TICK, particularly if you let the user configure the polling frequency (including an option for "never poll, please, as I like my battery and bandwidth usage to stay low").

Please feel free to ask a separate StackOverflow question regarding whatever problems you feel your are experiencing with it.

But every night the service is terminated and will never be called again

Android can and will terminate your process at any point, either by user request or due to old age.

Manifest-entries:

The <receiver> is pointless, as you cannot register for ACTION_TIME_TICK via the manifest.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks. I think the AlarmManager ist better. I want the user to configure, on which times the app should run in background. But is there any possibility to keep the app alive and to avoid that android terminate it? – user2854483 Oct 07 '13 at 13:16
  • 1
    @user2854483: Keeping a process around is an anti-pattern on Android. Please use `AlarmManager` and allow your process to go away between checks at the user-defined polling period. – CommonsWare Oct 07 '13 at 13:19
  • the idea is, that the user configure e.g. some time-ranges for football matches oder races, which lies some days in the future. and i would like to run the background-process automatically. but after a time of more than 24 hours, the app is "dead" and no receiver is caught. it's okay for me to set the app to sleep or stop, but is there no posibility to "reanimate" the app? – user2854483 Oct 07 '13 at 13:32
  • 1
    @user2854483: That is what `AlarmManager` is for. You do not need to keep your app running constantly. `AlarmManager` will give your app control at the time(s) you specify (e.g., "some time-ranges... some days in the future"). – CommonsWare Oct 07 '13 at 13:34