Set alarm automomatically when schedule is saved from the DB - android

I have created a database for schedules, fields are (id integer primary, subject text, description text, DueDateTime DATETIME)
I was able to save the values inserted on the EditTexts and Textview to the database, but I wasn't able to create an alarm for that based on the DueDateTime value. I don't quite understand on how and when should I use the intent service, broadcastreceiver, alarm manager etc.
My goal is to set an alarm for the schedule saved in the database when is on due An activity will popup like this on the screen even if the app is closed or when the device is re-booted.
These are pictures. The character is a picture only and the balloon is also a picture but in there Subject name of the particular data will be written and also the these pictures are clickable, when clicked anywhere from the pictures it will be closed
Ok I found this code https://dhimitraq.wordpress.com/tag/multiple-alarms/ which could (maybe) resolve my problem but I don't understand how he use it, so I can't relate it to my app since he has a lot of things he added (some of are hard-codded) which I don't need much of them, he even added a link to download his sample.

You want to schedule an alarm manager with due date?
You can schedule in the following way.
Code :
get (Due_Date_in_milli_sec - current_date_time_in_milli_sec)
now schedule an alarm manager with resultant milliseconds.in case of system bootup you will need to restart alarm manager again.

Related

How do I set multiple notifications using one date selection?

I need to set multiple notifications at the same time based on a list of mutiple choice questions. I will figure the radio buttons and if statements out later, for now I'm just setting the dates randomly for testing purposes.
If the user selects jan 1st and clicks a button. It will set a notification in the future with a reminder to do an activity. It also saves this information so the user can edit the details if he desires or change a few options.
This part I already have coded and it works just fine. however, It only sets a single notification currently. It is a pretty involved code consisting database, receivers, fragments, etc. that all talk to each other.
As I said, this part I have working just fine but I am not including all the code because it is seriously involved and no one would try to break it down if i drown you in a sea of code. I can certainly post specific code if someone request it.
My issue is that I need it to set a good amount of notifications at various future dates upon the button click, not just one. I need to change the notification message to a preset variable string for each additional notification event but some things like title will remain the same.
my current working code executes like this....
User Selects Jan 1 > User Clicks Button > Notification set for Feb 14 with a unique title and message set by the user and saved for future editing...
At the same time the notifications are set they are saved so the user can change the date and a few options if needed. I want only one title and a single date saved. I have the save feature already working but I need to know how to link the additional reminders to the existing saved item. Im trying to make all the data linked so that if the user deletes the saved item, all the set notifications for that file are deleted and not just the one that falls on the user selected date.
===== This is what I am trying to pull off
User Picks Some Options and Clicks A Button [I have this working already]
Upon Click of said button, the following notifications are all set in the future :
[currently it sets this notification only]
Jan 1 notification :
(title)Day Master App
(message)Happy New Year
[Im trying to figure out how to add the following notifications upon the above button click and also save this information to the existing db under the same item]
Feb 14 notification : (title)Day Master App - (message)Its V Day
March 1 notification : (title)Day Master App - (message)Spring Is Near
March 14 notification : (title)Day Master App - (message)Its probably raining
(((and we'll just pretend i finished the list...)))
I cant make the future notifications a static number because when the future notifications are set is determined by a bunch of radio button choices before the user clicks the execute button. This is going to be a pretty complex and hacky if/else novel the way I think I have to do it. Am I correct?
I have a display/edit listview that shows your saved notifications and the unique name and date the user set. This works fine currently but only sets a single notification based on user input.
I need to add some more notifications but i dont need to save them under a different item or name. I want them all under the same save item so all the notifications that were set when the button was clicked can be added and deleted as a group. There will be no option to delete certain notifications that were set. It will be all or none.
I would imagine I could just add some more variables into the existing "save notification" code? As in piggyback some more items (like all the dates and messages) for the additional notifications? do i need to write a new function for each future notification I set in order to be able to delete it?
do I need to create a new db for each additional notification that is set? A separate Adapter? Im so confused...
===
Im not looking for a code example exactly, I want to know how this would be implemented into an existing code. I realize there is probably 100 ways to code what I have described. I just need the process explained.
Please explain this to me slowly. I know im way overthinking this.
I tried to explain this as best I could, if you need clarification on something please ask. Thank You.
I have solved my problem although I could not accomplish exactly what I was trying to originally.
I managed to set the multiple notifications on a single click with different dates by simply copying the same code I used to display the single notification and simply giving each additional notification a unique ID and creating a new variables to give them individual text, future dates, separate times, etc. These variables can be easily set programmaticly or by the user when you set them up.
// On clicking the set notifications button
public void SetNotificationsButton(View v){
ReminderDatabase rb = new ReminderDatabase(this);
// Creating Original Reminder
int ID = rb.addReminder(new Reminder(mTitle,
mDate, mTime, mRepeat, mRepeatNo, mRepeatType, mActive));
// Create Feeding Notification
int FeedingID = rb.addReminder(new Reminder("Have you fed the cat today?",
mFeedingDate, mFeedingTime, mRepeat, mRepeatNo, mRepeatType, mActive));
}
//
// and just continued copy and pasting the rest of the notification events
// changing the ID for each individual notification. Without a unique ID
// the current notification will override the previous notification and
// appear to only set the last reminder.
I was not able to remove all notifications by selecting a single list item, I probably could have figured this out by grouping the notifications into a separate variable but once I realized that you can only have a maximum of 50 notifications set at any given time it seemed like overkill.
Since I have a repeat option for certain notifications I can keep my notifications set; numbers low and stay away from the 50 at a time limit while still firing a reminder to the user every day.
I found a great example for setting single notifications I used as reference.
https://github.com/blanyal/Remindly
Thank those of you who took the time to read my question.

Android widget, how to show the next value from the database on a touch event?

in my application I have a database, that I access through a custom ContentProvider, with the following columns:
id_widget = the appWidgetId of the widget I would like to show the values on
id_item = the id of a value in another table, which I'll get with a SQL JOIN query
name_item = the name of the item, assigned by me through the widget configuration activity that I've created.
An example of the table could be something like this:
id_widget | id_item | nameĀ­_item
----------+---------+----------
54 | 1 | avaible
54 | 2 | used
58 | 1 | left
58 | 3 | avaible2
58 | 5 | old
My widgets should show just the first value stored, but when pressed, I'd like them to show the next value and so on until the last one, then they should move to first again.
So for example, the widget which has appWidgetId = 58, should show "left" (and another value that I'll get from another table using an SQL JOIN QUERY. Those values are parsed from a site through a IntentService, and saved in another table). When touched, it should show the value "avaible2", then after another touch "old" and if pressed once again it should move to the first value, "left".
The first idea that I've got was to query the database in order to get all the values, and store them in some Collections inside the AppWidgetProvider, in order to use them in the onUpdate method. That way I wouldn't have to query the database everytime that i touched the widget. That was until i read the doc on AppWidgetProvider and found out how it actually works and the fact that I don't always have the same instance.
I've been searching for two days for another solution, but I still haven't found anything. The only idea that I have got is the following:
- keep track of which row of the table I'm showing (maybe by adding another column to the table)
- query the database for the next row everytime that I touch a widget.
Is it efficient enough? Are there any other ways to solve my problem?
I apologize for my English, but as this is not my first language, this is the best I could do.
Thanks in advance
First off, your English is great! Wouldn't even have known it wasn't your native language if you hadn't mentioned it. :)
As you already pointed out in your question, AppWidgetProviders are stateless: you're not guaranteed (in fact, it is unlikely) that you will get the same instance of your AppWidgetProvider class. They are a special type of BroadcastReceiver, which only exists as long as it needs to in order to process the event it is listening for. See this section, in particular.
The only way to maintain a widget's state/data is to store the needed information persistently, and in Android, that means writing to a file, database, or the app's SharedPreferences (which ends up being a file as well). So you have a couple options with that, but seeing as you already have a database/ContentProvider set up, it would probably make sense to simply add another column, like you mentioned. Ultimately, it needs to be saved somewhere, so even though it's better in general to minimize the number of file/database reads/writes, it is necessary to do so in this case. You could display a ProgressBar on the widget until the query completes, and then display the updated information, especially if you find that the database queries are taking a long time to execute.
Just for completeness's sake... some widgets use a background Service to maintain and/or update a widget's state. But on its own, this will obviously not persist the widget's state/data when the device is turned off, or in the event that the service is stopped for whatever reason. I don't think this would work well for your situation.
All that to say, based on my knowledge of widgets, I agree with the approach you've suggested!
This isn't directly related to your question about handling data, but I found this sample app by Commonsware very helpful in handling widget clicks when I was getting started.

find out 1st records after installation and boot

My app is a location tracker that finds out location of a device at every 3 min. It retrives the latitude and longitude and stores it in a sqlite db. How can I find out the 1st record after installation, and 1st record after boot.
Example, I will make one more column in db for record_type. there I will store the status for code as:
installed, onboot, regular. How can this be achived.
For the first record after installation, you could just check the value of a SharedPreference, and then set the preference. For example, if "sp" is your SharedPreferences object, and you do:
boolean firstRecord = sp.getBoolean("firstRecord", true);
sp.edit().putBoolean("firstRecord", false).commit();
Then sp will contain "true" the first time you execute this code, and false the rest of the time (assuming the user does not manually clear the app's data).
For the first record after boot, you could use a BroadcastReceiver that receives the ACTION_BOOT_COMPLETED event. This way you can specify code that will be executed everytime the device is restarted. When you get this event, you could also work with the SharedPreferences by doing something like (sp being your SharedPreferences object):
sp.edit().putBoolean("justBooted", true).commit();
so if you do:
boolean firstRecordAfterBoot = sp.getBoolean("justBooted", false);
sp.edit().putBoolean("justBooted", false).commit();
you will know that this is the first time you execute this code after booting.

Alarm broadcaster, pass object or pass id and pull object from sqlite?

I need your recommendation on something. In a nutshell, I am setting up an alarm (task reminder kind of app) in which upon expiry, the broadcaster puts a notification with the task info in the notification bar. The task information are storied in 2 objects.
Should I:
1) put the task info in an intent (serialized) in which I would pull it up upon alarm expiry from the passed in intent and display the relevent info?
2) Don't pass the two objects in the intent, just pass an ID, and upon expiry I would pull the two object from my SQL lite db to display them?
which one is more efficient, better way to do it?
Thank you so much
I'd just pass the id. That will save you a lot of trouble with serialization and deserialization.

Android application structure issue

I'm working on application and I need some suggestions which is the best way to code it for Android. Basically it's something like event guide for a few cities. Imagine this :
I have an activity with 7 different buttons (7 different cities) and clicking on one of these buttons I'm opening a new activity where I have all months (January-December) as buttons again. If there is some event in February for example in the chosen city the button will be active and I will go to another activity where I have a list with the events. So my idea is to do it in this way :
Create one Activity with all Cities
Create one Calendar activity with all months.
When user select for example Paris, I'll send an extra via intent with an ID of the chosen city :
intent.putExtra("chosenCity", 2); //something like this
In the Calendar activity I will make active/inactive months buttons depending on that extra sent from Cities activity.
And when user chose a month I will send that chosenCity extra again to the ListViewEvents activity and populate the list view from Database (for example) depending on that extra.
So my question is : is it a good way to build an application like that? Or if it's not, which is the best way to achieve this?
Thanks in advance!
Sounds like you already know pretty much what you need to do. The only thing left is to implement it. The only thing I'll add is that your should probably store all your database and use a CursorAdapter to display them in the ListView. You can store all the events in the database and just create your select statement in such a way that it only selects the events you want to display at any given time. When using this CursorAdapter, I highly recommend you use the CursorLoader.

Categories

Resources