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 sous android et bindservice


Sujet :

Android

  1. #1
    Membre averti
    Profil pro
    Inscrit en
    Octobre 2004
    Messages
    51
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2004
    Messages : 51
    Par défaut Service sous android et bindservice
    Bonjour,

    Voila mon petit souci:
    Depuis quelques jours je souhaite échanger un objet entre 2 activity.
    La meilleur solution que j'ai trouvé semble être d'utiliser un service en background qui s'occupera des Get et Set mes objets.

    Pour cela j'ai codé le service en background en utilisant l'exemple référance de google http://developer.android.com/referen...p/Service.html

    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
     
    import android.app.Service;
    import android.content.Intent;
    import android.os.Binder;
    import android.os.IBinder;
    import android.util.Log;
     
    public class BackgroundService extends Service {
        /**
         * Class for clients to access.  Because we know this service always
         * runs in the same process as its clients, we don't need to deal with
         * IPC.
         */
     
     
        // This is the object that receives interactions from clients.  See
        // RemoteService for a more complete example.
        private final IBinder mBinder = new LocalBinder();
     
     
     
        public class LocalBinder extends Binder {
            public BackgroundService getService() {
                return BackgroundService.this;
            }
        }
     
        @Override
        public void onCreate() {
            super.onCreate();
        }
     
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            Log.i("BackgroudService", "Received start id " + startId + ": " + intent);
            // We want this service to continue running until it is explicitly
            // stopped, so return sticky.
            return START_STICKY;
        }
     
        @Override
        public void onDestroy() {
                super.onDestroy();
        }
     
     
     
     
     
     
        @Override
        public IBinder onBind(Intent intent) {
            // TODO Auto-generated method stub
            return null;
        }
     
    }

    et pour acceder au service j'ai codé dans mon activity principal:
    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
        void DemarrerService ()
        {
            BackgroundService backgroundService;
     
                ServiceConnection    mConnection = new ServiceConnection() {
     
                @Override
                public void onServiceConnected(ComponentName name, IBinder service) {
                    // TODO Auto-generated method stub
                    mIsBound = true;
                    Log.i("BackgroundService","Conected");
                    mBoundService = ((BackgroundService.LocalBinder)service).getService();
                //    Toast.makeText(this, "Connected",Toast.LENGTH_SHORT).show();
                }
     
                @Override
                public void onServiceDisconnected(ComponentName name) {
                    // TODO Auto-generated method stub
                    mIsBound = false;
                    Log.i("BackgroundService","Disconected");
     
                }};
     
     
                // Establish a connection with the service.  We use an explicit
                // class name because we want a specific service implementation that
                // we know will be running in our own process (and thus won't be
                // supporting component replacement by other applications).
                Intent intent = new Intent(this,BackgroundService.class); 
            boolean a=bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
     
     
        }

    Dans mon intent je met en premier argument "this", alors que dans l'exemple de google que je suis ils mettent "binding.this", or je ne comprend pas ce que c'est et je ne trouve aucune info à ce sujet.....

    Mon problème doc c'est que "bindService" revoit toujours false et je n'arrive pas à comprendre pourquoi, quelqu'un aurait-t-il une idée??


    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,

    Tu as un bon tutoriel pour apprendre Android sur developpez.

  3. #3
    Membre averti
    Profil pro
    Inscrit en
    Octobre 2004
    Messages
    51
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2004
    Messages : 51
    Par défaut
    Merci pour ta réponse, j'avais commencé par ce tuto, mais j'ai finalement repris celui de google, car je n'avais pas besoin dans un premier temps de la partie Listener.

Discussions similaires

  1. Web Service sous Android
    Par mendjijet dans le forum API standards et tierces
    Réponses: 6
    Dernier message: 25/02/2013, 17h30
  2. Utilisation du Web Service sous Android
    Par Laidback dans le forum API standards et tierces
    Réponses: 0
    Dernier message: 05/03/2012, 15h43
  3. Problème avec "Service" sous Android
    Par n2engineer5 dans le forum Android
    Réponses: 0
    Dernier message: 07/07/2011, 22h00
  4. [Tutoriel]: Les services sous Android
    Par Djug dans le forum Android
    Réponses: 0
    Dernier message: 26/05/2011, 11h58

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