how to make alarm notification On just Friday every week in android - android

I am working on an application in which i want to make a notification on Friday every week i have tried this code but it make notification on every day
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_WEEK,6);
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
String h = String.valueOf(hourOfDay);
String m = String.valueOf(minute);
Intent intent = new Intent(getApplicationContext(), notification.class);
pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 7*AlarmManager.INTERVAL_DAY, pendingIntent);
Toast.makeText(Settings.this, h+ " : " + m , Toast.LENGTH_SHORT).show();
Notification.class
public class notification extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
PendingIntent pendingIntent=PendingIntent.getActivity(context,0,new Intent(context,The_main.class),0);
NotificationCompat.Builder builder=new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.awq)
.setContentTitle("remainder")
.setContentText(" time to read");
builder.setContentIntent(pendingIntent);
builder.setDefaults(NotificationCompat.DEFAULT_SOUND);
builder.setAutoCancel(true);
NotificationManager notificationManager=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(1);
notificationManager.notify(1,builder.build());
}}

Try replacing
`7*AlarmManager.INTERVAL_DAY`
with
7 * 24 * 60 * 60 * 1000 (7days Interval in milliseconds)

Related

Configuring an AlarmManager for another timezone

I would like to configure alarm manager for a different timezone. I would like the alarm to trigger a notification whenever 10:20 NY time has reached. Is it possible to code like below?
private void configureDailyAlarm(Context context) {
TimeZone USTimeZone = TimeZone.getTimeZone("America/New_York");
Calendar USCalendar = Calendar.getInstance(USTimeZone);
USCalendar.set(Calendar.HOUR_OF_DAY, 10);
USCalendar.set(Calendar.MINUTE, 20);
Intent alarmReceiverIntent = new Intent(context, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmReceiverIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, USCalendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}
Use this method:
public int getTimeFromNY() {
TimeZone USTimeZone = TimeZone.getTimeZone("America/New_York");
Calendar USCalendar = Calendar.getInstance(USTimeZone);
USCalendar.set(Calendar.HOUR_OF_DAY, 10);
USCalendar.set(Calendar.MINUTE, 20);
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date()); // Current date
return USCalendar.getTimeInMillis() - calendar.getTimeInMillis();
}
Then plug that into AlarmManager
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeInMillis() + USCalendar.getTimeFromNY(), AlarmManager.INTERVAL_DAY, pendingIntent);

Dialy Alarm code Explanation

