Maha
Maha

Reputation: 459

alarm in my application

i want an alarm in my app that every 10 minutes send a notification even if my app is close and stop after 2 Hours i am really need it

i wanna someone professional in alarm to help me please,it is for my GP i should submit it tomorrow

any help will be appreciate

Upvotes: 1

Views: 670

Answers (2)

Amokrane Chentir
Amokrane Chentir

Reputation: 30385

I have written a very detailed answer about how to use the AlarmManager. You can read it here.

Regarding your particular scenario. You can add a stopScheduling method to your scheduler, using the cancel() method:

public class TaskScheduler {
    PendingIntent mPendingIntent;
    AlarmManager mAlarmManager;

    public static void startScheduling(Context context) {
            Intent intent = new Intent(context, MyReceiver.class);
            mPendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
            mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
            mAlarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), 600, pendingIntent);
    }

    public static void stopScheduling() {
       mAlarmManager.cancel(mPendingIntent);
    }
}

So when you want to stop the scheduling you can call that method.

Now I am not aware of any simple method that would cause the AlarmManagerto stop after 2 hours by itself but you can write another scheduler that would start after 2 hours to remove the first scheduler.

I suggest you to do it step by step. First try implementing a simple alarm and play with the parameters to see how scheduling work and then try removing this alarm. Once this work, you'll easily do the rest as it's just some a basic programming task. Also please read the detailed answer that I have written here as it provides all the information needed to setup a basic alarm.

Upvotes: 6

FoamyGuy
FoamyGuy

Reputation: 46856

Read up on AlarmManager

Here is an example

Upvotes: 4

Related Questions