我一直在测试LocationListener(http://developer.android.com/reference/android/location/LocationListener.html),并实现了抽象onLocationChange。
在onLocationChange方法中,我修改了主活动中的TextViews,以显示实际的坐标变化。
活动开始后,GPS开始连接后,TextView会在每次坐标更改时进行核心更新。
这是我的代码:
package com.example.androidgps_get_coordinates;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
TextView textView_lat;
TextView textView_long;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView_lat=(TextView)findViewById(R.id.textV_lat);
textView_long=(TextView)findViewById(R.id.textV_long);
String serviceString=Context.LOCATION_SERVICE;
LocationManager locationManager; //remember to import LocationManager
locationManager=(LocationManager)getSystemService(serviceString);
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 0, 0,
new LocationListener() {
public void onLocationChanged(Location location) {
textView_lat.setText(String.valueOf(location.getLatitude()));
textView_long.setText(String.valueOf(location.getLongitude()));
}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status,
Bundle extras) {}
}
);
String provider=LocationManager.GPS_PROVIDER;
Location l= locationManager.getLastKnownLocation(provider);
}
答案 0 :(得分:1)
onLocationChange
,因此在您在UI线程中运行TextView
时会更新AsyncTask
。是什么让您需要Thread
或单独的null
?
另一方面,在您尝试阅读之前检查Location
的{{1}}值是一种很好的做法。
答案 1 :(得分:1)
您已编写侦听器(LocationListener)并调用requestLocationUpdate * s *() 这意味着您要求提供商定期提供位置 - 通常每隔1秒。
BTW我不知道你为什么写这行:
Location l= locationManager.getLastKnownLocation(provider);
它什么都不做。
答案 2 :(得分:0)
onLocationChanged
方法的工作方式与onClickListener
之类的其他监听器类似。当您的位置更改时,此侦听器将调用,就像用户单击按钮一样。所以不需要使用Thread但是如果你想保存Locations数据或其他东西,那么使用Thread是很重要的。
这是我将locationListener与线程一起使用的示例,但Thread仅用于发送数据:
public void onLocationChanged(final Location location) {
// TODO Auto-generated method stub
locationThread = new Thread(new Runnable() {
public void run() {
// TODO Auto-generated method stub
int Status = 0;
latitudeField=location.getLatitude();
longitudeField=location.getLongitude();
Date dt = new Date();
dt.setDate(dt.getDate());
Status=CallSoap.CurrentPosition(YarVisitLogin.UserName, YarVisitLogin.Password, YarVisitLogin.Visitor, latitudeField, longitudeField, PersianCalendar.getPersianDate(dt), PersianCalendar.getPersianTime());
yardb.InsertIntoMyLocations(yardb.getMaxMyLocation_ID()+1, PersianCalendar.getPersianDate(dt), PersianCalendar.getPersianTime(), Double.toString(latitudeField), Double.toString(longitudeField), Status);
}
});
locationThread.start();
}