I am new to android , I want to make alarm that notify me dialy at 11am .I have found some code on net,But dont know some part of it. Below is code
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(AndroidAlarmService.this, MyAlarmService.class);
pendingIntent = PendingIntent.getService(AndroidAlarmService.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis()); does
calendar.add(Calendar.SECOND, 10);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 5*1000, pendingIntent);
Toast.makeText(AndroidAlarmService.this, "Start Alarm", Toast.LENGTH_LONG).show();
}});
can any one explain me what this two line does
calendar.setTimeInMillis(System.currentTimeMillis()); does
calendar.add(Calendar.SECOND, 10);
And also help me how can i set alarm that will notify me on the same time for example at 11am
Call this function where you need:
private void setDailyNotification(int ID, int hh, int mm, int ss) {
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent alarmIntent = new Intent(Dashboard.this, MyDailyReceiver.class);
alarmIntent.putExtra("ID", ID);
Log.d("setDailyNotification", "ID:" + ID);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
Dashboard.this, ID, alarmIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
mEverydayPendingIntent = pendingIntent;
Calendar calendar = Calendar.getInstance();
Calendar now = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hh);
calendar.set(Calendar.MINUTE, mm);
calendar.set(Calendar.SECOND, ss);
// check whether the time is earlier than current time. If so, set it to
// tomorrow. Otherwise, all alarms for earlier time will fire
if (calendar.before(now)) {
calendar.add(Calendar.DATE, 1);
}
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY,
mEverydayPendingIntent);
Log.d("setRepeated", "ID:" + ID);
}
Broadcast receiver:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
context);
mBuilder.setContentTitle("Daily Summery");
mBuilder.setContentText("Today's Transaction");
mBuilder.setSmallIcon(R.drawable.app_icon);
Log.d("tag1234", "In if" + daily_Reminder);
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
mBuilder.setStyle(inboxStyle);
mNotificationMa = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationMa.notify(11, mBuilder.build());
call function like this:
setDailyNotification(11, 11,00, 00);
System.currentTimeMillis()- gets the current time from the device.
calendar.setTimeInMillis()- sets the current time.
calendar.add(Calendar.SECOND, 10) - adds ten seconds to the current time.
In total, you are fetching the current time of the device and adding ten seconds in it.
You can try this Worked for me
int interval = 1000 * 60 * 1;
// 1 for minutes ,you can pass 60*24
//And set alarm manager like this
Intent alarmIntent = new Intent(AlaramManagerActivity.this, NotificationReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(AlaramManagerActivity.this, RQS_1, alarmIntent, 0);
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),interval, pendingIntent);
Intent myIntent = new Intent(activity.this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(Splash.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar firingCal= Calendar.getInstance();
Calendar currentCal = Calendar.getInstance();
firingCal.set(Calendar.HOUR,11); // At the hour you wanna fire
firingCal.set(Calendar.MINUTE, 0); // Particular minute
firingCal.set(Calendar.SECOND, 0); // particular second
long intendedTime = firingCal.getTimeInMillis();
long currentTime = currentCal.getTimeInMillis();
if(intendedTime >= currentTime){
// you can add buffer time too here to ignore some small differences in milliseconds
// set from today
alarmManager.setRepeating(AlarmManager.RTC, intendedTime, AlarmManager.INTERVAL_DAY, pendingIntent);
}
else{
// set from next day
// you might consider using calendar.add() for adding one day to the current day
firingCal.add(Calendar.DAY_OF_MONTH, 1);
intendedTime = firingCal.getTimeInMillis();
alarmManager.setRepeating(AlarmManager.RTC, intendedTime, AlarmManager.INTERVAL_DAY, pendingIntent);}

Repeating alarm wont work in Android

User can set their own repeat interval, for example he/she selected 5 minutes to be reminded of the new goal she set. The reminder will start on the goal's start date which is also set by the user.
No problem with setting goal, and setting the repeat interval. The problem is it wont work.
What i would like to happen: This is an example
Goal 1 starts tomorrow. User will get reminder of Goal 1 in every 1 hour tomorrow.
Here's my code:
public void setReminder(){
List<Goals> oneGoal = dbhandler.getLatestGoal(goal_id);
for (final Goals goals : oneGoal) {
if (repeat.isChecked()) {
long futureInMillis = 0;
dbhandler.updateReminders("true",choiceNumber,choiceRepeat,goal_id);
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.set(Calendar.DATE,Integer.parseInt(goals.getSDay())); //1-31
cal.set(Calendar.MONTH,Integer.parseInt(goals.getSMonth())-1); //first month is 0!!! January is zero!!!
cal.set(Calendar.YEAR, Integer.parseInt(goals.getSYear()));//year...
//assigned a unique id to notifications
Random random = new Random();
int m = random.nextInt(9999 - 1000) + 1000;
//Create a new PendingIntent and add it to the AlarmManager
Intent intent3 = new Intent(this, TimeAlarm.class);
intent3.putExtra("goalid", Integer.toString(goal_id));
PendingIntent pendingIntent = PendingIntent.getActivity(this,
goals.getGoalId(), intent3, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am =
(AlarmManager) getSystemService(Activity.ALARM_SERVICE);
if (choiceRepeat.equalsIgnoreCase("Seconds")) {
am.setRepeating(AlarmManager.RTC_WAKEUP, SystemClock.elapsedRealtime(), 1000 * choiceNumber,
pendingIntent);
} else if (choiceRepeat.equalsIgnoreCase("Minutes")) {
am.setRepeating(AlarmManager.RTC_WAKEUP, SystemClock.elapsedRealtime(), 1000 * 60 * choiceNumber,
pendingIntent);
} else if (choiceRepeat.equalsIgnoreCase("Hours")) {
am.setRepeating(AlarmManager.RTC_WAKEUP, SystemClock.elapsedRealtime(), 1000 * 60 * 60 * choiceNumber,
pendingIntent);
}
MessageTo.message(SetReminderActivity.this, "You will be reminded every "+choiceNumber+" "+choiceRepeat+" for the new goal.");
//am.cancel(pendingIntent);
}else{
MessageTo.message(SetReminderActivity.this, "You've chosen not to set reminder for the new goal.");
}
}
}
TimeAlarm.java // for the notifications
public class TimeAlarm extends BroadcastReceiver {
NotificationManager nm;
MyDBAdapter dbhandler;
#Override
public void onReceive(Context context, Intent intent) {
int goal_id = Integer.parseInt(intent.getStringExtra("goalid"));
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
new Intent(), 0);
//assigned a unique id to notifications
Random random = new Random();
int m = random.nextInt(9999 - 1000) + 1000;
List<Goals> oneGoal = dbhandler.getLatestGoal(goal_id);
for (final Goals goals : oneGoal) {
Notification mNotification = new Notification.Builder(context)
.setContentTitle("A Reminder from GSO")
.setContentText(goals.getGoalName())
.setSubText(goals.getStartDate() + " - " + goals.getEndDate())
.setSmallIcon(R.drawable.gsoicon)
.setContentIntent(contentIntent)
.setSound(soundUri)
.build();
nm = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
// If you want to hide the notification after it was selected, do the code below
mNotification.flags |= Notification.FLAG_AUTO_CANCEL;
nm.notify(m, mNotification);
}
}
}
in Android Manifest:
<receiver android:name=".TimeAlarm" />
I can't tell what wrong with my code. Pls. help.
You are only setting the alarm using AlarmManager's set() method. You should use setRepeating() method for repeating the alarm events.
So, your below line
am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent);
should be replaced with
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent);
You can also refer this Example : Create Repeating Alarm .
Example to repeat event on every two minutes
AlarmManager am = (AlarmManager)getSystemService(Activity.ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(),2*60*60,pendingIntent);
You have to declare the receiver in your AndroidManifest.xml
<receiver android:name="TimeAlarm" >
See: http://developer.android.com/guide/topics/manifest/receiver-element.html

