Originariamente inviato da
e.bracchi
Spero che questo esempio completo ti possa essere utile:
Vediamo subito la creazione del layout del nostro widget:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_gravity="center"
android:layout_width="match_parent" android:layout_height="wrap_content">
<FrameLayout android:layout_width="match_parent" android:id="@+id/frameLayout1" android:background="@drawable/sfondo_wid" android:layout_height="200dip">
<TextView android:layout_height="wrap_content" android:textColor="#ffffff" android:layout_marginTop="5dip" android:id="@+id/widget_textview" android:text="@string/Hello" android:layout_width="wrap_content" android:textSize="20dip" androidadding="10dip" android:layout_gravity="left|center_horizontal" android:layout_marginLeft="10dip"></TextView>
<TextView android:layout_marginBottom="5dip" android:layout_height="wrap_content" android:textColor="#ffffff" android:layout_marginLeft="10dip" android:layout_marginRight="10dip" android:id="@+id/corpo" android:text="@string/Hello1" android:layout_width="wrap_content" androidadding="10dip" android:layout_gravity="left|center_horizontal" android:layout_marginTop="50dip"></TextView>
<Button android:layout_gravity="right|center_horizontal" android:layout_marginTop="15dip" android:layout_marginRight="15dip" android:id="@+id/butt_wid" android:background="@drawable/b_wid" android:layout_width="30dp" android:layout_height="30dip"></Button>
</FrameLayout>
</LinearLayout>
Come possiamo vedere dal codice abbiamo due textview e un button.
Passiamo poi alla classe precedentemente creata dove andiamo ad eseguire l’override del metodo onUpdate:
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int appWidgetIds)
{
Timer timer = new Timer();
prendiNews(context);
contatore = 0;
timer.scheduleAtFixedRate(new MyNews(context, contatore, appWidgetManager), 1, 5000);
}
La classe MyNews che richiamiamo all’interno del metodo timer.scheduleAtFixedRate ha questo aspetto:
private class MyNews extends TimerTask
{
RemoteViews remoteViews;
AppWidgetManager appWidgetManager;
ComponentName thisWidget;
Context context;
//java.text.DateFormat format = SimpleDateFormat.getTimeInstance(SimpleDateFormat. MEDIUM, Locale.getDefault());
public MyNews(Context context, int conta, AppWidgetManager appWidgetManager)
{
this.appWidgetManager = appWidgetManager;
this.context = context;
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
thisWidget = new ComponentName(context, HelloWidget.class);
}
@Override
public void run()
{
Map_news mpn1 = new Map_news();
mpn1 = news.get(contatore);
if(contatore < news.size()-1)
{
contatore++;
}
else
{
contatore = 0;
}
remoteViews.setTextViewText(R.id.widget_textview, mpn1.getTitolo());
remoteViews.setTextViewText(R.id.corpo, mpn1.getSummary());
Intent clickIntent = new Intent(context, Loading.class);
PendingIntent clickPI = PendingIntent.getActivity(context, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.butt_wid, clickPI);
appWidgetManager.updateAppWidget(thisWidget, remoteViews);
}
}
La sezione principale di questa classe è la struttura del metodo run dove andiamo a settare i due testi e l’action del bottone che nell’evento onClick starta l’applicazione. Andiamo poi a vedere come recuperare i testi dal database:
private void prendiNews(Context con)
{
DBAiuto db;
db = new DBAiuto(con);
db.open();
news = new ArrayList();
Cursor cursor = db.mDb.rawQuery("SELECT * FROM news", null);
while(cursor.moveToNext())
{
Map_news mpn = new Map_news();
String titolo = cursor.getString(1);
String corpo = cursor.getString(2);
String summary = cursor.getString(3);
mpn.setTitolo(titolo);
mpn.setCorpo(corpo);
mpn.setSummary(summary);
news.add(mpn);
}
cursor.close();
db.close();
}
In questo metodo eseguo semplicemente una query e inserisco i dati recuperati in una struttura di tipo ArrayList. Qui di seguito inserisco tutta la classe completa:
public class HelloWidget extends AppWidgetProvider
{
private ArrayList news;
private int contatore;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int appWidgetIds)
{
Timer timer = new Timer();
prendiNews(context);
contatore = 0;
timer.scheduleAtFixedRate(new MyNews(context, contatore, appWidgetManager), 1, 5000);
}
private class MyNews extends TimerTask
{
RemoteViews remoteViews;
AppWidgetManager appWidgetManager;
ComponentName thisWidget;
Context context;
//java.text.DateFormat format = SimpleDateFormat.getTimeInstance(SimpleDateFormat. MEDIUM, Locale.getDefault());
public MyNews(Context context, int conta, AppWidgetManager appWidgetManager)
{
this.appWidgetManager = appWidgetManager;
this.context = context;
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
thisWidget = new ComponentName(context, HelloWidget.class);
}
@Override
public void run()
{
Map_news mpn1 = new Map_news();
mpn1 = news.get(contatore);
if(contatore < news.size()-1)
{
contatore++;
}
else
{
contatore = 0;
}
remoteViews.setTextViewText(R.id.widget_textview, mpn1.getTitolo());
remoteViews.setTextViewText(R.id.corpo, mpn1.getSummary());
Intent clickIntent = new Intent(context, Loading.class);
PendingIntent clickPI = PendingIntent.getActivity(context, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.butt_wid, clickPI);
appWidgetManager.updateAppWidget(thisWidget, remoteViews);
}
}
private void prendiNews(Context con)
{
DBAiuto db;
db = new DBAiuto(con);
db.open();
news = new ArrayList();
Cursor cursor = db.mDb.rawQuery("SELECT * FROM news", null);
while(cursor.moveToNext())
{
Map_news mpn = new Map_news();
String titolo = cursor.getString(1);
String corpo = cursor.getString(2);
String summary = cursor.getString(3);
mpn.setTitolo(titolo);
mpn.setCorpo(corpo);
mpn.setSummary(summary);
news.add(mpn);
}
cursor.close();
db.close();
}
}