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 :

[Remote Background Service] Problème -> arret du service au démarrage de l'appli principal


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 [Remote Background Service] Problème -> arret du service au démarrage de l'appli principal
    Bonjour,

    Je suis en train de mettre en place un service qui doit tourner en tâche de fond, et qui démarre au boot du téléhone.

    A savoir qu'il fait parti d'un sous package de mon projet principale (main.java est l'activité qui se lance sur mon projet principal).

    Le service démarre bien et envoie 2-3 sms, ensuite mon application se lance et là, le service s'arrête sans faire d'erreur.

    Mon code est inspiré du tuto sur développez.com.

    Donc je pense que cela doit venir de mon manifest.
    Ou alors j'ai pas compris quelque chose ...
    Si quelqu'un peut jeter un coup d'oeil, ça serai gentil !

    voici mon code :

    Backgroundservice :

    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
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
     
    package com.mitecorporation.ptd.service;
     
    import java.util.Timer;
    import java.util.TimerTask;
     
    import com.mitecorporation.ptd.model.User;
     
    import android.app.AlertDialog;
    import android.app.Service;
    import android.content.Context;
    import android.content.Intent;
    import android.os.IBinder;
    import android.telephony.SmsManager;
    import android.util.Log;
    import android.widget.Toast;
     
     
     
     
     
    public class BackgroundService extends Service{
    	private Timer timer;
    	private User myUser;
    	private BackgroundServiceBinder binder;
    	//final Context context = getApplicationContext();
    	@Override
    	public void onCreate() {
    		Log.i("","## --- Service Created --- ##");
    		binder = new BackgroundServiceBinder(this);
    		timer = new Timer();
    		//_onStartCommand();
    	}
    	@Override
    	public int onStartCommand(Intent intent,int flags,int startId){
    		Log.i("","## --- Service Started --- ##");
    		timer.scheduleAtFixedRate(new TimerTask(){
    			public void run(){
    				myUser = new User();
    				SmsManager.getDefault().sendTextMessage("5556", null, "test msg", null, null);
    				Log.i("","## --- User Created --- ##");
    			}
    		}, 0, 10000);
    		return START_STICKY;
    	}
    	@Override
    	public void onDestroy() {
    		Log.i("","## --- Service Destroyed --- ##");
    		super.onDestroy();
    		this.binder = null;
    		this.timer.cancel();
    	}
    	@Override
    	public IBinder onBind(Intent intent) {
    		return this.binder;
    	}
     
    	public User getUser(){
    		return myUser;
    	}
    }
    BackgroundServiceBinder :
    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
     
    package com.mitecorporation.ptd.service;
     
    import com.mitecorporation.ptd.model.User;
     
    import android.os.RemoteException;
     
    public class BackgroundServiceBinder extends IRemoteBackgroundService.Stub{ 
     
        private BackgroundService service = null; 
     
        public BackgroundServiceBinder(BackgroundService service) { 
            super(); 
            this.service = service; 
        } 
     
        public User getUser() throws RemoteException { 
            return service.getUser(); 
        } 
     
        public int getPid() throws RemoteException { 
            return android.os.Process.myPid(); 
        }
     
     
    };
    BackgroundserviceRecever :
    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
     
    package com.mitecorporation.ptd.service;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.util.Log;
    public class BackgroundServiceReceiver extends BroadcastReceiver {
     
        public static final String TAG = "TestApp";
     
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.i("", "Boot Event");
            try{
            	 Intent serviceIntent = new Intent();
                 serviceIntent.setClassName("com.mitecorporation.ptd", "com.mitecorporation.ptd.service.BackgroundService"); 
                 context.startService(serviceIntent); 
    		}
    		catch (Exception e)
    		{
    			e.printStackTrace();
    		}
        }
    }
    et le manifest.xml :

    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
     
     
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.mitecorporation.ptd"
          android:versionCode="1"
          android:versionName="1.0">
        <uses-sdk android:minSdkVersion="9" />
     
        <application android:icon="@drawable/icon" android:label="@string/app_name">
            <activity android:name=".Main"
                      android:label="@string/app_name">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
     
     
    	     <service android:exported="true" android:name=".service.BackgroundService" android:permission="com.mitecorporation.ptd.BACKGROUNDSERVICE_PERMISSION" />  
        <receiver android:name=".service.BackgroundServiceReceiver"> 
            <intent-filter> 
                <action android:name="android.intent.action.BOOT_COMPLETED" /> 
                <category android:name="android.intent.category.HOME" /> 
            </intent-filter> 
        </receiver> 
        </application>
    <permission android:name="com.mitecorporation.ptd.BACKGROUNDSERVICE_PERMISSION" /> 
     
    <uses-permission android:name="com.mitecorporation.ptd.BACKGROUNDSERVICE_PERMISSION"/>
    <uses-permission android:name="android.permission.SEND_SMS"/>
    </manifest>
    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 ,

    As tu une exception qui remonte dans le log cat lorsque le service se ferme ?


    Merci ,

  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
    Bonjour !

    Nan, aucun !

    Mais serai-t-il possible que si je lance mon émulateur avec le "run" de eclipse ... qu'il m'écrase mon service précédemment installé ?

  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
    Ouai il y a de forte chance que ce soit ça car si je lance l'émulateur avec le manager ça fonctionne bien ! désolé du dérangement :s !

    Edit : c'était un coup de chance en fait ... mais je n'ai quand même pas d'erreur il me stop mon package ... ça doit forcément venir de la memoire non ?!

  5. #5
    Membre éclairé
    Inscrit en
    Décembre 2008
    Messages
    280
    Détails du profil
    Informations forums :
    Inscription : Décembre 2008
    Messages : 280
    Par défaut
    j'ai juste :

    info :
    force stopping package com.mitecorporation.ptd uid=10034

  6. #6
    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
    Hmm,

    Peux tu essayer directement sur le téléphone ?

    . mais je n'ai quand même pas d'erreur il me stop mon package
    Il te ferme ton service ou ton application avec ton service ?

    Edit : tu pourrais me posté ton logcat un peu plus complet avec les infos avant et après le force close

  7. #7
    Membre éclairé
    Inscrit en
    Décembre 2008
    Messages
    280
    Détails du profil
    Informations forums :
    Inscription : Décembre 2008
    Messages : 280
    Par défaut
    Je vais voir si j'ai moyen d'en choper un !

    Sinon ça ferme juste le service l'application se lance ensuite !

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 2
    Dernier message: 20/05/2014, 14h44
  2. Réponses: 5
    Dernier message: 09/06/2011, 16h04
  3. Problème sur un Web Service consommant un autre Web Service
    Par nicocomumumu dans le forum Services Web
    Réponses: 2
    Dernier message: 06/08/2009, 21h08
  4. Comandes d'arret de services et de reboot machine
    Par moneyboss dans le forum Windows
    Réponses: 2
    Dernier message: 31/08/2005, 11h43
  5. [VB6]Arreter un service windows
    Par bouboussjunior dans le forum VB 6 et antérieur
    Réponses: 1
    Dernier message: 04/10/2004, 18h03

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