set date and time from respective pickers to set alarm - android

I preparing an app like "To-Do List" in which user sets alarm to its task. Now the problem is that i don't know how to set alarm using date and time pickers.
The working i need is that user first selects date and then time. This date and time is to be used to set Notification on this specified date and time. And after clicking on that notification, the activity that contains whole to do list starts. In that activity, once the to do task is marked finish, the alarm must be removed from that specific date and time.
Please someone help me. I am trying this for the first time. Please help me learning this.
PS: The work i've done till now is, i have the prepared an activity that takes necessary details about task such as task brief, task priority, task date task time, and i am storing it in database. I can successfully mark the task done / undone. The work i'm remaining with is setting the Notification about that task.

find the difference between the current time and the time at which the work id to be done in milliseconds and set it to alarm.
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * diffInMillis , pi);
Hope it helps

Related

Developing reminder application in Android

I'm creating a reminder application that will show notifications on scheduled time, I'll be storing reminder related data in a database and created the working database and content provider for it, initially I thought of creating an alarm whenever user creates a new reminder, but now I'm not able to fire the notification whenever the alarm triggers, and don't know how can I retrieve the reminder related info from the database, and for repeating reminders like daily or weekly I want to reschedule the same alarm after it triggers as the android developer docs suggested so, but don't have the exact idea of it. Please guide me through this which approach will be better and is there any other better way to do it, and any working demo will be very helpful.
This seems like it is quite a few questions bundled up into one. I would suggest searching for each individual piece as all of the answers to your questions are on this site.
In any case, I copied over an answer on how to set a repeating alarm
AlarmManager am = (AlarmManager) ct.getSystemService(Context.ALARM_SERVICE);
Intent intent1 = new Intent(CurrentActivity.this, Alarm.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(ct, 0,intent1, PendingIntent.FLAG_CANCEL_CURRENT);
Date curr=new Date();
curr.setHours(h);
curr.setMinutes(m);
c.setTime(curr);
c.set(Calendar.SECOND, 0);
Calendar c1 = Calendar.getInstance();
am.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
With this code an intent will be fired every day that calls Alarm.class. It is up to you to figure out if you need a service, intent, or anything else as Alarm.class, as it is unclear what the alarm needs to do based on your description.

Android: which kind of Broadcast/Receiver combination should I use?

In my app, I want to kick off an timer that triggers an action every x minutes, regardless if the user is currently in the app or not. I have been reading around and am not sure which combo of Broadcast and Receiver types I should use - any guidance would be helpful.
Example of user actions:
User hits a button, sets initial timer (alarm)
Timer is reached, trigger an action and set the timer again
Repeat until it has run for x minutes
when the user hits button set alarm as
AlarmManager am = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
am.setRepeating (AlarmManager.Type,
long triggeringtime,
long interval,
PendingIntent operation);
here triggeringtime is how to time it shud take to take an action
and interval how to much time it would take to do the same.
here operation is the Intent which you need to execute like it may be an activity or Service you can Define it as
operation = PendingIntent.getActivity( context, 0, intent or service, 0);
2 and 3 step will be continuously run thats what the alarm manager does.
This alarm continues repeating until explicitly removed with
cancel(AlarmManager.OnAlarmListener).
I am a beginner Sry if i wrong. Hope it helps!
Android JobScheduler
You can find a lot of tutorial online.

How to test Alarm Manager which set for a INTERVAL_DAY

I written a small application which invokes a service using AlarmManager with interval of the day. To test this app, i am using emulator 5.1 . When i set manually date and time(for current date or future date time) the AlarmManager invokes service for that day. When i am trying to change the date for checking whether it will work for other day by changing date then its not invoking service.
Here is my code.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(calendar.HOUR_OF_DAY,13);
calendar.set(calendar.MINUTE,20);
calendar.set(Calendar.SECOND,10);
alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this,BackgroundService.class);
pendingIntent = PendingIntent.getService(this, 0, intent, 0);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingIntent);
Toast.makeText(this, "Alarm Set", Toast.LENGTH_SHORT).show();
BackgroundService is service which popup with a Toast .
I have 2 issues.
How can test AlarmManager , which we set with Interval of a day?
The Alarm Manager is not exactly invoking at the second. For example , i set the alarm at 13:20:10 but its coming at 13:20:some Random second
Your help is much appreciated.
Thank you.
RTC (Real Time Clock) is the clock that you can adjust, so you could possibly just change the current time programatically for a test case. See this answer
You're using a alarmManager.setInexactRepeating method, which doesn't guarantee an exact time of alarm delivery. For more precise timings, use alarmManager.setRepeating, however keep in mind that it's not really recommended, as most use cases do not require exact timings and it will cause higher battery usage. See inexactRepeating and repeating

Android: Alarm to be play every 30 minutes and it start from 12:30

Here i am going to use the alarm service to play the alarm at every 30 minutes. Right now i have set it to play it at every 10 second from the Every start.
Here is the Code:
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_layout);
Intent myIntent = new Intent(SettingsActivity.this, MyAlarmService.class);
pendingIntent = PendingIntent.getService(SettingsActivity.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 10*1000, pendingIntent);
}
Now the Problem is, I want to start the alarm from the 12:30 not from the time application start and it should repeatedly play at evert 30 minutes. like 1:00, 1:30, 2:00 . . . etc
So what changes i have to do in my code ?
To start a service at specific time.
see this post maybe helpful:
Using Alarmmanager to start a service at specific time
Set your initial alarm time for 12:30 using the Set method.
When the alarm fires, then set up your next alarm time and keep doing that until you don't want the alarm any more.
You don't need a service to do such a simple task. AlarmManager is more than capable of handling this.
Try it
And Use first time -
Try another
A naive approach would be:
Start a context that will always run, such as a Thread in a Service.
Calculate the timestamp of the time you want the next alert to ring using Date and put it in "long alertTimestamp".
In a loop, calculate the timestamp right now using Date and put it in "long nowTimestamp" .
If nowTimestamp < alertTimestamp, put the thread to sleep for (alertTimestamp - nowTimestamp).
Else sound the alert and recalculate alertTimestamp, repeat.
Make sure you catch notify interruptions to the tread gracefully, that is the key to aborting the timer.
Hope this was helpful.
For repeating every 30 minutes you will need to add this to your code:
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000*60*30, pendingIntent); //millisecs*seconds*minutes
But I haven't figure out how to start at specific time yet.

two alarms set at same time

in my application users are free to select time and date to set alarms .
What will happen if user choose exactly the same date and time for two alarms.
I am taking input from user(for date and time) and setting the alarm.
GregorianCalendar gc=new GregorianCalendar();
gc.set(2012, 1, 22, 11, 19,0);//values as given by the user
final Intent intent = new Intent(this, AlarmService.class);
gc.set(Calendar.AM_PM,0);
// final PendingIntent sender = PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
am.set(AlarmManager.RTC_WAKEUP, gc.getTimeInMillis(),PendingIntent.getBroadcast(this,1, intent, PendingIntent.FLAG_UPDATE_CURRENT));
I have used a broadcastreceiver to recieve the alarm broadcast.
How should I handle this situation. What should I prefer to do?
I want to know the tecchnical aspect as what happens in this situation.
AFAIK,it won't create any technical problem for alarms set for the same time.It will fire all alarms at the same time.
It depends upon you how you want your user to set alarm.If you want them to set same time for various alarms,it's not a problem.
But if you don't want them to repeat the alarm time once it is set for other,then you can store alarms' time in database and at the time of setting new one,you can check for the conflict of newly set alarm time with previously set alarms from database and reject if it is found same.

Categories

Resources