2



I am using AlarmManager() to fire Notification. I am setting it to fire at 10:30 AM of the morning and repeat at every 24 hours.

My code is as follow. I have tested yesterday and the problem is that It was repeated around 4-5 times in just next 2 hours. I am not understanding that what is the problem. I want to fire it only at 10:30 AM of the morning and repeat at only 24 hours.

Please help me solve the problem. I am calling this code on my app's Splash screen onCreate()

My Code :

Intent myIntent = new Intent(Splash.this, AlarmReceiver.class);

PendingIntent pendingIntent = PendingIntent.getBroadcast(Splash.this,
        0, myIntent, 0);

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

Calendar firingCal = Calendar.getInstance();
Calendar currentCal = Calendar.getInstance();

firingCal.set(Calendar.HOUR_OF_DAY, 10);
firingCal.set(Calendar.MINUTE, 30);
firingCal.set(Calendar.SECOND, 0);

long intendedTime = firingCal.getTimeInMillis();
long currentTime = currentCal.getTimeInMillis();

if (intendedTime >= currentTime) {
    alarmManager.setRepeating(AlarmManager.RTC, intendedTime,
            AlarmManager.INTERVAL_DAY, pendingIntent);

} else {
    firingCal.add(Calendar.DAY_OF_MONTH, 1);
    intendedTime = firingCal.getTimeInMillis();

    alarmManager.setRepeating(AlarmManager.RTC, intendedTime,
            AlarmManager.INTERVAL_DAY, pendingIntent);
}
Jeeten Parmar
  • 403
  • 2
  • 6
  • 16

2 Answers2

3

You can use CommonsWare cwac-wakeful library. It has an inbuilt support to set alarms.

Tarun
  • 13,727
  • 8
  • 42
  • 57
  • I am new to android development. So can you please give me some more guidance ? – Jeeten Parmar Jun 10 '13 at 05:43
  • The docs of the library has everything that you need to do. You can even check out the sample project. https://github.com/commonsguy/cwac-wakeful/tree/master/demo2 – Tarun Jun 10 '13 at 05:48
  • showing error : WakefulIntentService does not resolve a type. – Jeeten Parmar Jun 10 '13 at 05:51
  • you need to add the library in your path. – Tarun Jun 10 '13 at 06:10
  • 1
    There is no issue with your code except if your phone is sleeping the alarm manager will not wake up and fire the intent. Since you are acquiring the wakelock. http://stackoverflow.com/q/6864712/786337 – Tarun Jun 10 '13 at 06:12
  • can u tell me in that code "MainActivity.APP_TAG" stands for what ? MainActivity is ohk but wat abt App_Tag ? – Jeeten Parmar Jun 10 '13 at 06:38
  • can you post the link to that file? – Tarun Jun 10 '13 at 06:42
  • http://stackoverflow.com/questions/6864712/android-alarmmanager-not-waking-phone-up check its first answer – Jeeten Parmar Jun 10 '13 at 07:07
  • Its just a string tag. You can set this tag to your app name. http://developer.android.com/reference/android/os/PowerManager.html#newWakeLock(int, java.lang.String) – Tarun Jun 10 '13 at 07:10
  • tag - Your class name (or other tag) for debugging purposes. – Tarun Jun 10 '13 at 07:10
  • is it the class of the activity which im launching when notification fires ??? and I just wrote class name but it is showing error – Jeeten Parmar Jun 10 '13 at 07:14
  • Its a string. Use "ActivityName" – Tarun Jun 10 '13 at 07:16
  • Pass any string in the tag parameter. – Tarun Jun 10 '13 at 07:40
  • ohk, no problem. one more question, with alarmmanager. im using alarmManager.setRepeating(AlarmManager.RTC, intendedTime, AlarmManager.INTERVAL_DAY, pendingIntent); now I come to know about RTC_WAKEUP, so if i use it then it is also need to use wakeuplocker ? – Jeeten Parmar Jun 10 '13 at 07:54
  • Yes still you need to use wakeup lock. You should read more about the alarm manager and the corresponding alarm types in the dev references. That will give you an insight. – Tarun Jun 10 '13 at 07:56
0
// Retrieve a PendingIntent that will perform a broadcast
            Intent alarmIntent = new Intent(HomeContactActivity.this,
                    AlarmReceiver.class);
            pendingIntent = PendingIntent.getBroadcast(
                    HomeContactActivity.this, 0, alarmIntent, 0);

            AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

            // Set the alarm to start at 10:00 AM
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());
            calendar.set(Calendar.HOUR_OF_DAY, 10);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);



            manager.setRepeating(AlarmManager.RTC_WAKEUP,
                    calendar.getTimeInMillis(), 86400000, 
                    pendingIntent);  // for repeating in every 24 hours
Pratibha Sarode
  • 1,819
  • 17
  • 17