Pour développer le widget android france je me suis inspiré du tuto de google ici
tous d’abord le fichier AndroidManifest.xml
par rapport au fichier habituel il y a deux nouvelles balises à ajouter receiver et service.
Dans le répertoire res je rajoute un nouveau répertoire xml et dans celui ci le fichier widget_af.xml.
Ce fichier permet la configuration de la taille de notre widget la péridiodicité de rafraichissement et la fenêtre par défaut ici
widget_messaf dans le répertoire layout.
le fichier widget_messaf
Ma classe Widgetandroidfance est une extension de la classe AppWidgetProvider
public class Widgetandroidfance extends AppWidgetProvider {
/** Called when the activity is first created. */
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// To prevent any ANR timeouts, we perform the update in a service
context.startService(new Intent(context, UpdateService.class));
}
public static class UpdateService extends Service {
@Override
public void onStart(Intent intent, int startId) {
// Build the widget update for today
RemoteViews updateViews = buildUpdate(this);
// Push update for this widget to the home screen
ComponentName thisWidget = new ComponentName(this, Widgetandroidfance.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
manager.updateAppWidget(thisWidget, updateViews);
}
/**
* Build a widget update to show the current Wiktionary
* "Word of the day." Will block until the online API returns.
*/
public RemoteViews buildUpdate(Context context) {
Lecteurflux objLecteurFlux = new Lecteurflux();
//récupération des 3 dernier titre du flux dans un tableau
String[] aTableauresult = objLecteurFlux.dernierFlux();
RemoteViews updateViews = null;
updateViews = new RemoteViews(context.getPackageName(), R.layout.widget_af);
updateViews.setTextViewText(R.id.word_title, aTableauresult[0]);
// updateViews.setTextViewText(R.id.definition, aTableauresult[1]);
updateViews.setTextViewText(R.id.word_title2,aTableauresult[2]);
updateViews.setTextViewText(R.id.word_title3,aTableauresult[4]);
//lien http a ouvrir dans le line
String definePage = "https://android-france.fr";
Intent defineIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(definePage));
PendingIntent pendingIntent = PendingIntent.getActivity(context,
0 /* no requestCode */, defineIntent, 0 /* no flags */);
updateViews.setOnClickPendingIntent(R.id.widget, pendingIntent);
return updateViews;
}
@Override
public IBinder onBind(Intent intent) {
// We don't need to bind to this service
return null;
}
}
}
le fichier widget_af dans le répertoire res/layout la fenêtre que j’appelle pour afficher mes titres.
N’oublier pas d’importer
import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.IBinder;
import android.widget.RemoteViews;
Il est temps de mettre à profit les tutoriels d’Android France:
Passez à la vitesse supérieure et investissez quelques dizaines d’euros pour acquérir les connaissances qui vous feront gagner de l’argent avec vos applications rendez-vous sur notre boutique Android-france pour ces formations en vidéo