IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Android Discussion :

[Service] Problème démarrage / arrêt d'un remote service


Sujet :

Android

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éclairé
    Inscrit en
    Décembre 2008
    Messages
    280
    Détails du profil
    Informations forums :
    Inscription : Décembre 2008
    Messages : 280
    Par défaut [Service] Problème démarrage / arrêt d'un remote service
    Bonjour,

    Je suis actuellement en train de créer une activity comprenant seulement un bouton démarrer et stopper le service.

    Mon service fonction et se lance bien au boot (c'est fait expres), mais je voudrais également avoir possibilité de le démarrer/stopper via mon application. Pour linstant j'arrive bien à démarrer le service via le bouton démarrer et à le stopper si et seulement si je le fait directement sans fermer mon application.

    Je pense donc que le problème ici est que le processus que je passe quand je veux fermer n'est plus correct voir null.

    Est-ce qu'il est possible de trouver mon service et de chopper l'id du processus ?! ou alors est-ce que je ne mis prend pas bien ?

    voici mon événement des boutons :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    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
    @Override
    	public void onClick(View v) {
    		Log.i("PYD", "CLICK DEMARRER"+v.getId());
    		switch (v.getId()){
    			case 1:
    				Log.i("PYD", "CASE1");
    				// Démarré
    				mIntent = new Intent(); 
    				//mStart = true;
    				//mIntent.putExtra("mStart", mStart);
    				mIntent.setClassName("com.mitecorporation.ptd", "com.mitecorporation.ptd.service.BackgroundService"); 
     
    				 mRemoteConnection = new ServiceConnection() {  
    					 @Override
    					 public void onServiceDisconnected(ComponentName name) {  
    				     } 
    				     @Override  
    				     public void onServiceConnected(ComponentName name, IBinder service) { 
    				         IRemoteBackgroundService mService = IRemoteBackgroundService.Stub.asInterface((IBinder) service); 
    				         try { 
    				        	 mService.getPid(); 
    				        	 Log.i("PYD SERVICE", "SERVICE EN CONNEXION : " + mService.getPid());
    				           //  service.getData(); 
    				         } catch (RemoteException e) { 
    				             e.printStackTrace();
    				         } 
    				     }
    				 }; 
     
    				 bindService(mIntent, mRemoteConnection, Context.BIND_AUTO_CREATE);
    				 startService(mIntent);
    				 break;
    			case 2:
    				// Arrêté
    				Log.i("PYD","BUTTON STOP");
    					unbindService(mRemoteConnection);
    					mStart = false;
    					stopService(mIntent);
    					break;
    		}
    	}
    voici l'erreur :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    06-09 11:28:15.820: ERROR/AndroidRuntime(20866): FATAL EXCEPTION: main
    06-09 11:28:15.820: ERROR/AndroidRuntime(20866): java.lang.IllegalArgumentException: Service not registered: null
    06-09 11:28:15.820: ERROR/AndroidRuntime(20866):     at android.app.LoadedApk.forgetServiceDispatcher(LoadedApk.java:891)
    06-09 11:28:15.820: ERROR/AndroidRuntime(20866):     at android.app.ContextImpl.unbindService(ContextImpl.java:893)
    06-09 11:28:15.820: ERROR/AndroidRuntime(20866):     at android.content.ContextWrapper.unbindService(ContextWrapper.java:352)
    06-09 11:28:15.820: ERROR/AndroidRuntime(20866):     at com.mitecorporation.ptd.debt_config_sms.onClick(debt_config_sms.java:85)
    06-09 11:28:15.820: ERROR/AndroidRuntime(20866):     at android.view.View.performClick(View.java:2485)
    06-09 11:28:15.820: ERROR/AndroidRuntime(20866):     at android.view.View$PerformClick.run(View.java:9080)
    06-09 11:28:15.820: ERROR/AndroidRuntime(20866):     at android.os.Handler.handleCallback(Handler.java:587)
    06-09 11:28:15.820: ERROR/AndroidRuntime(20866):     at android.os.Handler.dispatchMessage(Handler.java:92)
    06-09 11:28:15.820: ERROR/AndroidRuntime(20866):     at android.os.Looper.loop(Looper.java:130)
    06-09 11:28:15.820: ERROR/AndroidRuntime(20866):     at android.app.ActivityThread.main(ActivityThread.java:3683)
    06-09 11:28:15.820: ERROR/AndroidRuntime(20866):     at java.lang.reflect.Method.invokeNative(Native Method)
    06-09 11:28:15.820: ERROR/AndroidRuntime(20866):     at java.lang.reflect.Method.invoke(Method.java:507)
    06-09 11:28:15.820: ERROR/AndroidRuntime(20866):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    06-09 11:28:15.820: ERROR/AndroidRuntime(20866):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    06-09 11:28:15.820: ERROR/AndroidRuntime(20866):     at dalvik.system.NativeStart.main(Native Method)
    Merci

  2. #2
    Expert confirmé

    Avatar de Feanorin
    Profil pro
    Inscrit en
    Avril 2004
    Messages
    4 589
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 4 589
    Par défaut
    Bonjour,

    Vite fait un code pour savoir si un service est lancé.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    public boolean Search(Context context,String serviceName) {
    		ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    		List<ActivityManager.RunningServiceInfo> services = manager
    				.getRunningServices(100);
    		String pname = context.getPackageName();
    		for (int i = 0; i < services.size(); i++) {
    			try {
    				if (services.get(i).service.toString().contains(serviceName)
    						&& services.get(i).started && 
    						services.get(i).service.toString().contains(pname)) {
    					return true;
    				}
    			} catch (Exception e) {
    			}
    		}
    		return false;
    	}
    Après pour avoir son pid il te suffit de faire juste un
    Maintenant tu peux toujours faire un bind sur ce service et le stoppé par la suite ( ce qui est plus propre que de le tuer).

  3. #3
    Membre éclairé
    Inscrit en
    Décembre 2008
    Messages
    280
    Détails du profil
    Informations forums :
    Inscription : Décembre 2008
    Messages : 280
    Par défaut
    Merci beaucoup je vais tester ça !

    Mais le Pid est t-il le même pour mon service lancé au boot et celui lancé par mon application ?!

  4. #4
    Membre éclairé
    Inscrit en
    Décembre 2008
    Messages
    280
    Détails du profil
    Informations forums :
    Inscription : Décembre 2008
    Messages : 280
    Par défaut
    Donc j'ai implémenté la fonction ça me permet donc de gérer mes bouton etc, mais la je pense que l'erreur vient du bind comme tu dis j'ai pas du tout comprendre la dessus ...

    Ou dois-je binder mon service ? Je fais bien un bind service pourtant et une unbindservice mais il me dit qu'il n'est pas enregistré. Si je reste sur l'activity ca fonction si je démarre que je quitte l'application et que je le stop (c'est bien détecté seul le bouton stop apparaît) il bug sur unbindservice en disant :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
     
    06-09 14:30:10.722: ERROR/AndroidRuntime(21670): FATAL EXCEPTION: main
    06-09 14:30:10.722: ERROR/AndroidRuntime(21670): java.lang.IllegalArgumentException: Service not registered: null
    06-09 14:30:10.722: ERROR/AndroidRuntime(21670):     at android.app.LoadedApk.forgetServiceDispatcher(LoadedApk.java:891)
    06-09 14:30:10.722: ERROR/AndroidRuntime(21670):     at android.app.ContextImpl.unbindService(ContextImpl.java:893)
    06-09 14:30:10.722: ERROR/AndroidRuntime(21670):     at android.content.ContextWrapper.unbindService(ContextWrapper.java:352)
    06-09 14:30:10.722: ERROR/AndroidRuntime(21670):     at com.mitecorporation.ptd.debt_config_sms.onClick(debt_config_sms.java:126)
    06-09 14:30:10.722: ERROR/AndroidRuntime(21670):     at android.view.View.performClick(View.java:2485)
    06-09 14:30:10.722: ERROR/AndroidRuntime(21670):     at android.view.View$PerformClick.run(View.java:9080)
    06-09 14:30:10.722: ERROR/AndroidRuntime(21670):     at android.os.Handler.handleCallback(Handler.java:587)
    06-09 14:30:10.722: ERROR/AndroidRuntime(21670):     at android.os.Handler.dispatchMessage(Handler.java:92)
    06-09 14:30:10.722: ERROR/AndroidRuntime(21670):     at android.os.Looper.loop(Looper.java:130)
    06-09 14:30:10.722: ERROR/AndroidRuntime(21670):     at android.app.ActivityThread.main(ActivityThread.java:3683)
    06-09 14:30:10.722: ERROR/AndroidRuntime(21670):     at java.lang.reflect.Method.invokeNative(Native Method)
    06-09 14:30:10.722: ERROR/AndroidRuntime(21670):     at java.lang.reflect.Method.invoke(Method.java:507)
    06-09 14:30:10.722: ERROR/AndroidRuntime(21670):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    06-09 14:30:10.722: ERROR/AndroidRuntime(21670):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    06-09 14:30:10.722: ERROR/AndroidRuntime(21670):     at dalvik.system.NativeStart.main(Native Method)

  5. #5
    Expert confirmé

    Avatar de Feanorin
    Profil pro
    Inscrit en
    Avril 2004
    Messages
    4 589
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 4 589
    Par défaut
    Bonjour,

    Je pense qu'il faut que tu bind ton service à ton Activity si le service est lancé ( dans le OnResume ou OnCreate de ton Activity , de là tu pourras alors l'arrêter car tu auras la référence du service.

    Mais le Pid est t-il le même pour mon service lancé au boot et celui lancé par mon application ?!
    Aucune idée , je ne sais pas comment android attribue les pids ( aléatoire surement ).Après si tu lances un service qui est déjà existant , android ne lance pas un nouveau process mais garde le même.

    public abstract ComponentName startService (Intent service)

    Since: API Level 1
    Request that a given application service be started. The Intent can either contain the complete class name of a specific service implementation to start, or an abstract definition through the action and other fields of the kind of service to start. If this service is not already running, it will be instantiated and started (creating a process for it if needed); if it is running then it remains running.
    Every call to this method will result in a corresponding call to the target service's onStartCommand(Intent, int, int) method, with the intent given here. This provides a convenient way to submit jobs to a service without having to bind and call on to its interface.
    Using startService() overrides the default service lifetime that is managed by bindService(Intent, ServiceConnection, int): it requires the service to remain running until stopService(Intent) is called, regardless of whether any clients are connected to it. Note that calls to startService() are not nesting: no matter how many times you call startService(), a single call to stopService(Intent) will stop it.
    The system attempts to keep running services around as much as possible. The only time they should be stopped is if the current foreground application is using so many resources that the service needs to be killed. If any errors happen in the service's process, it will automatically be restarted.
    This function will throw SecurityException if you do not have permission to start the given service.
    Parameters

    service Identifies the service to be started. The Intent may specify either an explicit component name to start, or a logical description (action, category, etc) to match an IntentFilter published by a service. Additional values may be included in the Intent extras to supply arguments along with this specific start call.
    Returns

    If the service is being started or is already running, the ComponentName of the actual service that was started is returned; else if the service does not exist null is returned.
    Throws

    SecurityException
    See Also

    stopService(Intent)
    bindService(Intent, ServiceConnection, int)

  6. #6
    Membre éclairé
    Inscrit en
    Décembre 2008
    Messages
    280
    Détails du profil
    Informations forums :
    Inscription : Décembre 2008
    Messages : 280
    Par défaut
    Merci beaucoup !!! Ca fonctionne !

Discussions similaires

  1. Réponses: 7
    Dernier message: 22/02/2011, 14h11
  2. Configuration démarrage / arrêt Web Service CXF
    Par surpriz dans le forum Services Web
    Réponses: 0
    Dernier message: 30/06/2010, 10h11
  3. [WS 2003] Problème d'arrêt de service (dépendance à ajouter ?)
    Par chaval dans le forum Windows Serveur
    Réponses: 4
    Dernier message: 03/03/2010, 20h05
  4. Problèmes Démarrage des services sous Linux
    Par clementp dans le forum Administration-Migration
    Réponses: 2
    Dernier message: 20/11/2009, 09h06
  5. Monit - problème pour le démarrage / l'arret d'un service
    Par Museum dans le forum Administration système
    Réponses: 4
    Dernier message: 20/09/2007, 14h06

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo