Reputation: 1219
My app consists of two methods namely, one time alarm and repeat alarm for every day, i need to set alarm based on my requirement. When i set one time alarm it is working fine but when i set repeat alarm it is not working. I have worked on following method also but it did work for me.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, hour); // hour - user selected hour
calendar.set(Calendar.MINUTE, minute); // minute -user selected minute
// setRepeating() lets you specify a precise custom interval--in this case,
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, alarmIntent);
Here is the method of one time and repeat alarm in my fragment.
private SimpleDateFormat EEE_DD_MMM_YYYY_HH_MM_SS_A_Formatter = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss a"); //Tue, 07 Mar 2017 10:50:00 a.m.
PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity().getBaseContext(), alarmId, intent, 0); // where alarmId is autoIncrement
AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(ALARM_SERVICE);
try {
Date alarmDate = EEE_DD_MMM_YYYY_HH_MM_SS_A_Formatter.parse(getDateTime()); // where we get both time and date from both pickers respectively.
if (oneTimeAlarm) {
Logs.i(TAG,"one time alarm");
alarmManager.set(AlarmManager.RTC_WAKEUP, alarmDate.getTime(), pendingIntent);
Log.i(TAG,"one time alarm set!");
} else {
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, alarmDate.getTime(), AlarmManager.INTERVAL_DAY, pendingIntent);
Log.i(TAG,"repeat alarm set!");
}
} catch (Exception e) {
e.printStackTrace();
}
And here is my alarm receiver class file
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Vibrator vib = (Vibrator) context.getSystemService(context.VIBRATOR_SERVICE); //for Vibration
vib.vibrate(2000);
showNotification(context);
}
private void showNotification(Context context) {
Intent intent = new Intent(context, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context,alarmId,intent, 0);
// NotificationCompat
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Title")
.setContentText("text");
mBuilder.setContentIntent(contentIntent);
mBuilder.setDefaults(Notification.DEFAULT_SOUND);
mBuilder.setAutoCancel(true);
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());}}
Upvotes: 0
Views: 1000
Reputation: 2576
Try with this. I am using this and it is working fine. And use Service or Intent service to throw Notification not using Brodcast.
AlarmManager al;
PendingIntent fintent;
Calendar calendar;
Intent notif;
long _alarm;
Calendar now = Calendar.getInstance();
Calendar wakeupcall = Calendar.getInstance();
wakeupcall.setTimeInMillis(System.currentTimeMillis());
wakeupcall.set(Calendar.HOUR_OF_DAY, 12);
wakeupcall.set(Calendar.MINUTE, 30);
if (wakeupcall.getTimeInMillis() <= now.getTimeInMillis())
_alarm=wakeupcall.getTimeInMillis() + (AlarmManager.INTERVAL_DAY+1);
else
_alarm=wakeupcall.getTimeInMillis();
al = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
notif= new Intent(this,TestNotifyService.class);
fintent = PendingIntent.getService(this,0,notif,0);
if (SDK_INT < Build.VERSION_CODES.KITKAT) {
al.set(AlarmManager.RTC_WAKEUP,_alarm, fintent);
}
else if (Build.VERSION_CODES.KITKAT <= SDK_INT && SDK_INT < Build.VERSION_CODES.M) {
al.setExact(AlarmManager.RTC_WAKEUP,_alarm,fintent);
}
else if (SDK_INT >= Build.VERSION_CODES.M) {
al.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,_alarm,fintent);
}
And if you want to fetch data from server and then post notification then Use Power Manager and Wakelog to successfully show notification. And don't forget to add WAKE LOG and SET ALARM permission to menifest.
Resetting alarm after creating notification:
private void reSETNOTIFY() {
Calendar now = Calendar.getInstance();
Calendar wakeupcall = Calendar.getInstance();
wakeupcall.setTimeInMillis(System.currentTimeMillis());
wakeupcall.set(Calendar.HOUR_OF_DAY, 12);
wakeupcall.set(Calendar.MINUTE, 29);
if (wakeupcall.getTimeInMillis() <= now.getTimeInMillis())
_alarm=wakeupcall.getTimeInMillis() + (AlarmManager.INTERVAL_DAY+1)+25000;
else
_alarm=wakeupcall.getTimeInMillis()+25000;
al = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
notif= new Intent(this,TestNotifyService.class);
fintent = PendingIntent.getService(this,0,notif,0);
if (SDK_INT < Build.VERSION_CODES.KITKAT) {
al.set(AlarmManager.RTC_WAKEUP,_alarm, fintent);
}
else if (Build.VERSION_CODES.KITKAT <= SDK_INT && SDK_INT < Build.VERSION_CODES.M) {
al.setExact(AlarmManager.RTC_WAKEUP,_alarm,fintent);
}
else if (SDK_INT >= Build.VERSION_CODES.M) {
al.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,_alarm,fintent);
}
}
Upvotes: 1