在LocationListener

时间:2016-06-12 09:55:57

标签: java android multithreading location locationlistener

我正在编写一个应用程序,可以在GPS的帮助下跟踪用户位置。这是我的GPSThread课程的实现。

package com.example.userpc.roadtracker;

import android.content.Context;
import android.database.sqlite.SQLiteConstraintException;
import android.database.sqlite.SQLiteDatabase;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;


public class GPSThread extends Thread {

    private Context context;
    private volatile boolean stop = true;
    private volatile Location location;
    private LocationManager locationManager;
    private LocationListener loclis;
    private final static long dist=0;     //change
    private final static long thread_delay=1*60*1000;       //change

    public static double round(double value, int places) {
        if (places < 0) throw new IllegalArgumentException();

        long factor = (long) Math.pow(10, places);
        value = value * factor;
        long tmp = Math.round(value);
        return (double) tmp / factor;
    }

    public GPSThread(Context c) {
        context = c;
        locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        Log.e("GPS", "Enabled "+isGPSEnabled);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, dist, loclis=new LocationListener() {

            @Override
            public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onProviderEnabled(String arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onProviderDisabled(String arg0) {
                // TODO Auto-generated method stub
                Log.e("GPS", "disabled");
            }

            @Override
            public void onLocationChanged(Location arg0) {
                // TODO Auto-generated method stub
                location=arg0;
                //location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);     
                //Log.e("Loccation", location.toString()+"hello");
                if(location!=null)
                {

                    // TODO write location to Database
                    double latitude=round(location.getLatitude(),4);
                    double longitude=round(location.getLongitude(),4);
                    Log.e("Location", "Latitude = "+latitude+" Longitude = "+longitude);
                    /*
                     * Implement write to database
                     * Consider making it Thread safe
                     * One helper object and one DB connection
                     * Use singleton pattern
                     */
                    DatabaseHelper dathelp=DatabaseHelper.getInstance(context);
                    SQLiteDatabase db=dathelp.getUsableDataBase();
                    try{
                        db.execSQL("INSERT INTO `location` (`longitude`, `latitude`) VALUES ("+longitude+", "+latitude+");");
                        Log.e("Database", "Inserted");
                    }
                    catch(SQLiteConstraintException e){
                        e.printStackTrace();
                        Log.e("Database", "Same Values");
                    }
                }
                else
                {
                    Log.e("GPS", "Unknown location");
                }
            }
        });
    }

    public boolean isRunning()
    {
        return !stop;
    }

    public void stopThread() {      
        stop = false;
        interrupt();
        locationManager.removeUpdates(loclis);      //
    }

    @Override
    public void run() {
        super.run(); 
        Log.e("GPS", "Thread started");
        while (stop) {               
            try {
                Thread.sleep(thread_delay);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }            
        }
        Log.e("GPS", "Thread stopped");
    }
}

以下是我的观察:

  • onLocationChanged方法仅在我停止线程后调用(即,当调用中断时)
  • 如果我不使用stopThread()方法中的LocationManager.removeUpdates(),我会在位置发生变化时获得回调,但是由于尚未删除的位置侦测器。但是,最近添加的LocationListener仅在线程停止后调用onLocationChanged方法。

我的疑惑:

  • 造成这种特殊行为的原因是什么?
  • 是因为我在循环中使用Thread.sleep吗? (只有当它打破了onLocationChanged方法时才会被调用)
  • 我如何克服这个?

提前致谢。

0 个答案:

没有答案