Widget et Services

Ressources :

http://code.tutsplus.com/tutorials/build-a-custom-clock-widget-implementing-user-configuration–mobile-11896

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 :

  1. Définition d’un appwidget provider :
    Ce fichier xml contient les metadatas du widget. Parmi les nombreuses propriétés les plus importantes sont :

  2. Insérez un receiver tag dans le paragraphe application de l’Android Manifest :
    At its base, the receiver 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.
  1. Create a layout file for our widget.
  2. 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.
    1. The most important method of AppWidgetProvider that our class has to override, is onUpdate(). 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.

    1. When Android fire an android.appwidget.action.APPWIDGET_UPDATE intent, the onReceive() method ofAppWidgetProvider dispatch that call to onUpdate().
    2. In other words, onReceive() is the “gate” through which the update trigger passes.

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.providerattribute. 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"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    ...................
    ...................
    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>   

Laisser un commentaire

Entrez vos coordonnées ci-dessous ou cliquez sur une icône pour vous connecter:

Logo WordPress.com

Vous commentez à l'aide de votre compte WordPress.com. Déconnexion / Changer )

Image Twitter

Vous commentez à l'aide de votre compte Twitter. Déconnexion / Changer )

Photo Facebook

Vous commentez à l'aide de votre compte Facebook. Déconnexion / Changer )

Photo Google+

Vous commentez à l'aide de votre compte Google+. Déconnexion / Changer )

Connexion à %s

Articles récents
Commentaires récents
fatima sur Bienvenue !
AdminDroid sur Bienvenue !
fatima sur Bienvenue !
Archives
Catégories
%d blogueurs aiment cette page :