14

I have a problem in status bar notification at 10 second interval.I have done with code for display it for one time by creating plugin.But I want to display it at every 10 minutes interval.So I used AlarmManager for generating notification at every 10 minutes.But it does not call onReceive(Context ctx, Intent intent) method of FirstQuoteAlarm class. I have following code for display notification and AlarmManager.

public void showNotification( CharSequence contentTitle, CharSequence contentText ) {
    int icon = R.drawable.nofication;
    long when = System.currentTimeMillis();

    Notification notification = new Notification(icon, contentTitle, when);

    Intent notificationIntent = new Intent(ctx, ctx.getClass());
    PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, notificationIntent, 0);
    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

    mNotificationManager.notify(1, notification);

      Date dt = new Date();
      Date newdate = new Date(dt.getYear(), dt.getMonth(), dt.getDate(),10,14,dt.getSeconds());
      long triggerAtTime =  newdate.getTime();
      long repeat_alarm_every = 1000;
      QuotesSetting.ON = 1;

       AlarmManager am = ( AlarmManager )  ctx.getSystemService(Context.ALARM_SERVICE );
       //Intent intent = new Intent( "REFRESH_ALARM" );
       Intent intent1 = new Intent(ctx,FirstQuoteAlarm.class);
       PendingIntent pi = PendingIntent.getBroadcast(ctx, 0, intent1, 0 );
       am.setRepeating(AlarmManager.RTC_WAKEUP, triggerAtTime, repeat_alarm_every, pi);
       Log.i("call2","msg");


}
Vivek Kalkur
  • 2,200
  • 2
  • 21
  • 40
M007
  • 580
  • 1
  • 5
  • 24

2 Answers2

1

You should use different notification id as below code you used

mNotificationManager.notify(i, notification);

And also increase your when time

 Notification notification = new Notification(icon, contentTitle, when);
Hemant Dubey
  • 143
  • 5
0

Use ScheduledExecutorService. That usually gives better results.

It is meant to repeat actions in the background every so and so minutes. Start with delay and much more. Check out: http://developer.android.com/reference/java/util/concurrent/ScheduledExecutorService.html

Here is a class with a method that sets up a ScheduledExecutorService to beep every ten seconds for an hour:

import static java.util.concurrent.TimeUnit.*;
class BeeperControl {
private final ScheduledExecutorService scheduler =
 Executors.newScheduledThreadPool(1);

public void beepForAnHour() {
 final Runnable beeper = new Runnable() {
   public void run() { System.out.println("beep"); 
 };
 final ScheduledFuture beeperHandle =
   scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS);
 scheduler.schedule(new Runnable() {
   public void run() { beeperHandle.cancel(true); }
 }, 60 * 60, SECONDS);
}
}}
SunnySonic
  • 1,318
  • 11
  • 37