Remembering data in an Activity after close

Put your problem here if it does not fit any of the other categories.

Remembering data in an Activity after close

Postby tsucheck » Wed Feb 20, 2008 3:50 am

Hi,

I'm trying to determine the best methods for remembering information that user enters in an activity. Here is an example. User starts up the application. The activity pops up. User enters some data (eg. wake me up alarm in 2 hours). The activity sets and Alarm for 2 hours that will cause a popup message to the user.

But the user has an option to re-lunch the activity and cancel the alarm. In this case I would have to show to the user that an alarm is set. I've read about the SharedPreferences object and was thinking of using it. Or I could write the data to a DB (in case the application dies and everything is lost, not sure if SharedPreferences survives that, doubfull). This way I could tell the user that the application died and thats their last 2 hour alarm never got triggered.

Anyway, I was hoping that you guys would throw some suggestions as what's the best mechanism to use.

Thanks in advance.

Tom
tsucheck
Developer
Developer
 
Posts: 29
Joined: Sun Feb 17, 2008 2:07 am

Top

Postby res » Wed Feb 20, 2008 3:56 am

For anything where you need to remember information after the application has finished I would use a database. I haven't used SharedPreferences so can't comment on them.
res
Senior Developer
Senior Developer
 
Posts: 146
Joined: Tue Nov 20, 2007 8:14 pm
Location: United States

Postby chouputra » Wed Feb 20, 2008 4:02 am

If I am not mistaken SharedPreference last only during the application lifetime as I am using that right now and I am sure you would need to use the SQLLite fore that so that you can access it from the alarm setting application and the background application.
chouputra
Junior Developer
Junior Developer
 
Posts: 10
Joined: Wed Feb 20, 2008 3:44 am
Location: Jakarta, Indonesia

Postby tsucheck » Wed Feb 20, 2008 4:27 am

Thank you both. Thats kind of what I expected, just wanted to make sure there is no other way of doing this.
chouputra, I had a followup question to your respose. Looking at the API example code that comes with the emulator there are few Alarm examples and in them all I see is an Activity that makes use of the Alarm Service and sets the timer. So really there is only the "alarm setting application" and the timer, no background application. So I'm not sure what you were refering to. Perhaps just my example wasn't explained well. But I would like to know what you meant in case I'm missing something. I'm assuming that the alarm is kind of Intent Receiver, even though it doesn't say that in the documentation so perhaps thats the background application that you meant.

On a different note, there was one additional architectural decision I was debating. I was thinking of having a service that actually does the alarm setting and the activity bind to that service. So initially it would spawn the service and any subsequent re-lunches would just connect to it (if the alarm was set). If user just exits the activity without setting the alarm no service would be started.
tsucheck
Developer
Developer
 
Posts: 29
Joined: Sun Feb 17, 2008 2:07 am

Postby chouputra » Wed Feb 20, 2008 5:49 am

I have a quick look at the android.app.AlarmManager and you could simpy use

public void set(int type, long triggerAtTime, Intent intent)
or
public void setRepeating(int type, long triggerAtTime, long interval, Intent intent)

to schedule when it should wake your activity and register your intent receiver that will be receiving if the alarm does goes off. However, I am still not sure how to retrieve all the time that has been registered into the alarm manager. One thing to note that it will be reset if the cell phone is turned off and rebooted. I was thinking maybe you could store the time being set by the user with SQLite and re-register them once the device is turned on but I am still not sure how to do that yet.

tsucheck wrote:Thank you both. Thats kind of what I expected, just wanted to make sure there is no other way of doing this.
chouputra, I had a followup question to your respose. Looking at the API example code that comes with the emulator there are few Alarm examples and in them all I see is an Activity that makes use of the Alarm Service and sets the timer. So really there is only the "alarm setting application" and the timer, no background application. So I'm not sure what you were refering to. Perhaps just my example wasn't explained well. But I would like to know what you meant in case I'm missing something. I'm assuming that the alarm is kind of Intent Receiver, even though it doesn't say that in the documentation so perhaps thats the background application that you meant.

On a different note, there was one additional architectural decision I was debating. I was thinking of having a service that actually does the alarm setting and the activity bind to that service. So initially it would spawn the service and any subsequent re-lunches would just connect to it (if the alarm was set). If user just exits the activity without setting the alarm no service would be started.
chouputra
Junior Developer
Junior Developer
 
Posts: 10
Joined: Wed Feb 20, 2008 3:44 am
Location: Jakarta, Indonesia

Postby plusminus » Wed Feb 20, 2008 10:40 am

Hey guys,

that SharedPreferences last only during the runtime of an Application would be new for me.
You can use Files, SQLiteDatabases, SharedPreferences, ContentProviders or the Internet ;) to store data.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Postby tsucheck » Thu Feb 21, 2008 1:34 am

Thanks plusminus, interesting that SharedPreferences are persistent even if the application dies. Have you actually verified this? That would mean that they are implemented in persistent way, eg. file, db, etc or others you mentioned. And each application that uses them would have an entry. I wonder how the system would know to get rid of them if the application is removed from the phone, perhaps there will be a mechanism to installing/uninstalling that will handle that.

As to my other question. Thank for the info chouputra. I spend the better part of the evening yesterday reading about Alarms/Services/Activities and Intent Receivers and I agree with you. Here is what I came up with.

First of all (and you can correct me if I'm wrong here) if the application dies or the phone is reset/rebooted it would pretty much be up to the user to explicitly turn on any applications they want running. I haven't seen anything in the documentation where user can register their applications to be started on android startup. Even if they are just services.

So here is what I was thinking. User starts the activity for the first time. Activity checks that there is no service running (associated with the activity). User enters the time for the alarm to activate. Activity spawns a service. The service starts the alarm. User exits the activity. After a while and before the alarm goes off. User comes back to the activity. Activity looks for the service and finds it. It can retrieve the information that and alarm has been set already from a DB or perhaps from the service (so not persistent if service). It binds to the service. User decides to cancel the alarm. The activity cancels the service which cancels the IntentReceiver.

So you might ask whats the point of the service and why not just stick with the activity and the Alarm. The reason I though I might need it for is. Let say that the application allows the user to provide several alarms in one session. So one alarm at 10pm another at 11pm. So when the user enters the information and exits the activity, the service would be alerted by the first alarm when its terminated and then would start the second alarm when the time comes. The service could get the information about the second alarm from the DB or it could have been sent to it by the activity when it was spawned.
tsucheck
Developer
Developer
 
Posts: 29
Joined: Sun Feb 17, 2008 2:07 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 15 guests