获取位置服务的程序,获取到位置后,隔一段时间向服务器发送数据我在onCreate()方法中调用的getLocationInfo()简单代码:onCreate(){ getLocationInfo();  }当位置变动,在监听器locationListener中的onLocationChanged方法中再次调用getLocationInfo();简单代码:public void onLocationChanged(Location location) {getLocationInfo();}在getLocationInfo()中建了一个线程,不断的向服务器发送数据简单代码:getLocationInfo(){new locationThread=LocationThread(location,carid);}当程序一运行,就会触发线程向服务器端发送数据,位置变动,又会触发一个新的线程,在后台输出的时候,不止打印当前经纬度,位置变动前的经纬度也打印出来了。
希望大侠给小弟解决一下;

解决方案 »

  1.   

    就是在程序一开始时,就调用了getLocationInfo(){new locationThread=LocationThread(location,carid);}建了一个线程,当位置变动,自动触发onLocationChanged()方法,再次调用getLocationInfo(){new locationThread=LocationThread(location,carid);}又创建了一个线程,原来的线程没有没有结束,如何结束原来的线程
      

  2.   

    创建一个handler ,将looper 设置到非UI线程。 在你需要更新的时候,用post OR sendmessage,到这个线程去做更新。
      

  3.   

    可能说的不是很详细,附上代码
    这是程序启动时显示的界面public class LocationActivity extends Activity {
    Button btnlogin; // 登录和退出按钮对像
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    btnlogin = (Button) findViewById(R.id.login); // 获得用户名和密码按钮实例

    btnlogin.setOnClickListener(new View.OnClickListener() { public void onClick(View arg0) {
    Intent service = new Intent(LocationActivity.this, GetLocation.class);
    startService(service); }
    });
    }
    }这是获取位置服务的代码public class GetLocation extends Service {

    HandlerThread thread;
    public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
    } @Override
    public void onCreate() { String serviceString = Context.LOCATION_SERVICE;
    LocationManager locationManager = (LocationManager) getSystemService(serviceString);
    String provider = locationManager.GPS_PROVIDER;
    Location location = locationManager.getLastKnownLocation(provider);
    locationManager.requestLocationUpdates(provider, 20000, 10000,
    locationListener);

    if (location != null) {
    getLocationInfo(location);
    } else {
    } } public void getLocationInfo(Location location) {
    // TODO Auto-generated method stub
    String latLongInfo; if (location == null) { latLongInfo = "No Location Found";
    }
    HandlerThread thread = new HandlerThread(location);
    } private final LocationListener locationListener = new LocationListener() { @Override
    public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub
    getLocationInfo(location); // 获取汽车的新位置
    System.out.println("location changed");
    } @Override
    public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub
    getLocationInfo(null);
    } @Override
    public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub
    getLocationInfo(null);
    } @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub } };
    }这是线程中的代码public class HandlerThread {
    Handler handler=new Handler();
    Location address=null;
    int state;
    double lat,lng;
    public HandlerThread(){}
        public HandlerThread(Location location){
         handler.post(runnable); 
            //handler.postDelayed(runnable,40000);
         lat=location.getLatitude(); //维度
         lng=location.getLongitude(); //精度
        
        }
    Runnable runnable=new Runnable() {

    @Override
    public void run() {

    handler.postDelayed(runnable,2300);
    System.out.println("汽车位置"+lat+","+lng);

     
    }

    };

    }
      

  4.   

    这是这个项目的下载地址 http://d.download.csdn.net/down/3483811/hanzhaoping
    希望大侠帮我一把,谢谢案啦
      

  5.   

    不太明白,简单说下几点疑问。
    1. HandlerThread 并不是真正的线程。
    2. getLocationInfo里面创建的LocationThread,由于有引用,不会被gc。调用一次就会增加一个LocationThread。
    3. “当位置变动,自动触发onLocationChanged()方法”,没必要再创建一个LocationThread?
      

  6.   

    解释一下,Handler是我创建的对象,我是想获取新的经纬度后,隔一段时间发送到服务器,所以想建一个LocationThread对象,现在问题解决了,就是在onLocationChanged()加了一个判断if(thread!=null){

    thread.handler.removeCallbacks(thread.runnable);
    thread=null;
    System.gc();

    }
    谢谢各位了,问题解决,马上结帖