2

i am using following code to alert users using notifications at a specific time about new updates in the app every thing works fine and we receive alerts at the desired time

here is the mainactivity.java

public void setAlarm(){
    alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarmIntent = new Intent(MainActivity.this, AlarmReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(  MainActivity.this, 0, alarmIntent, 0);

    alarmStartTime.set(Calendar.HOUR_OF_DAY, 10);
    alarmStartTime.set(Calendar.MINUTE, 00);
    alarmStartTime.set(Calendar.SECOND, 0);
    alarmManager.setRepeating(AlarmManager.RTC, alarmStartTime.getTimeInMillis(), getInterval(), pendingIntent);
}
private int getInterval(){
     int days = 1;
     int hours = 24;
     int minutes = 60;
     int seconds = 60;
     int milliseconds = 1000;
     int repeatMS = days * hours * minutes * seconds * milliseconds;
     return repeatMS;
}

AlarmReceiver.java

public class AlarmReceiver extends BroadcastReceiver {

    NotificationManager notificationManager;

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent service1 = new Intent(context, AlarmService.class);
            context.startService(service1);
    }
}

and AlarmService.java

public class AlarmService extends Service { 
   private static final int NOTIFICATION_ID = 1;
   private NotificationManager notificationManager;
   private PendingIntent pendingIntent;

    @Override
    public IBinder onBind(Intent arg0)
    {
        return null;
    }

   @SuppressWarnings("static-access")
   @Override
   public void onStart(Intent intent, int startId)
   {
       super.onStart(intent, startId);
       Context context = this.getApplicationContext();
       notificationManager = (NotificationManager)context.getSystemService(context.NOTIFICATION_SERVICE);
       Intent mIntent = new Intent(this, MainActivity.class);
    pendingIntent = PendingIntent.getActivity(context, 0, mIntent, PendingIntent.FLAG_CANCEL_CURRENT);     
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setContentTitle("Bananas");
    builder.setContentText("get your bananas");
    builder.setSmallIcon(R.drawable.ic_launcher);
    builder.setContentIntent(pendingIntent);

    notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(NOTIFICATION_ID, builder.build());
    }
}

my problem is if we restart the phone all the previous info about alarms is lost and we dont receive an alert next time after restart, what can be done is it an issue with alarm manager or notifications.

1234567
  • 2,226
  • 4
  • 24
  • 69
  • 1
    you need to receive BOOT_COMPLETED in another broadcast reciever. http://stackoverflow.com/questions/12512717/android-alarmmanager-after-reboot – Krupal Shah May 24 '15 at 14:01
  • could you explain it in code , its not mentioned even in the info you provided – 1234567 May 24 '15 at 14:08

3 Answers3

7

You need to add this to your AndroidManifest.xml:

 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

 <receiver
            android:name="Your package name.AlarmReboot"
            android:enabled="true"
            android:exported="true" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.QUICKBOOT_POWERON" />
            </intent-filter>
        </receiver>

and add a Broadcast Reciever that will set the alarm again once the phone is rebooted:

public class AlarmReboot extends BroadcastReceiver{

}
Marija
  • 422
  • 4
  • 12
MorZa
  • 2,215
  • 18
  • 33
  • well the alarm did ring when phone was restarted, but a per the code it should ring every 24 hours after that, that part did not work how to get that working – 1234567 Jun 26 '15 at 06:31
  • 1
    What I did was to save the date as a string in shared preferences and then retrieve it in the BroadcastReciever, translate it again into a date and set the alarm again. I'm not sure that's the most elegant way to do that but it worked for me... – MorZa Jun 27 '15 at 12:48
2

this explains better

http://javatechig.com/android/repeat-alarm-example-in-android

how to set alarm with device reboot

1234567
  • 2,226
  • 4
  • 24
  • 69
0

Instead of using setRepeating, you can use setExact() API as setRepeating() API delays the AlarmManager intent for the duration it was in sleep mode or switched off.

Mithun
  • 2,075
  • 3
  • 19
  • 26
Sarfaraz
  • 324
  • 1
  • 3
  • 13