我希望每隔1分钟获得一次当前位置,为此我正在使用Timer类并在其中实现位置提取,但控件不在Timer块中,不知道为什么, 我尝试了很多代码,但我每次都面临问题,
我使用下面这段代码,
没有错误也没有输出, 似乎控制不会进入所需的代码块。
有人请帮我解决我的代码有什么问题吗
public class location3 extends Activity implements LocationListener {
LocationManager locationManager;
Location location;
Timer timer;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Handler handler = new Handler();
Timer timer = new Timer();
TimerTask doAsynchronousTask = new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
try {
//your method here
getLocation();
} catch (Exception e) {
}
}
});
}
};
timer.schedule(doAsynchronousTask, 0, 10000); //execute in every 1 minute
}
public void getLocation(){
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,1000,0,location3.this);
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
double lat=location.getLatitude();
double lon=location.getLongitude();
Toast.makeText(location3.this, lat+","+lon, Toast.LENGTH_LONG).show();
}
public void onLocationChanged(Location location){}
public void onProviderDisabled(String provider){}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
}