Communauté française android news, news du Web et bien d'autres domaines

telephones, smart phones android, programmation et applications, news twitter facebook

Services sous android Leçon 1

By • nov 20th, 2009 • Category: Tutorials

A la différence des activités qui présentent une interface graphique a l’utilisateur, les services  s’exécutent en tache de fond

Une fois le service crée il faut l’enregistrer aupres de l’application
Pour démarrer un service on fait appel a startService();
Si le service requiert des permissions que l’application ne possède pas alors cet appel lancera une exception

public class MyService extends Service {
 
  @Override
  public void onCreate(){
     // What to do when service is created
  }
 
  @Override
  public IBinder onBind(Intent intent){
     // Replace with service binding implementation
  }
 
  @Override
  public void onStart(){
     // Action to perform when service starts
 
  }
 
}
 
// techniques pour demarrer un service
// implicite
startService(new Intent(MyService.MY_ACTION));
// Explicite
startService(new Intent(this,MYService.class));
 
// stop service en appelant la methode stopService et en lui passant
// le service a stopper
 
stopService(new Intent(this,service.getClass()));
 
// appel explicite
try {
    Class serviceClass = Class.forName(service.getClassName());
stopService(new Intent(this,serviceClass));
} catch (ClassNotFoundException e){
 
}

A voir aussi:

  1. Earthquake suite du projet android
  2. Travailler avec les preferences sous ANDROID
  3. Projet android par etapes : Applications EarthQuake

is
Email this author | All posts by

Comments are closed.