Multiple alarms not working properly Android

Here is what I tried in my code. Multiple alarms not working properly Android
AlarmManager alarmManager = ((AlarmManager) getSystemService(Context.ALARM_SERVICE));
int time1 = Integer.parseInt(e.getText().toString());
time1 = time1 * 1000;
final int id = (int) System.currentTimeMillis();
Intent intentAlarm = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, id, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT);
long r4 = time1 + System.currentTimeMillis();
alarmManager.set(AlarmManager.RTC_WAKEUP, r4, pendingIntent);
Anyone has any idea what is wrong,
some help will be appreciated, thank you:)
How you are assigning time to trigger alarm alarmManager.set(AlarmManager.RTC_WAKEUP,r4, pendingIntent); is wrong.
The time in seconds you get from user is needed to set in Calendar and then assign that time in the alarmManager.set();
Calendar cal = Calendar.getInstance();
cal.set(Calendar.SECOND, time1);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
Your code would be like,
AlarmManager alarmManager;
alarmManager=( (AlarmManager)getSystemService(Context.ALARM_SERVICE));
int time1 = Integer.parseInt(e.getText().toString());
Calendar cal = Calendar.getInstance();
cal.set(Calendar.SECOND, time1);
final int _id = (int) System.currentTimeMillis();
Intent intentAlarm = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent=PendingIntent.getBroadcast(this,_id , intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);

Android AlarmManager to Trigger After One Hour

I am trying to do something really simple! I have a sessionTimeout variable which have the value 3600000 milliseconds which means 60 minutes/1 hour. I need to set the alarm after 60 minutes. I am using the following code but calendar.getTimeInMillis gives me a very high value. If I just pass 3600000 in the alarmManager it still does not work and trigger the alarm receiver instantly.
private void setupSessionTimeoutAlarm()
{
Calendar calendar = Calendar.getInstance();
calendar.add(calendar.MILLISECOND,(int) sessionTimeout);
long timeInMilliSeconds = calendar.getTimeInMillis() + sessionTimeout;
// schedule the alarm
Intent myIntent = new Intent(Gateway.this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(Gateway.this, 0, myIntent,0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP,timeInMilliSeconds,pendingIntent);
}
Calendar calendar = Calendar.getInstance();
Date date = new Date();
date.setTime(System.currentTimeMillis() + (60 * 60 * 1000));
calendar.setTime(date);
Log.i(TAG, "start: " + calendar.getTime().getMinutes());
Log.i(TAG, "start: " + calendar.getTime().getHours());
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, A3LocationUpdateReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, Configuration.LocationUpdateEveryHoursRequest, intent, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_HOUR,
pendingIntent);

Categories

Resources