tags:

views:

62

answers:

1

I am a new to developing for Android and have been reading up and researching in order to become more familiar with the platform. My programming knowledge is moderate I have experience with C++ and a fairly proficient with Actionscript 3.0. I picked up programming primary to work on game development but come from a more artistic/design background.

I have a relatively simple idea for an app that I want to approach from the most practical way possible seeing how it’s my first attempt. Essentially the core functionality of the app will be drawing information I will be getting from an ephemeris chart. At a certain time in the day which is based on the data from the chart I want to change an image/display information based on that day. I am trying to wrap my head around the best possible way to do this. Also I would like to give the user an option of having a notification of some kind when the change occurs, but that is something I have come across in terms of basic implementation.

So my question is if you were to approach setting something like this up how would you? What I am not sure about is setting up a time element so the application knows when to change the image/data to display. If you took the time to even read this I really appreciate it. Wade.

A: 

It really depends on where/when/how this image is displayed. I will assume that the image is shown in a normal Activity:

Look into using the AlarmManager to set alarms. These alarms could start a Service that changes the image source (perhaps updating a db entry of the image resource or filename). Once the work is complete in the Service, send a Broadcast (or StickyBroadcast) with the results in a Bundle using Intent.putExtra(). You could also set the status bar notification at this point using the NotificationManager. The PendingIntent of the Notification should be the Activity that shows the image.

Check out the "Alarm" and "Notification" sections of the APIDemos (/samples/android-8/ApiDemos/src/com/example/android/apis/app)

UPDATE: A more robust Alarm system is Mark Murphy's WakefulIntentService found here.

Then, register a BroadcastReceiver in that Activity, listening for the Broadcast Intent that you set in the Service. This will listen for when the service completes. If you use a StickyBroadcast in your Service, the data will be cached, making it available when the Activity comes to the front of the stack. Otherwise, you will need to persist the data (in an SQLite db or SharedPrefernces) and use a timestamp.

UPDATE - Demonstrate persistent data storage:

A great place to start with learning SQLite is the Android Note Pad demo. This will teach you many of the key aspects of Android including life-cycles, ContentProviders (db wrappers), and using Android Views (widgets).

The alternative I mentioned is using the Application's default SharedPreferences. A simple example of usage is this:

String imageUri = null;
long timestamp = 0;
final String currentImage = "current_image";
final String lastTimestamp = "last_timestamp"

// Access the default SharedPreferences
SharedPreferences preferences = 
    PreferenceManager.getDefaultSharedPreferences(this);
// The SharedPreferences editor - must use commit() to submit changes
SharedPreferences.Editor editor = preferences.edit();

// Get the current image URI
if (preferences.contains(currentImage) {
    imageUri = preferences.getString(currentImage, null);
}
// Get the last timestamp
if (preferences.contains(lastTimestamp) {
    timestamp = preferences.getLong(lastTimestamp, 0);
} 

// To set the SharedPreferences
editor.putString(currentImage, imageUri);
editor.putLong(lastTimestamp, timestamp);
editor.commit();

Hope I understand you correctly, and that this helps.

iPaulPro
Thanks for the reply Paul. I am pretty sure I'm following your breakdown. I will look into the alarm and notification sections as you advised. Something Id like to trouble you to elaborate on is persisting the data and using a timestamp.
Wade