我试图每隔几秒就获得一次GPS值,而我却错过了一些技巧。以下是我尝试过的内容:
public class Locn extends ActionBarActivity
{
private LocationManager locationManager;
private String provider;
private Location loc = null;
private Criteria criteria;
... local variables ...
@Override
protected void onCreate(Bundle savedInstanceState)
{
...
// Get the location manager
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
boolean enabled = service.isProviderEnabled(LocationManager.GPS_PROVIDER);
// Check if enabled. If not send user to the GPS settings
if (!enabled)
{
Toast.makeText(this, "Please enable GPS location service",
Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
else
{
Toast.makeText(this, "GPS location service is enabled",
Toast.LENGTH_SHORT).show();
}
// Define the criteria to select the location provider -> use default
criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);
// Let Android select the best location provider based on criteria
provider = locationManager.getBestProvider(criteria, true);
...
}
//--------------------------------------
// Set up timer handlers
Handler timerHandler = new Handler();
Runnable timerRunnable = new Runnable()
{
@Override
public void run()
{
provider = locationManager.getBestProvider(criteria, true);
loc = locationManager.getLastKnownLocation(provider);
milli = System.currentTimeMillis();
longitude = loc.getLongitude();
latitude = loc.getLatitude();
count++;
timerHandler.postDelayed(this, 2000);
}
};
计数和毫秒每两秒更换一次,但纬度和经度根本不会改变。 (是的,我改变了位置 - 最多2英里)
我在这里缺少什么?在再次调用getLastKnownLocation之前是否必须清除loc?
谢谢, 沃尔特
答案 0 :(得分:0)
Timer tm =new Timer();
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
boolean enabled = service.isProviderEnabled(LocationManager.GPS_PROVIDER);
tm.schedule(new task(),10,10000);//this execute task every 10 seconds
//在主要活动中使用计时器任务
class task extends TimerTask {
public void run() {
Home.this.runOnUiThread(new Runnable() {
public void run() {
longitude = loc.getLongitude();
latitude = loc.getLatitude();
});
}
};
答案 1 :(得分:0)
你不需要定时器。只需使用LocationManager.requestLocationUpdates()
来自Android文档:
requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener)
只需使用如下:
public class MainActivity extends Activity implements LocationListener {
private String provider;
private LocationManager lm;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
provider = lm.getBestProvider(criteria, false);
Location location = lm.getLastKnownLocation(provider);
if (location != null) {
onLocationChanged(location);
}
}
@Override
protected void onResume() {
super.onResume();
lm.requestLocationUpdates(provider, 1000, 10, this);
}
@Override
protected void onPause() {
super.onPause();
lm.removeUpdates(this);
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
public void onLocationChanged(Location location) {
double lat = location.getLatitude();
double lng = location.getLongitude();
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}