Android Programming Tutorials

(Romina) #1
More Home Cooking

Figure 59. The app widget with the newly-added button

Step #3: Migrate Update Logic to an IntentService


Right now, we are doing our database I/O right in our AppWidgetProvider


implementation's onUpdate() method. That is probably fine, but if we add


too much logic, we run the risk of timing out, as onUpdate() is called on the


main application thread. Since we can update our app widget


asynchronously without issue, it is safer if we can delegate this work to


something that can be done off the main thread, such as an IntentService.


With that in mind, create an IntentService named WidgetService in the


LunchList project, with the following implementation:


package apt.tutorial;

import android.app.IntentService;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;

327
Free download pdf