关于线程的问题,希望高手给分析一下

11646435ser2 2011-07-30 09:14:53
获取位置服务的程序,获取到位置后,隔一段时间向服务器发送数据

我在onCreate()方法中调用的getLocationInfo()

简单代码:

onCreate(){

getLocationInfo();

}

当位置变动,在监听器locationListener中的onLocationChanged方法中再次调用getLocationInfo();

简单代码:

public void onLocationChanged(Location location) {

getLocationInfo();

}

在getLocationInfo()中建了一个线程,不断的向服务器发送数据

简单代码:

getLocationInfo(){

new locationThread=LocationThread(location,carid);

}

当程序一运行,就会触发线程向服务器端发送数据,位置变动,又会触发一个新的线程,在后台输出的时候,不止打印当前经纬度,位置变动前的经纬度也打印出来了。
希望大侠给小弟解决一下;
...全文
143 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
11646435ser2 2011-08-01
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 yy4040 的回复:]

不太明白,简单说下几点疑问。
1. HandlerThread 并不是真正的线程。
2. getLocationInfo里面创建的LocationThread,由于有引用,不会被gc。调用一次就会增加一个LocationThread。
3. “当位置变动,自动触发onLocationChanged()方法”,没必要再创建一个LocationThread?
[/Quote]
解释一下,Handler是我创建的对象,我是想获取新的经纬度后,隔一段时间发送到服务器,所以想建一个LocationThread对象,现在问题解决了,就是在onLocationChanged()加了一个判断

if(thread!=null){

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

}
谢谢各位了,问题解决,马上结帖
yytt4040 2011-08-01
  • 打赏
  • 举报
回复
不太明白,简单说下几点疑问。
1. HandlerThread 并不是真正的线程。
2. getLocationInfo里面创建的LocationThread,由于有引用,不会被gc。调用一次就会增加一个LocationThread。
3. “当位置变动,自动触发onLocationChanged()方法”,没必要再创建一个LocationThread?
fishmen26 2011-07-31
  • 打赏
  • 举报
回复
创建一个handler ,将looper 设置到非UI线程。 在你需要更新的时候,用post OR sendmessage,到这个线程去做更新。
11646435ser2 2011-07-31
  • 打赏
  • 举报
回复
这是这个项目的下载地址 http://d.download.csdn.net/down/3483811/hanzhaoping
希望大侠帮我一把,谢谢案啦
11646435ser2 2011-07-31
  • 打赏
  • 举报
回复
可能说的不是很详细,附上代码
这是程序启动时显示的界面

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


}

};

}
domonate 2011-07-30
  • 打赏
  • 举报
回复
学习一下,帮你顶一下!
11646435ser2 2011-07-30
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 chenqinglin163 的回复:]

不是很明白,你能说的更明白一点吗、
[/Quote]
就是在程序一开始时,就调用了getLocationInfo(){

new locationThread=LocationThread(location,carid);

}建了一个线程,当位置变动,自动触发onLocationChanged()方法,再次调用getLocationInfo(){

new locationThread=LocationThread(location,carid);

}又创建了一个线程,原来的线程没有没有结束,如何结束原来的线程
怪人伽利略 2011-07-30
  • 打赏
  • 举报
回复
不是很明白,你能说的更明白一点吗、

80,359

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