More Home Cooking
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.RemoteViews;
public class WidgetService extends IntentService {
public WidgetService() {
super("WidgetService");
}
@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 );
c.close();
if (count> 0 ) {
int offset=(int)(count*Math.random());
String args[]={String.valueOf(offset)};
c=helper
.getReadableDatabase()
.rawQuery("SELECT _ID, name FROM restaurants LIMIT 1 OFFSET ?",
args);
c.moveToFirst();
updateViews.setTextViewText(R.id.name, c.getString( 0 ));
}
else {
updateViews.setTextViewText(R.id.title,
this.getString(R.string.empty));
}
}
finally {
helper.close();
}
mgr.updateAppWidget(me, updateViews);
}
}