找不到GPS位置

时间:2012-04-25 20:56:29

标签: android android-emulator

我有一个singelton线程类,它将gps指向我的服务器。该类保持并刷新gps位置。

FinderThread:

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;
    }




}

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

请帮忙

1 个答案:

答案 0 :(得分:0)

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

你在FINE_LOCATION的清单中给自己一个权限,对吗?

我要提出的一个建议就是更改你的requestLocationUpdates(),以便它们要求修复它们之间的0时间和0之间的距离。我见过的所有GPS接收器都会通过此设置为您提供大约每秒一次的修复,这几乎是大多数GPS引擎的工作方式。只是在您提出请求的一分钟和20米的可能性让您失望。如果你得到了这个建议的修复,你可以在那里工作,以尽量减少gps的开启时间。

祝你好运!