-4

I need a really simple explanation for the following:

I need a background service for my app, that runs even if the app isn't open. The examples I searched online don't work for me or my cases, so I really need detailled help here.

On the push of a button I want to start a task, that shows me instantly and all 5 minutes that it runs (I don't know what practice would be good to check this).

I know I need to work with an AlarmManager for this, but they never work. Can you make it really simple for a beginner? Thank you!

EDIT1:

public void scheduleAlarm(View V)
{
    Long time = new GregorianCalendar().getTimeInMillis()+5*1000;
    Intent intentAlarm = new Intent(this, AlarmReciever.class);
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP,time, PendingIntent.getBroadcast(this, 1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
    Toast.makeText(this, "Alarm Scheduled", Toast.LENGTH_LONG).show();

}

public class AlarmReciever extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        Toast.makeText(context, "Alarm Triggered!!!!", Toast.LENGTH_LONG).show();
    }

}

I used the example in the link below and got this now. The alarm should fire the Toast every 5 seconds, right? But it doesn't do it. I see "Alarm Scheduled" but nothing happens afterwards...

  • 1
    *"...but they never work"*. Why not? What have you tried? Post your code. – Luke Joshua Park Mar 26 '16 at 02:13
  • I deleted it, because it causes the app to crash, even without warnings or errors in the logs, super weird. I just need this as simple as it gets as example and then I can try it out if it really works, and what was different than before – Daniel Scholz Mar 26 '16 at 04:25
  • Concurrency isn't simple, regardless of how you look at it. SO doesn't provide free code, it helps with code that you have. Try again, find the problem, then post. – Luke Joshua Park Mar 26 '16 at 04:28
  • I put my code now, which I got with the help of the link below, maybe you can help with that now? – Daniel Scholz Mar 26 '16 at 04:58
  • I feel like you are trying to solve an XY problem here. If you need a background service, why are you not just using a background service? Why have you chosen AlarmManager? – Luke Joshua Park Mar 26 '16 at 06:00
  • Read the [official docs](http://developer.android.com/intl/in/training/scheduling/alarms.html). With downloadable example. – Phantômaxx Mar 26 '16 at 10:25

1 Answers1

0

Perhaps start off by reading through the Java tutorial on concurrency, and then move on to this Android tutorial specifically talking about AlarmManagers.

With the vagueness of your question I'm not sure what other help I can give you.

Zach Olivare
  • 3,805
  • 3
  • 32
  • 45