Reputation: 2485
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.
Upvotes: 2
Views: 2217
Reputation: 2325
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{
}
Upvotes: 7
Reputation: 2485
this explains better
http://javatechig.com/android/repeat-alarm-example-in-android
how to set alarm with device reboot
Upvotes: 2
Reputation: 345
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
.
Upvotes: 0