获取位置服务的程序,获取到位置后,隔一段时间向服务器发送数据我在onCreate()方法中调用的getLocationInfo()简单代码:onCreate(){ getLocationInfo(); }当位置变动,在监听器locationListener中的onLocationChanged方法中再次调用getLocationInfo();简单代码:public void onLocationChanged(Location location) {getLocationInfo();}在getLocationInfo()中建了一个线程,不断的向服务器发送数据简单代码:getLocationInfo(){new locationThread=LocationThread(location,carid);}当程序一运行,就会触发线程向服务器端发送数据,位置变动,又会触发一个新的线程,在后台输出的时候,不止打印当前经纬度,位置变动前的经纬度也打印出来了。
希望大侠给小弟解决一下;
希望大侠给小弟解决一下;
这是程序启动时显示的界面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);
}
};
}
希望大侠帮我一把,谢谢案啦
1. HandlerThread 并不是真正的线程。
2. getLocationInfo里面创建的LocationThread,由于有引用,不会被gc。调用一次就会增加一个LocationThread。
3. “当位置变动,自动触发onLocationChanged()方法”,没必要再创建一个LocationThread?
thread.handler.removeCallbacks(thread.runnable);
thread=null;
System.gc();
}
谢谢各位了,问题解决,马上结帖