My Requirment: I want my application to show a reminder notification on every Friday @ 8 am
I have used AlarmManager, BroadcastReceivers to implement my reminder notification. The problem I am getting is, When I set notification time using current system time and Add 2 minutes in it... It works perfectly when I use it in this way, it trigger my notification exactly after 2 minutes.
But,
When I use calender instance to set notification time at specific time of any day, it trigger my reminder notification when ever I start/open my app on my device/emulator and secondly it wont trigger notification at specified time
Following are my classes
Here is my HomeActivity.java
// long when = System.currentTimeMillis()+2*60*1000; // notification time
// WHEN I RUN THE ABOVE COMMENTED CODE… THE REMINDER IS TRIGGERD AFTER EXACTLY 2 MINS
//BUT WHEN I USE THE BELOW CODE USING CALENDER INSTANCE, IT TRIGGER MY REMINDER IMMIDIETLY WHEN I RUN IT ON MY DEVICE/EMULATOR
Calendar calendar = Calendar.getInstance();
//calendar.set(2014,Calendar.getInstance().get(Calendar.MONTH),Calendar.SUNDAY , 8, 00, 00);
calendar.set(2014,5,1,19,55,00);
long when = calendar.getTimeInMillis(); // notification time
Log.d("time", when+" ");
Intent intentAlarm = new Intent(this, AlarmReceiver.class);
// create the object
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
//set the alarm for particular time
alarmManager.set(AlarmManager.RTC_WAKEUP,when, PendingIntent.getBroadcast(this,1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
Here is my AlarmReceiver.java
package com.myapp.app;
import java.util.Calendar;
import java.util.GregorianCalendar;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Calendar now = GregorianCalendar.getInstance();
int dayOfWeek = now.get(Calendar.DATE);
if(dayOfWeek != 5 && dayOfWeek != 7) {
Notification.Builder mBuilder =
new Notification.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Here is my Title")
.setContentText("Here is my text");
Intent resultIntent = new Intent(context, HomeActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(HomeActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
}
}
}
Manifest.xml
I’ve added following in manifest
<receiver
android:name="com.myapp.app.AlarmReceiver"
/>
Thanks in advance for your help... :)
You need to use the setRepeating
to repeat your alarm every week.
alarmManager.setRepeating(AlarmManager.RTC, when, AlarmManager.INTERVAL_DAY * 7, pendingIntent);
P.S. Don't set the year. Otherwise, your alarm will trigger only once.