*I am developing an Android application which triggers an alarm at certain time that the user selects. But the problem is this alarm is only triggered if the user sets it to a little amount of time from now. So it's like if the app is not running the alarm won't trigger. I already acquired a lock and also set the flags to keep the screen on. Sometimes when I set it to 15 minutes from now or something like that it works and then I try again and it doesn't. I also included the broadcast receiver in the manifest. Here's my schedule alarm code:
public void schedule(Activity activity){
Context context = activity.getApplicationContext();
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmActivityBroadcastReceiver.class);
intent.putExtra("id", this.getId());
pendingIntent = PendingIntent.getBroadcast(context, 1234565666, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, this.getCalendarTime().getTimeInMillis(), pendingIntent);
}
To get the calendarTime i use the following function where time is the string of the time the user entered:
private Calendar convertTimeStringToCalendar(){
String[] timeArray = this.time.split(":");
int minute = Integer.parseInt(timeArray[1]);
int hour = Integer.parseInt(timeArray[0]);
Calendar c = Calendar.getInstance();
c.setTimeInMillis(System.currentTimeMillis());
int nowHour = c.get(Calendar.HOUR_OF_DAY);
int nowMinute = c.get(Calendar.MINUTE);
if (hour < nowHour || (hour == nowHour && minute <= nowMinute)) {
c.add(Calendar.DAY_OF_YEAR, 1);
}
c.set(Calendar.HOUR_OF_DAY, hour);
c.set(Calendar.MINUTE, minute);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
return c;
}
And finally this is my broadcast receiver:
public class AlarmActivityBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
WakeLock.acquire(context);
Alarm alarm = DataBaseSQL.getAlarm(intent.getLongExtra("id", 0L));
Intent alarmActivityIntent = new Intent(context, AlarmActivity.class);
alarmActivityIntent.putExtra("id", alarm.getId());
alarmActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
alarmActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NO_USER_ACTION);
context.startActivity(alarmActivityIntent);
}
}
I would appreciate any help in what it could be the reason for the alarms triggering only sometimes.
this code is working fine for me you can use it i hope it will work for you.
this is a fetch data method in main activty
public void fetchdata() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.DAY_OF_MONTH, day);
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
Intent myIntent = new Intent(MainActivity.this, MyBroadcastReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
}
This is broadcast receiver class
public class MyBroadcastReceiver extends BroadcastReceiver {
int notifyID=1;
public void onReceive(Context context, Intent intent) {
Intent i=new Intent();
PendingIntent pi=PendingIntent.getBroadcast(context,0,i,0);
NotificationManager nm=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notify=new Notification(R.drawable.ic_launcher,"Notification Triggered",System.currentTimeMillis());
CharSequence from="Remainder";
CharSequence message="Hurry up!!!!";
notify.setLatestEventInfo(context, from, message, pi);
nm.notify(notifyID,notify);
// Vibrate the mobile phone
Vibrator vibrator = (Vibrator) context
.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(2000);
}
}