How to make a memo reminder of date&time in android - android

Well I am trying to develop a memo or notebook app with a new feature of reminding the users in certain date and time they set.
I used datepicker and timepicker to pick up date and time, and I use alarmmanager to set an alarm to notify the users, but I don't know how to check the date, could anyone help me?
THX
PS, is there any opensource android apps about it? I would like to know

You can use AlarmManager class to schedule reminders. It allow you to schedule your application to be run at some point in the future. Here's a sample code to get you started:
Intent intent = new Intent(this, YourMemoService.class);
PendingIntent pending = PendingIntent.getService(this, 0, intent, 0);
AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
alarm.set(AlarmManager.RTC_WAKEUP, Time_To_wake, pending);

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.

Calendar instance in android for AlarmManager

I am developing an app which displays notifications by using AlarmManager.
For that I'm taking the user input values for hour, minute and second.
Something like:
int hour = 4;
int min = 40;
int sec =36
Calendar Calendar_Object = Calendar.getInstance();
Calendar_Object.set(Calendar.HOUR, hour);
Calendar_Object.set(Calendar.MINUTE, min);
Calendar_Object.set(Calendar.SECOND, sec);
Intent myIntent = new Intent(MyView.this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MyView.this,0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, Calendar_Object.getTimeInMillis(), myIntent);
Notifications and the rest of the code work fine, but the problem is that instead of 4:40:36 the notifications get invoked on the current time(as soon as i run/debug the app).
I think there is some problem in Calender_Object part.
Looking for a solution.
Thanks in advance.
Please note: Alarms will be executed immediately, if the notification time has elapsed already.
As a workaround you might want to consider a date part too. Or just a variable in memory which acts as a boolean if the time has elapsed or not.
From the Docs:
If the stated trigger time is in the past, the alarm will be triggered immediately. If there is already an alarm for this Intent scheduled (with the equality of two intents being defined by filterEquals(Intent)), then it will be removed and replaced by this one.
Also please consider the API level 19 version of how AlarmManager works.

Best way to set multiple alarms when some are of repeating in nature in Android

I'm building a reminders application where one time, weekly, monthly reminders and we notify the user of the reminder on due date and time. Reminders can be updated any time by the user to update the reminding time or delete the reminder altogether. I have thought of two ways I can solve the particular problem.
Whenever user sets a reminder, schedule an alarm accordingly with an unique Id and update or delete it in case user updates or deletes the alarm.
Since I store the reminding time in DB, better approach would be to schedule an alarm for the nearest reminder. And have the Service which is triggered by the alarm schedule a new alarm for the next nearest reminder.
2nd approach seems clean approach but how do we tackle the case where the Service triggered by alarm gets killed by the system before it schedules a new alarm for the next reminder?
Edit
Looks like if the system kills a Service for memory, it will re-create the Service. Does it mean it is safe to rely on the Service to schedule alarm every time it is run?
Edit 2
I've realized that Android kills any alarms whenever the device is restarted. This makes approach 2 a better solution. I've implemented it for now.
The pending intent needs to be created exactly in the same way for canceling the already set alarm. You can cancel previous alarm when you set a new alarm, if you want to have a single alarm activated at some time.
For setting alarm use:
Intent myIntent = new Intent(getApplicationContext(), SessionReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 1, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, now.getTimeInMillis(), pendingIntent);
For canceling the alarm use:
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent myIntent = new Intent(getApplicationContext(), SessionReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 1, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.cancel(pendingIntent);

Starting alarm service in android

In my application i want to use alarm service for specific period of time.I'm taking start time and end time values from user and saving it in database,Now i want to start a alarm service at start time and alarm should go off at end time specified by user.I'm new to this topic and not able to understand how to implement this...Any help will be appreciated.Thank u..
This is how you implement an alarm manager. But you will need to read about Calendar object in android also.
String alarm = Context.ALARM_SERVICE;
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 8);//Just an example setting the alarm for the 8th hour of a day.
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND,0);
calendar.set(Calendar.MILLISECOND, 0);
AlarmManager am = (AlarmManager)getActivity().getSystemService(alarm);
//This is the intent that is launched when the alarm goes off.
Intent intent = new Intent("WAKE_UP");
PendingIntent sender = PendingIntent.getBroadcast(getActivity(), 0, intent, 0);
//If the user wants the alarm to repeat then use AlarmManager.setRepeating if they just want it one time use AlarmManager.set().
am.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis() , AlarmManager.INTERVAL_DAY, sender);
}
Also you will need to register a BroadCast Receiver to except the intent when the alarm sets it off.
You create the BroadCast reciever and register it in your manifest to receive the intent from the alarm.
http://www.vogella.de/articles/AndroidServices/article.html
Here is a great tutorial to help you understand better
The key is to use the AlarmManager with a pending intent.
mAlarmSender = PendingIntent.getService(AlarmService.this,
0, new Intent(AlarmService.this, AlarmService_Service.class), 0);
Then you create the AlarmManager from the current context:
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
And schedule the previously created pending intent.
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
firstTime, 30*1000, mAlarmSender);
On schedule the AlarmService_Service service will be called, or you can put another intent like open a specific activity.
Here is the complete example of how you can schedule an alarm: AlarmService.java

make an android app autostart

Is it possible to make an installed app auto start itself?
thanks
Read this article if you want to start an application on boot up.
Yes .
For this you have to use Alarm Notification .
You can found many tutorial for this on Net. Just google it..
You will found code something like this...
Intent intent = new Intent(FirstScreen.this, MyBroadcastListener.class);
PendingIntent sender = PendingIntent.getBroadcast(FirstScreen.this,
0, intent, 0);
// We want the alarm to go off 30 seconds from now.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 30);
// Schedule the alarm!
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
The BroadCastListener You will set can call any activity in return. of own app too..:)
Auto Start on any event? Check the IntentFilter out to make your app default for particular actions.
Auto Start on/after a particular time? check Alarm out!

Categories

Resources