0

我有一个将 gps 点发送到我的服务器的单线程类。该类保持并刷新 gps 位置。

查找线程:

public class FinderThread extends Thread {
    private static FinderThread SINGLETON;
    public boolean isinterrupt = true;
    public int maxage;
    public int minage;
    public int distance;
    public String gender;
    public String latitude;
    public String longitude;
    public String sid;
    public Context ctx;
    LocationManager gps;
    boolean nav;
    SharedPreferences pref;
    private Handler dlh;
    private Handler myLocationHandler;
    boolean pause=false;




    private FinderThread(Context ctx,Handler dlh, Handler myLocationHandler) {
        this.ctx = ctx;

        gps = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);
        gps.requestLocationUpdates( LocationManager.GPS_PROVIDER,60000, 20, llistener);

        pref = ctx.getSharedPreferences("NFF", 0);
        sid = pref.getString("sid", "");

        this.dlh = dlh;

        if (myLocationHandler!=null)
        {
            this.myLocationHandler = myLocationHandler;
        }
        if(myLocationHandler != null)
        {
            myLocationHandler.sendEmptyMessage(0);
        }

    }
    public static synchronized FinderThread getInstance(Context ctx,Handler dlh, Handler myLocationHandler) 
    {
         if (SINGLETON == null) // falls null 
         { 
               SINGLETON = new FinderThread (ctx,dlh,myLocationHandler);//instanzieren 
         }
         else
         {
             SINGLETON.myLocationHandler= myLocationHandler;
             SINGLETON.dlh = dlh;
             myLocationHandler.sendEmptyMessage(0);
         }
         return SINGLETON;
    }

    /*
    @Override
    public synchronized void start() {
        if(SINGLETON != null)
        {
            SINGLETON.start();
        }
    }
    public synchronized void pause()
    {
        if(SINGLETON.getState()==State.RUNNABLE || SINGLETON.getState()==State.WAITING)
        {
                SINGLETON.pause = true;

        }
    }
    public synchronized void tcontinue ()
    {
        if(SINGLETON.getState()==State.RUNNABLE || SINGLETON.getState()==State.WAITING)
        {
                SINGLETON.pause = false;

        }
    }
    public synchronized boolean isPaused()
    {
        return SINGLETON.pause;
    }
    */

    public synchronized void pause()
    {
        this.pause = true;
    }
    public synchronized boolean isPaused()
    {
        return this.pause;
    }
    public synchronized void tcontinue ()
    {
                this.pause = false;
    }

    @Override
    public void run() {
        //String locationProvider = LocationManager.GPS_PROVIDER;
        //Location lastpoint = gps.getLastKnownLocation(locationProvider);

        //longitude = String.valueOf(lastpoint.getLongitude());
        //latitude = String.valueOf(lastpoint.getLatitude());

        try
        {
            while (isinterrupt) {
                if(!pause)
                {
                    Log.e("NFF", "Finder Thread Begin");
                    RestConnection r = new RestConnection("finder", dlh, "POST");
                    r.setParameter("minage", String.valueOf(minage));
                    r.setParameter("maxage", String.valueOf(maxage));
                    r.setParameter("distance", String.valueOf(distance));
                    r.setParameter("gender", String.valueOf(gender));
                    r.setParameter("latitude", String.valueOf(latitude));
                    r.setParameter("longitude", String.valueOf(longitude));
                    r.setParameter("sid", sid);
                    Log.e("NFF Finder Paramater", String.valueOf(minage)+" "+String.valueOf(maxage)+" "+String.valueOf(distance)+" "+String.valueOf(gender) + " "+String.valueOf(latitude)+" " +String.valueOf(longitude)+ " " + sid  );

                    r.start();
                }

                Thread.sleep(10000);
            }
        }
        catch (InterruptedException e) {
            Log.e("NFF FT", "InterruptedException");
            isinterrupt = false;
        }
    }
    private LocationListener llistener = new LocationListener (){

        @Override
        public void onLocationChanged(Location location) {
            if(String.valueOf(location.getLatitude())!= null)
            {
                latitude = String.valueOf(location.getLatitude());
                longitude =  String.valueOf(location.getLongitude());
                if(myLocationHandler != null)
                {
                    myLocationHandler.sendEmptyMessage(0);
                }
            }


        }

        @Override
        public void onProviderDisabled(String provider) {
            Log.e("NFF", "GPS Disabled");
            isinterrupt = true;

        }

        @Override
        public void onProviderEnabled(String provider) {
            Log.e("NFF", "GPS Enabled");

        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub

        }

    };
    public void saveProperties ()
    {
        Editor editor = pref.edit();
        editor.putInt("maxage", maxage);
        editor.putInt("minage", minage);
        editor.putInt("distance", distance);
        editor.putString("gender", gender);
        editor.putString("lat", latitude);
        editor.putString("long", longitude);
        editor.commit();

    }
    public void loadProperties ()
    {
        maxage = pref.getInt("maxage", -1);
        minage = pref.getInt("minage", -1);
        distance = pref.getInt("distance", -1);
        gender = pref.getString("gender", "f");
        latitude = pref.getString("lat", "f");
        longitude = pref.getString("long", "f");
    }
    public synchronized void pushSettings()
    {
        SINGLETON.maxage = maxage;
        SINGLETON.minage = minage;
        SINGLETON.distance = distance;
        SINGLETON.gender = gender;
        SINGLETON.latitude = latitude;
        SINGLETON.longitude = longitude;
    }




}

当我在模拟器上测试我的应用程序并使用模拟器控制设置位置时,一切正常。当我在手机上测试时,机器人找不到位置。我已经测试了应用程序“GPS Test”,它在 3 分钟内找到了 5 个卫星的位置。

请帮忙

4

1 回答 1

0

看起来你在那里拥有一切。

您在清单中授予自己 FINE_LOCATION 的权限,对吗?

我会提出的一个建议是改变你的requestLocationUpdates(),以便它要求修复它们之间的时间为 0,它们之间的距离为 0。我见过的所有 GPS 接收器都会使用此设置为您提供大约每秒一次的修复,这几乎是大多数 gps 引擎的工作方式。只是很有可能您提出的请求中的一分钟和 20 m 把您搞砸了。如果您对此建议进行了修复,则可以从那里开始工作以最大程度地减少 gps 的运行时间。

祝你好运!

于 2012-04-26T00:15:38.407 回答