Android Programming Tutorials

(Romina) #1
More Home Cooking

Add the following lines to the end of the onHandleIntent() method in


WidgetService:


Intent i=new Intent(this, WidgetService.class);
PendingIntent pi=PendingIntent.getService(this, 0 , i, 0 );

updateViews.setOnClickPendingIntent(R.id.next, pi);
mgr.updateAppWidget(me, updateViews);

Here, we create a service PendingIntent and register it as the click handler


for the button.


Now, if you recompile and reinstall the project, you will see that pressing


the button gives you a new restaurant (unless it happens to randomly select


the current one again).


Step #5: Get Control on Name Clicks


Finally, we want to bring up the DetailForm activity on the currently-visible


restaurant if the user taps on the restaurant's name. To do this, we will need


another PendingIntent, but we also need a bit more data from the content


provider first.


Replace the current implementation of onHandleIntent() in WidgetService


with the following:


@Override
public void onHandleIntent(Intent intent) {
ComponentName me=new ComponentName(this, AppWidget.class);
RemoteViews updateViews=new RemoteViews("apt.tutorial",
R.layout.widget);
RestaurantHelper helper=new RestaurantHelper(this);
AppWidgetManager mgr=AppWidgetManager.getInstance(this);

try {
Cursor c=helper
.getReadableDatabase()
.rawQuery("SELECT COUNT(*) FROM restaurants", null);

c.moveToFirst();

int count=c.getInt( 0 );

331
Free download pdf