-
25mai
Développez un widget pour android
Pour développer le widget android france je me suis inspiré du tuto de google ici
tous d’abord le fichier AndroidManifest.xml?Download download.txt1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="org.com.widgetandroidfrance" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/iconeaf" android:label="@string/app_name"> <receiver android:name=".Widgetandroidfance" android:label="@string/widget_name"> <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_af" /> </receiver> <!-- Service to perform web API queries --> <service android:name=".Widgetandroidfance$UpdateService" /> </application> <uses-permission android:name="android.permission.INTERNET" /> <uses-sdk android:minSdkVersion="3" /> </manifest>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.
?Download download.txt1 2 3 4 5 6 7
<?xml version="1.0" encoding="utf-8"?> <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:minWidth="250dip" android:minHeight="100dip" android:updatePeriodMillis="86400000" android:initialLayout="@layout/widget_messaf" />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
?Download download.txt1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/widget" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" style="@style/WidgetBackground"> <TextView android:id="@+id/message" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="12dip" android:padding="10dip" android:gravity="center" android:text="@string/widget_loading" style="@style/Text.Loading" /><!--récupération du text de lancement dans le fichier style du repertoire value--> </LinearLayout>Ma classe Widgetandroidfance est une extension de la classe AppWidgetProvider
?Download download.txt1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
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 = "http://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.
?Download download.txt1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/widget" android:layout_width="fill_parent" android:layout_height="wrap_content" android:focusable="true" style="@style/WidgetBackground"> <TextView android:id="@+id/word_app" android:layout_width="wrap_content" android:layout_height="wrap_content" android:includeFontPadding="false" android:singleLine="true" android:paddingRight="5dip" android:paddingBottom="10dip" android:ellipsize="end" style="@style/Text.WordTitle" /> <TextView android:id="@+id/word_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/word_app" android:paddingRight="15dip" android:paddingLeft="20dip" android:paddingBottom="5dip" android:includeFontPadding="false" android:ellipsize="end" style="@style/Text.WordTitle" android:inputType="textMultiLine" /> <TextView android:id="@+id/word_title2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/word_title" android:paddingRight="15dip" android:paddingLeft="20dip" android:paddingBottom="5dip" android:includeFontPadding="false" android:ellipsize="end" style="@style/Text.WordTitle" /> <TextView android:id="@+id/word_title3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/word_title2" android:paddingRight="15dip" android:paddingLeft="20dip" android:paddingBottom="5dip" android:includeFontPadding="false" android:ellipsize="end" style="@style/Text.WordTitle" /> </RelativeLayout>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
13 commentaires
Laissez un commentaire
Vous devez être identifié pour publier un commentaire.
Connexion - Inscription
S'inscrire










Bonjour,
Serait-il possible d’avoir une petite capture d’écran du résultat, histoire de ?
Merci de proposer ces tutoriaux !
Florian
@floflo :
tu as une demo du widget http://android-france.fr/2009/05/21/le-widget-du-site-android-france-disponible-sur-android-market/
@Guy
Exact, merci pour l’info
Bonjour,
Merci pour ce tuto.
Le paramètre de fréquence de MAJ (android:updatePeriodMillis) étant dans du XML, il est à priori fixe.
Est-ce possible de le MAJ par code ?
Je n’ai rien vu dans cette page
http://developer.android.com/reference/android/widget/package-summary.html
Merci
Wadaël
@Wadael : j’ai vu que certain widget l’autorise donc c’est possible je t’envoie un lien d’une application sur un widget méteo avec cette fonctionnalité
http://code.google.com/p/android-sky/
Salut,
j’ai pas bien compris les étapes, on peut pas avoir un fichier zippé qui contient chacun de fichier?
merci pour les tutoriaux. Je bosse sur le sujet prochainement
balnax
Euh…. Il n’y aurait pas une version pour les Nuls par hasard?
Cordialement.
Tu parles d’un tutoriel, 5 lignes d’explications pour 300 lignes de code + mélange français/anglais dans le source + le français approximatif et mal orthographié… c’est complètement bidon.