开发者

How to Request and removes updates of locationManager from a thread to a service

I have a service that implements the interface LocationListener with the methods of this interface.

When I run the service, I instantiate a LocationManager.

Then I start a thread that will run non-stop with an infinite loop.

Since this thread, I wish I could make a locationManager.removeupdates on my locationManager instantiated at the beginning of service.

But there is a proble开发者_C百科m, apparently I would need a Looper, I tried many things, but I can not use it.

Basically, here is my code, obviously, I don't know how use Looper, because my code stop after Log.d("GPS", "GPS Activé");

I've been searched stuff on Looper, but find a comprehensible how-to in my language (I'm French) are really difficult.

The code may seem weird, because, as I removed a lot of things...

public class ServicePrincipal extends Service implements LocationListener {

    boolean localisationOn = false;

    LocationManager locationManager;

    public class MyBinder extends Binder{
        ServicePrincipal getService(){
            return ServicePrincipal.this;
        }
    }

    @Override
    public IBinder onBind(Intent arg0) {
        return new MyBinder();
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public void onDestroy() {
        locationManager.removeUpdates(this);
        super.onDestroy();
    }

    @Override
    public void onStart(Intent intent, int startId) {

        MyThread mythread = new MyThread();
                    MyThread.start();
        super.onStart(intent, startId);
    }

    public class MyThread extends Thread {

        int nbInfos;
        @Override
        public void run() {

            for (;;) {

                        if (localisationOn)
                        {
                            localisationOn = false;
                            Looper.prepare();
                            stopGPS();
                            Looper.loop();
                        }

                            if (!localisationOn)
                            {
                                Looper.prepare();
                                startGPS();
                                Looper.loop();
                                /* On active le flag de localisation */
                                localisationOn = true;
                            }
                }
                try {
                    Log.d("Boucle for", "~~~~ Fin de boucle ~~~~");
                    this.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void onLocationChanged(Location location) {
        if ((location != null) && (localisationOn))
        {
            Log.d("Localisation", "Envoi des informations de localisation avec :");
            Log.d("Latitude", String.valueOf(location.getLatitude()));
            Log.d("Longitude", String.valueOf(location.getLongitude()));
        }

    }

    public void onProviderDisabled(String provider) {

    }

    public void onProviderEnabled(String provider) {

    }

    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    public void startGPS()
    {
        /* Intent du service de localisation */
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        /* On active l'actualisation par le GPS et par le réseau téléphonique */
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,1,1,this);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1,1,this);

        Log.d("GPS", "GPS Activé");
    }

    public void stopGPS()
    {

        locationManager.removeUpdates(this);
        Log.d("GPS", "GPS Désactivé");
    }

}


This problem happens when requestLocationUpdates() is called from a thread.Your program will be crashed.I solved that problem when I was working. I modified your code.Hope now your looper problem will be solved.Moreover,it will remove your infinite loop problem.Hope this help.

public class ServicePrincipal extends Service implements LocationListener {

    boolean localisationOn = false;

    LocationManager locationManager;
    private final Handler handler = new Handler();
    public class MyBinder extends Binder{
        ServicePrincipal getService(){
            return ServicePrincipal.this;
        }
    }

    @Override
    public IBinder onBind(Intent arg0) {
        return new MyBinder();
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public void onDestroy() {
        locationManager.removeUpdates(this);
        super.onDestroy();
    }

    @Override
    public void onStart(Intent intent, int startId) {

        /*MyThread mythread = new MyThread();
                    MyThread.start();*/
    handler.post(getData);
        super.onStart(intent, startId);
    }

    private final Runnable getData = new Runnable() {
        public void run() {
            getDataFrame();      
        }
    };

    private void getDataFrame() {
        if (localisationOn){
                            localisationOn = false;
                            stopGPS();
                }
        if (!localisationOn){
                        startGPS();
                        /* On active le flag de localisation */
                        localisationOn = true;
                }
        handler.postDelayed(getData,10000);

    }
    public void onLocationChanged(Location location) {
        if ((location != null) && (localisationOn))
        {
            Log.d("Localisation", "Envoi des informations de localisation avec :");
            Log.d("Latitude", String.valueOf(location.getLatitude()));
            Log.d("Longitude", String.valueOf(location.getLongitude()));
        }

    }

    public void onProviderDisabled(String provider) {

    }

    public void onProviderEnabled(String provider) {

    }

    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    public void startGPS()
    {
        /* Intent du service de localisation */
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        /* On active l'actualisation par le GPS et par le réseau téléphonique */
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,1,1,this);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1,1,this);

        Log.d("GPS", "GPS Activé");
    }

    public void stopGPS()
    {

        locationManager.removeUpdates(this);
        Log.d("GPS", "GPS Désactivé");
    }

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