Abhishek Saxena
Abhishek Saxena

Reputation: 129

Notification started from AlarmManager keeps repeating again

When I set a Notification on a particular date and time chosen from a DatePicker and a TimePicker, The notification appears on the correct time but after I cancel it, it reappears after some time.
I have tried setAutoCancel(true) and Notification.FLAG_AUTO_CANCEL but they don't seem to work.

here is the code-snippet where I'm setting alarm

 if(item.getTitle().equals("Add Reminder"))
    {
        Log.d("REMINDER", "Show Time Picker Entered from Decided Case");
        final Dialog dialog = new Dialog(this);

        dialog.setContentView(R.layout.date_time_layout);

        dialog.setTitle("Choose Date and Time");

        dialog.show();
        Button ok = (Button) dialog.findViewById(R.id.alarm_set);
        ok.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                DatePicker dp = (DatePicker) dialog.findViewById(R.id.datePicker1);
                TimePicker tp = (TimePicker)dialog.findViewById(R.id.timePicker1);

                Calendar cal = Calendar.getInstance();       //for using this you need to import java.util.Calendar;

                // add minutes to the calendar object
                cal.set(Calendar.MONTH, dp.getMonth());
                cal.set(Calendar.YEAR, dp.getYear());               
                cal.set(Calendar.DAY_OF_MONTH, dp.getDayOfMonth());
                cal.set(Calendar.HOUR_OF_DAY, tp.getCurrentHour());
                cal.set(Calendar.MINUTE, tp.getCurrentMinute());
                //cal.add(Calendar.MINUTE, 1);
                 //cal.setTimeInMillis(System.currentTimeMillis());
                // cal.add(Calendar.SECOND, 10);
                Intent alarmintent = new Intent(getApplicationContext(), AlarmReceiver.class);


                PendingIntent sender = PendingIntent.getBroadcast(getApplicationContext(), 0,
                alarmintent,0);
                int HELLO_ID=1;
                // Get the AlarmManager service
                HELLO_ID++;
                AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
                am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
                Log.d("Alarm Set","day = "+dp.getDayOfMonth()+" month = "+(dp.getMonth()+1)+" year = "+dp.getYear()+" hours "+tp.getCurrentHour()+" minutes = "+tp.getCurrentMinute());
                dialog.dismiss();

            }
        });
    }  

AlarmReciever.java

public class AlarmReceiver extends BroadcastReceiver {



    @Override
public void onReceive(Context context, Intent intent) {

    // When our Alaram time is triggered , this method will be excuted (onReceive)
     // We're invoking a service in this method which shows Notification to the User
      Intent myIntent = new Intent(context, NotificationService.class);
      context.startService(myIntent);



}
};  

NotificationService.java

public class NotificationService extends Service {

private NotificationManager mManager;
Notification notification;
private static int NOTIFICATION_ID = 1;

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onCreate() {
    super.onCreate();
}

@Override
public void onStart(Intent intent, int startId) {
    super.onStart(intent, startId);
    // Getting Notification Service
    mManager = (NotificationManager) this.getApplicationContext()
            .getSystemService(
                    this.getApplicationContext().NOTIFICATION_SERVICE);
    /*
     * When the user taps the notification we have to show the Home Screen
     * of our App, this job can be done with the help of the following
     * Intent.
     */
    Intent intent1 = new Intent(this.getApplicationContext(), DecidedCase.class);
    PendingIntent pendingNotificationIntent = PendingIntent.getActivity(
            this.getApplicationContext(),NOTIFICATION_ID, intent1,
            PendingIntent.FLAG_UPDATE_CURRENT);
    //notification = new Notification(R.drawable.ic_launcher,
        //  "Trying out Alarm", System.currentTimeMillis());
    notification = new NotificationCompat.Builder(this.getApplicationContext())
                    .setContentTitle("ScheduLAWyer")
                    .setContentText("You've got a notification")
                    .setContentIntent(pendingNotificationIntent)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setAutoCancel(true)
                    .setPriority(8)
                    .setTicker("A case has to be viewed").build();
                    //.addAction(R.drawable.ic_launcher,"Launch app",pendingNotificationIntent)
                    //.addAction(R.drawable.ic_launcher, "Cancel",pendingNotificationIntent).build();

    intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
            | Intent.FLAG_ACTIVITY_CLEAR_TOP);

    //PendingIntent pendingNotificationIntent = PendingIntent.getActivity(
        //  this.getApplicationContext(), 0, intent1,
            //PendingIntent.FLAG_UPDATE_CURRENT);



    /*notification.setLatestEventInfo(this.getApplicationContext(),
            "sudduLAWyer", "Trying out Alarm Manager",
            pendingNotificationIntent);*/
    //notification.flags = Notification.FLAG_INSISTENT;
    notification.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_INSISTENT | Notification.FLAG_SHOW_LIGHTS;
    notification.defaults |= Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE;
    //notification.defaults |= Notification.DEFAULT_VIBRATE;
    //notification.flags |= Notification.FLAG_SHOW_LIGHTS;
    notification.ledARGB = 0xFFFFA500;
    notification.ledOnMS = 800;
    notification.ledOffMS = 1000;

    mManager.notify(NOTIFICATION_ID++, notification);
    Log.d("NOTIFICATION","Notficiation Set");
}

@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    mManager.cancel(0);
}

}

Upvotes: 0

Views: 740

Answers (2)

Sharanabasu Angadi
Sharanabasu Angadi

Reputation: 4382

Ideally you should not use service to just send notification , I think it would be better if you write some Util function and move the code of service onStart to Util method.

If you requirement needs service only then I suggest you to use IntentServive

Upvotes: 0

tasomaniac
tasomaniac

Reputation: 10342

It is probably because of the alarm. Notification itself cannot add it one more time if you call setAutoCancel(true).

You can try

a.cancel(sender) in onReceive of AlarmReceiver of yours. You will have to recreate that sender object to cancel it.

Upvotes: 2

Related Questions