Question

Jusqu'à présent, j'ai une classe pour un simple service local. Ce que je veux faire est de envoyer des demandes à ce service local de différentes activies dans ma demande. En fonction des paramètres de ces demandes le service se connecte via HttpClient à un serveur Web et recevoir un JSONObject et le retourner à l'activité. Toute la communication HTTP travaille déjà dans mon activité, mais je voudrais à exécuter dans un thread séparé dans mon service local maintenant.

Le code source de mon service local très simple ressemble à ceci jusqu'à présent:

// BackgroundService.java
package com.test.localservice;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class BackgroundService extends Service
{
    @Override
    public void onCreate() {
        super.onCreate();
        Log.i("BackgroundService", "onCreate()");
        Thread thr = new Thread(null, new RunThread(), "BackgroundService");
        thr.start();
    }
    class RunThread implements Runnable
    {
        public void run() {
            Log.i("BackgroundService", "run()");

            /* Here the HTTP JSON communication is going to happen */

            //BackgroundService.this.stopSelf();
        }
    }
    @Override
    public void onDestroy()
    {
        Log.i("BackgroundService", "onDestroy()");
        super.onDestroy();
    }

    @Override
    public void onStart(Intent intent, int startId) {
        Log.i("BackgroundService", "onStart()");
        super.onStart(intent, startId);
    }
    @Override
    public IBinder onBind(Intent intent) {
        Log.i("BackgroundService", "onBind()");
        return null;
    }

}

Le problème que je suis face maintenant (en raison d'un manque de connaissance) est la partie de la communication Activités <-> Service local . J'ai trouvé quelques exemples de communication et d'échange pour un objet Service à distance avec

scroll top