Reputation: 232
The full scenario is I am trying to show notification according to user selected time for that I am using a TimePickerDialog
, BroadcastReceiver
class and a Service
class, every thing is working fine notification also appears at specific times, but the problem is when I open and close the application every time notification comes.
Activity.java
Intent myIntent = new Intent(ReminderActivity.this, MyBreakfastReciver.class);
System.out.println("getting Breakfast Reminder");
pendingIntent = PendingIntent.getBroadcast(ReminderActivity.this, 0, myIntent,0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
//bTimePart1 and bTimePart2 is the time choosen by user through time picker
calendar.set(Calendar.HOUR_OF_DAY, bTimePart1 );
calendar.set(Calendar.MINUTE, bTimePart2);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
BroadcastReciever
public class MyBreakfastReciver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
Intent service1 = new Intent(context, MyBreakfastAlarmService.class);
context.startService(service1);
}
}
Reciever class
private void showNotification(Context context) {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.notificationlogo)
.setContentTitle("DietGuru")
.setAutoCancel(true)
.setContentText("You haven't logged your BreakFast for today.")
.setSubText("Would you like to do it now?");
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(context, CalorieMainActivity.class);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(CalorieMainActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_CANCEL_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
//mBuilder.setContentIntent(resultPendingIntent);
mBuilder.setDefaults(Notification.DEFAULT_ALL);
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(1, mBuilder.build());
}
I just want to stop my notification when I open and close my application.
Upvotes: 4
Views: 679
Reputation: 24848
If your time time selection is before ur current time then Alaram fire event so try to check if time is before current time then add day so Alaram fire event on next day with given time, Check below example with fire notifiaction 8 a.m on each day :
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
// Set the alarm's trigger to next day if set alAram after 8 a.m.
if(calendar.get(Calendar.HOUR_OF_DAY)>8){
calendar.add(Calendar.DAY_OF_MONTH,1);
}
// Set the alarm's trigger time to 8:00 a.m.
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE,0);
calendar.set(Calendar.SECOND,0);
Upvotes: 1