获取位置服务的程序,获取到位置后,隔一段时间向服务器发送数据我在onCreate()方法中调用的getLocationInfo()简单代码:onCreate(){ getLocationInfo(); }当位置变动,在监听器locationListener中的onLocationChanged方法中再次调用getLocationInfo();简单代码:public void onLocationChanged(Location location) {getLocationInfo();}在getLocationInfo()中建了一个线程,不断的向服务器发送数据简单代码:getLocationInfo(){new locationThread=LocationThread(location,carid);}当程序一运行,就会触发线程向服务器端发送数据,位置变动,又会触发一个新的线程,在后台输出的时候,不止打印当前经纬度,位置变动前的经纬度也打印出来了。
希望大侠给小弟解决一下;
希望大侠给小弟解决一下;
解决方案 »
- 录音格式转换成wav格式
- 在static中引用SimpleAdapter
- 再问关于SQLiteQueryBuilder和SQLitedatabase的问题
- android模拟器运行一个程序时打印出以下信息,大家给看下
- android实时视频采集传到PC端不能播放!!!!急
- android 的一个bug,不知道有何解决办法?
- android 图片左右滑动问题(急)
- Android SDK更新错误,求助
- 这是我刚刚写的用网络状态改变时发出的广播启动该程序的例子,但是为什么没达到效果呢
- 关于Socket服务端和客户端的问题
- android 关于线程的问题,希望高手给分析一下
- EditText 高度问题
这是程序启动时显示的界面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();
}
谢谢各位了,问题解决,马上结帖