Services

A service is started when an application component, such as an activity, starts it by calling startService(). Once started, a service can run in the background indefinitely, even if the component that started it is destroyed.

android_service_lifecycle

An IntentService has a few limitations:

  • It can’t interact directly with your user interface. To put its results in the UI, you have to send them to an Activity.
  • Work requests run sequentially. If an operation is running in an IntentService, and you send it another request, the request waits until the first operation is finished.
  • An operation running on an IntentService can’t be interrupted.

Etapes :

1°) Créez la classe du service

 package xxxx;  
 import android.app.Service;  
 import android.os.IBinder;  
 import android.widget.Toast;  
 public class ServiceYYY extends Service {  
      @Override  
      public int onStartCommand(Intent intent, int flags, int startId) {  
           // Let it continue running until it is stopped.  
           Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();  
         //-- Inser Code here ----  
           //  
           return START_STICKY;  
      }  
      @Override  
      public void onDestroy() {  
           super.onDestroy();  
           Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();  
      }  
      @Override  
      public IBinder onBind(Intent intent) {  
           // TODO Auto-generated method stub  
           return null;  
      }  
 }  

 2°) Insérez dans le manifest de l’application la ligne déclarant le service :

avant

2°) Prévoir dans l’activité Android un Start et Stop (ou alors il faut désinstaller l’appli)

         setContentView(R.layout.main);  
           if (!isMyServiceRunning())  
             startService(new Intent(getBaseContext(), ServiceYYY.class));  

Avec comme fonction pour détecter si le service est déjà lancé toutefois il est impossible de lancer plusieurs fois le même service :

 private boolean isMyServiceRunning() {  
        ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);  
        for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {  
          if ("packagexxxx.ServiceYYY".equals(service.service.getClassName())) {  
            return true;  
          }  
        }  
        return false;  
      }  

Arrêt :

       Button bStopService = (Button) findViewById(R.id.StopService);  
       bStopService.setOnClickListener(new View.OnClickListener() {  
         @Override  
         public void onClick(View v) {  
           stopService(new Intent(getBaseContext(), ServiceSurvAnnonce.class));  
         }  
       });  

 Lancement du service au démarrage du téléphone :

After boot completes the Android system broadcasts an intent with the action android.intent.action.BOOT_COMPLETED. And now all we need is an IntentReceiver, called a BroadcastReceiver, to listen and act on it.

1°) Créez une classe qui va intercepeter l’événement BOOT_COMPLETED :

 public class BootStartUpReciever extends BroadcastReceiver {  
     @Override  
     public void onReceive(Context context, Intent intent) {  
        //**This method is called when the BroadcastReceiver is receiving  
        //-- Start Service On Boot Start Up  
        if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
            Intent pushIntent = new Intent(context, ServiceYYY.class);
            context.startService(pushIntent);
        }
        //--Start App On Boot Start Up  
        //Intent App = new Intent(context, MainActivity.class);  
        //App.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
        //context.startActivity(App);  
     }  
 }  

2°) Ajouter au fichier manifest le receiver :


Et ne pas oublier d’ajouter la permission : « android.permission.RECEIVE_BOOT_COMPLETED »/> Pour créer un service qui répète une tâche régulièrement et qui affiche un Toast :

 public class ServiceTest extends Service {  
   private static Timer timer = new Timer();  
   private Context ctx;  
   public IBinder onBind(Intent arg0) {  
     return null;  
   }  
   public void onCreate() {  
     super.onCreate();  
     ctx = this;  
     startService();  
   }  
   private void startService() {  
     timer.scheduleAtFixedRate(new mainTask(), 0, 60000);  
   }  
   private class mainTask extends TimerTask {  
     public void run() {  
       toastHandler.sendEmptyMessage(0);  
     }  
   }  
   public void onDestroy() {  
     super.onDestroy();  
     Toast.makeText(this, "Service Stopped ...", Toast.LENGTH_SHORT).show();  
   }  
   private final Handler toastHandler = new Handler() {  
     @Override  
     public void handleMessage(Message msg) {  
       Toast.makeText(getApplicationContext(), "Affichage test", Toast.LENGTH_SHORT).show();  
     }  
   };    
 }  

Test si le service existe :

Créez une classe :

public class ServiceTools {
    private static String LOG_TAG = ServiceTools.class.getName();

    public static boolean isServiceRunning(Context context,Class<?> serviceClass){
        final ActivityManager activityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
        final List<RunningServiceInfo> services = activityManager.getRunningServices(Integer.MAX_VALUE);

        for (RunningServiceInfo runningServiceInfo : services) {
            Log.d("TestService", String.format("Service:%s", runningServiceInfo.service.getClassName()));
            if (runningServiceInfo.service.getClassName().equals(serviceClass.getName())){
                return true;
            }
        }
        return false;
    }
}

Dans le code :

  Boolean isServiceRunning = ServiceTools.isServiceRunning(  
                   getBaseContext(),  
                   ServiceSurvAnnonce.class);  
  Log.d("TestService",isServiceRunning+"");  
  if (!isServiceRunning)  
                startService(new Intent(getBaseContext(), ServiceSurvAnnonce.class));  

Pour éviter qu’un service s’arrête :

Dans le cas où le service est stoppé, il est possible d’utiliser un BroadcastReceiver qui est activé dans la méthode onDestroy() du service (https://stackoverflow.com/questions/21550204/how-to-automatically-restart-a-service-even-if-user-force-close-it)

StickyService.java

public class StickyService extends Service
{
    private static final String TAG = "StickyService";


    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e(TAG, "onStartCommand");
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        sendBroadcast(new Intent("YouWillNeverKillMe"));
    }

}

RestartServiceReceiver.java

public class RestartServiceReceiver extends BroadcastReceiver
{

    private static final String TAG = "RestartServiceReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.e(TAG, "onReceive");
    context.startService(new Intent(context.getApplicationContext(), StickyService.class));

    }

}

Declare the components in manifest file:

    <service android:name=".StickyService" >
    </service>

    <receiver android:name=".RestartServiceReceiver" >
        <intent-filter>
            <action android:name="YouWillNeverKillMe" >
            </action>
        </intent-filter>
    </receiver>

Start the StickyService in a Component (i.e. ApplicationActivityFragment):

startService(new Intent(this, StickyService.class));

Ou

sendBroadcast(new Intent("YouWillNeverKillMe"));

Références :

http://techblogon.com/android-start-service-on-boot/

https://developer.android.com/training/run-background-service/create-service.html

http://www.tutorialspoint.com/android/android_services.htm

http://www.javacodegeeks.com/2014/01/android-service-tutorial.html

http://developer.android.com/guide/components/services.html#StartingAService

https://xjaphx.wordpress.com/2012/07/07/create-a-service-that-does-a-schedule-task

http://stackoverflow.com/questions/3819676/android-timer-within-a-service

Votre 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 )

Photo Facebook

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

Connexion à %s

Ce site utilise Akismet pour réduire les indésirables. En savoir plus sur la façon dont les données de vos commentaires sont traitées.

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