Skip to content

Instantly share code, notes, and snippets.

@rafaeltoledo

rafaeltoledo/snippet01.xml Secret

Last active Nov 5, 2019
Embed
What would you like to do?
Android 23
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/frame">
<TextView android:id="@+id/nome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:textSize="10pt"
android:textColor="#FFFFFF" />
</RelativeLayout>
package net.rafaeltoledo.restaurante;
import android.appwidget.AppWidgetProvider;
public class WidgetAplicativo extends AppWidgetProvider {
}
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="300dip"
android:minHeight="79dip"
android:updatePeriodMillis="1800000"
android:initialLayout="@layout/widget" />
<receiver android:name=".WidgetAplicativo"
android:label="@string/app_name"
android:icon="@drawable/ic_launcher">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/provedor_widget" />
</receiver>
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
ComponentName cn = new ComponentName(context, WidgetApp.class);
RemoteViews atualizarFrame = new RemoteViews("net.rafaeltoledo.restaurante", R.layout.widget);
GerenciadorRestaurantes gerenciador = new GerenciadorRestaurantes(context);
try {
Cursor c = gerenciador.getReadableDatabase().rawQuery("SELECT COUNT(*) FROM restaurantes", 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 = gerenciador.getReadableDatabase().rawQuery("SELECT nome FROM restaurantes LIMIT 1 OFFSET ?", args);
c.moveToFirst();
atualizarFrame.setTextViewText(R.id.nome, c.getString(0));
} else {
atualizarFrame.setTextViewText(R.id.nome, context.getString(R.string.vazio));
}
} finally {
gerenciador.close();
}
appWidgetManager.updateAppWidget(cn, atualizarFrame);
}
<string name="vazio">Nenhum registro.</string>
<string name="vazio">Ningún registro.</string>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.