-
-
Save rafaeltoledo/ad3163fbb81fe2ac25cd9b82c41300bb to your computer and use it in GitHub Desktop.
Android 24
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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" /> | |
<ImageButton | |
android:id="@+id/proximo" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_centerVertical="true" | |
android:layout_alignParentRight="true" | |
android:src="@drawable/proximo" /> | |
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.rafaeltoledo.restaurante; | |
import android.app.IntentService; | |
import android.app.PendingIntent; | |
import android.appwidget.AppWidgetManager; | |
import android.content.ComponentName; | |
import android.content.Intent; | |
import android.database.Cursor; | |
import android.widget.RemoteViews; | |
public class WidgetService extends IntentService { | |
public WidgetService() { | |
super("WidgetService"); | |
} | |
@Override | |
protected void onHandleIntent(Intent intent) { | |
ComponentName cn = new ComponentName(this, WidgetAplicativo.class); | |
RemoteViews atualizarFrame = new RemoteViews("net.rafaeltoledo.restaurante", R.layout.widget); | |
GerenciadorRestaurantes gerenciador = new GerenciadorRestaurantes(this); | |
AppWidgetManager mgr = AppWidgetManager.getInstance(this); | |
try { | |
Cursor c = gerenciador.getReadableDatabase().rawQuery("SELECT COUNT(*) FROM restaurantes", null); | |
c.moveToFirst(); | |
int contador = c.getInt(0); | |
c.close(); | |
if (contador > 0) { | |
int deslocamento = (int) (contador * Math.random()); | |
String args[] = {String.valueOf(deslocamento)}; | |
c = gerenciador.getReadableDatabase().rawQuery("SELECT _id, nome FROM restaurantes LIMIT 1 OFFSET ?", args); | |
c.moveToFirst(); | |
atualizarFrame.setTextViewText(R.id.nome, c.getString(1)); | |
c.close(); | |
} else { | |
atualizarFrame.setTextViewText(R.id.nome, getString(R.string.vazio)); | |
} | |
} finally { | |
gerenciador.close(); | |
} | |
mgr.updateAppWidget(cn, atualizarFrame); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<service android:name=".WidgetService" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.rafaeltoledo.restaurante; | |
import android.appwidget.AppWidgetManager; | |
import android.appwidget.AppWidgetProvider; | |
import android.content.Context; | |
import android.content.Intent; | |
public class WidgetAplicativo extends AppWidgetProvider { | |
@Override | |
public void onUpdate(Context context, AppWidgetManager appWidgetManager, | |
int[] appWidgetIds) { | |
context.startService(new Intent(context, WidgetService.class)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Intent i = new Intent(this, WidgetService.class); | |
PendingIntent pi = PendingIntent.getService(this, 0, i, 0); | |
atualizarFrame.setOnClickPendingIntent(R.id.proximo, pi); | |
mgr.updateAppWidget(cn, atualizarFrame); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
protected void onHandleIntent(Intent intent) { | |
ComponentName cn = new ComponentName(this, WidgetAplicativo.class); | |
RemoteViews atualizarFrame = new RemoteViews("net.rafaeltoledo.restaurante", R.layout.widget); | |
GerenciadorRestaurantes gerenciador = new GerenciadorRestaurantes(this); | |
AppWidgetManager mgr = AppWidgetManager.getInstance(this); | |
try { | |
Cursor c = gerenciador.getReadableDatabase().rawQuery("SELECT COUNT(*) FROM restaurantes", null); | |
c.moveToFirst(); | |
int contador = c.getInt(0); | |
c.close(); | |
if (contador > 0) { | |
int deslocamento = (int) (contador * Math.random()); | |
String args[] = {String.valueOf(deslocamento)}; | |
c = gerenciador.getReadableDatabase().rawQuery("SELECT _id, nome FROM restaurantes LIMIT 1 OFFSET ?", args); | |
c.moveToFirst(); | |
atualizarFrame.setTextViewText(R.id.nome, c.getString(1)); | |
Intent i = new Intent(this, FormularioDetalhes.class); | |
i.putExtra(ListaRestaurantes._ID, c.getString(0)); | |
PendingIntent pi = PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT); | |
atualizarFrame.setOnClickPendingIntent(R.id.nome, pi); | |
c.close(); | |
} else { | |
atualizarFrame.setTextViewText(R.id.nome, getString(R.string.vazio)); | |
} | |
} finally { | |
gerenciador.close(); | |
} | |
Intent i = new Intent(this, WidgetService.class); | |
PendingIntent pi = PendingIntent.getService(this, 0, i, 0); | |
atualizarFrame.setOnClickPendingIntent(R.id.proximo, pi); | |
mgr.updateAppWidget(cn, atualizarFrame); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment