Ressources :
http://www.vogella.com/tutorials/AndroidWidgets/article.html#widget_serviceupdates
http://linuxtopia.org/online_books/android/devguide/guide/topics/appwidgets/index.html
http://yalantis.com/blog/implement-app-widgets-android/
http://www.helloandroid.com/tutorials/mastering-android-widget-development-part4
http://looksok.wordpress.com/2012/12/15/android-complete-widget-tutorial-including-source-code/
http://www.maraumax.fr/pages-3-creation-d-un-widget-parametrable-sur-android.html
http://karanbalkar.com/2013/04/tutorial-17-analogclock-widget-in-android/
http://malubu.wordpress.com/2012/06/02/android-app-widgets-and-their-configuration/
Pour détecter la présence d’un service :
private static boolean isMyServiceRunning(Context context) {
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (<<mon_service>>.class.getName().equals(service.service.getClassName())) {
Log.i("Service","Running");
return true;
}
}
Log.i("Service","NOT Running");
return false;
}
Les étapes de base pour créer un Widget avec une possibilité de configuration sont les suivantes :
- Définition d’un appwidget provider :
Ce fichier xml contient les metadatas du widget. Parmi les nombreuses propriétés les plus importantes sont :- The size of the widget, width and height. Android will take these numbers and convert them in number of cells http://developer.android.com/guide/practices/ui_guidelines/widget_design.html#anatomy_determining_size.
- http://www.kandroid.org/guide/practices/ui_guidelines/widget_design.html
- An update interval, in milliseconds. The widget will use this value as some sort of periodic timetable and refresh itself at each occurrences. Pour calculer les périodes days * 24 * 60 * 60 * 1000 ou :
TimeUnit.MILLISECONDS.convert(1, TimeUnit.DAYS); //gives 86400000
- The class that will act as the Configuration Activity, described with its full scope.
- Insérez un
receiver
tag dans le paragrapheapplication
de l’Android Manifest :
At its base, thereceiver
section tells to Android three things:- Where to find the class that implement the Provider of our widget.
- That our Provider will intercepts the system intent that tells that the widget must update itself. This system intent is
android.appwidget.action.APPWIDGET_UPDATE
. - Where is the appwidget provider info file, through a
meta-data
tag - This step is considered as the “registration step” of our widget.
- Create a layout file for our widget.
- Define a Provider class, that extends an
AppWidgetProvider
. This class is called at each steps of the lifecycle of our widget and is responsible of the actual creation of our widget.- The most important method of
AppWidgetProvider
that our class has to override, isonUpdate()
. As the name suggests, this method is called whenever the widget must update. The frequency of the updates is defined in the appwidget provider info file.
The Provider creates the UI of the widget through instances of
RemoteViews
, inflating the layout file.- When Android fire an
android.appwidget.action.APPWIDGET_UPDATE
intent, theonReceive()
method ofAppWidgetProvider
dispatch that call toonUpdate()
. - In other words,
onReceive()
is the “gate” through which the update trigger passes.
- The most important method of
http://www.miximum.fr/tutos/647-creer-un-widget-pour-android-exemples-et-bonnes-pratiques
http://developer.android.com/reference/android/appwidget/AppWidgetProvider.html
http://www.vogella.com/articles/AndroidWidgets/article.html
http://looksok.wordpress.com/2012/12/15/android-complete-widget-tutorial-including-source-code/
http://javatechig.com/android/app-widgets-example-in-android
http://sberfini.developpez.com/tutoriaux/android/appwidget/
http://www.helloandroid.com/tutorials/mastering-android-widget-development-part1
http://www.helloandroid.com/tutorials/mastering-android-widget-development-part2
http://www.helloandroid.com/tutorials/mastering-android-widget-development-part3
Dans le manifest :
Définir un receiver et un provider
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name="MyWidgetProvider" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/demo_widget_provider" />
</receiver>
<receiver
android:name="MyWidgetIntentReceiver"
android:label="widgetBroadcastReceiver" >
<intent-filter>
<action android:name="pl.looksok.intent.action.CHANGE_PICTURE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/demo_widget_provider" />
</receiver>
<service
android:name=".CallDetectService"
android:enabled="true"
android:exported="false" >
</service>
</application>
Définir un fichier xml caractérisant le widget (dans res/xml/) :
You also specify the meta-data for the widget via the android:name="android.appwidget.provider
attribute. The configuration file referred by this meta-data contains the configuration settings for the widget. If contains for example the update interface, the size and the initial layout of the widget.
3.1 Setting preview image to Android App Widget
You can set a preview image to specify what the app widget will look like on the widgets list screen. This help users to know about the widget while configuration. This is optional configuration, if not provided then the application icon will be displayed as default widget preview image.
Note: This attribute is introduced from Android 3.0
You can set the preview image using the following code
1
2
3
4
5
6
|
<? xml version = "1.0" encoding = "utf-8" ?> ................... ................... android:previewImage = "@drawable/widget" </appwidget-provider> |
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/widget_layout"
android:minHeight="72dp"
android:minWidth="146dp"
android:updatePeriodMillis="1800000" >
</appwidget-provider>
Votre commentaire