A typical type of template is that you register a specific part of your application to “receive” or listen to specific intentions. Thus, your application can wake up at any arbitrary point in time, look at the intent of the call and decide how to handle it (whether it is launching a full application, displaying a dialog, or something else). One of the good things that come with this is to use AlarmManager to set up an alarm that will launch and run your application in the future (periodic updates).
Does this answer your question? I did something similar in my application and I can help you with the code if you want.
EDIT , to implement this, you must create a java class that extends BroadcastReceiver or IntentService (depending on whether you want the class to work in the ui stream or as a service, respectively). In my example below, I defined my own action key, but usually you test the action using the .getAction () intent:
public class QueryService extends IntentService { public final static String SERVICE_NAME = "QueryService"; // incoming flags public final static String FLAG_ACTION = "R_ACTION"; public final static String FLAG_EVENTS_RETURNED = "R_EVENTS"; public final static String FLAG_EVENTS_ARCHIVED_RETURNED = "R_EVENTS_ARCH"; public final static String FLAG_SHARED_PREFERNCES_RETURNED = "R_PREFS"; // outgoing flags public final static String RETURN_EVENTS = "RETURN_E"; public final static String RETURN_EVENTS_ARCHIVED = "RETURN_E_A"; public final static String RETURN_SHARED_PREFS = "RETURN_S_P"; public QueryService() { super(SERVICE_NAME); } protected void onHandleIntent(Intent intent) { String rAction = intent.getStringExtra(FLAG_ACTION); boolean rEvents = intent.getBooleanExtra(FLAG_EVENTS_RETURNED, true); boolean rEventsArchived = intent.getBooleanExtra(FLAG_EVENTS_ARCHIVED_RETURNED, true); boolean rSharedPrefs = intent.getBooleanExtra(FLAG_SHARED_PREFERNCES_RETURNED, true); if(rAction == null){ Log.e(SERVICE_NAME, "no return action specified, exiting..."); return; } Log.i(SERVICE_NAME, "Caller: " + rAction); DroidTaskApplication app = (DroidTaskApplication)getApplicationContext(); Intent resultsIntent = new Intent(rAction); // TODO assembling events / archived events from a database needs // a function that gets the complete event instead of just its headers // assemble event objects and insert them if(rEvents){ List<Event> liteEvents = app.edo.getAllEvents(); if(liteEvents != null){ for(Event e : liteEvents){ int id = e.getId(); e.setAlarms(app.edo.getAlarmsById(id)); e.setSubtasks(app.edo.getSubtasksById(id)); e.setNotes(app.edo.getNotesById(id)); } } resultsIntent.putExtra(RETURN_EVENTS, (Serializable)liteEvents); } // assemble archived event objects and insert if(rEventsArchived){ List<Event> liteEventsA = app.edo.getAllArchivedEvents(); resultsIntent.putExtra(RETURN_EVENTS_ARCHIVED, (Serializable)liteEventsA); } // collect the shared data and send it if(rSharedPrefs){ SharedPreferences prefs = getSharedPreferences(getString(R.string.PREFS_FILE_NAME), MODE_WORLD_READABLE); resultsIntent.putExtra(RETURN_SHARED_PREFS, (Serializable)(prefs == null? null : prefs.getAll())); } Log.i(SERVICE_NAME, "returning results"); // send everything to the caller sendBroadcast(resultsIntent); } }
Regardless of whether the class is a service or broadcast receiver, you must register it with the application as a “receiver” (which means that it can receive any kind of target broadcasts); this can be done in java code or in the android manifest. An example that I use for this service in the manifest is below (although my Java class does not care about the action action):
<receiver android:name="edu.clarkson.dtask.BK.NotificationReceiver" android:enabled="true"> <intent-filter> <action android:name="edu.clarkson.dtask.BK.NotificationReceiver.DISPATCH_ALARM" /> <action android:name="edu.clarkson.dtask.BK.NotificationReceiver.CANCEL_ALARM" /> </intent-filter> </receiver>
Various system actions and notifications will be sent to the OS when a certain state changes; if you want to hear about these changes, you register the broadcast or intenservice in the same way as in the manifest above for another Intent action (for example, ACTION_BATTERY_STATE_CHANGED). All documentation can be found on the Android developer site . Hope you start on the right track.