在我的应用程序中,我通过调用 locationManager.requestLocationUpdates(); 来设置10秒的计时器以获取当前位置但问题是我无法获得更新的位置而是提供相同的纬度申请开始的经度和经度..
以下是我在服务中编写的代码..帮助和建议赞赏
public class DemoService extends Service{
LocationManager locationManager;
public Timer timer;
public static int i;
private String FILE_NAME = "location.txt";
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
Toast.makeText(getApplicationContext(), "onStart()....", Toast.LENGTH_SHORT).show();
timer = new Timer();
timer.schedule(new mainTask(), 0, 10*1000);
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Toast.makeText(getApplicationContext(), "onCreate()....", Toast.LENGTH_SHORT).show();
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Toast.makeText(getApplicationContext(), "onDestroy()....", Toast.LENGTH_SHORT).show();
timer.cancel();
}
private class mainTask extends TimerTask
{
public void run()
{
toastHandler.sendEmptyMessage(0);
}
}
private final Handler toastHandler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
Toast.makeText(getApplicationContext(), "test "+(i), Toast.LENGTH_SHORT).show();
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, new MyLocationListerner());
}
};
public class MyLocationListerner implements LocationListener{
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
i++;
Toast.makeText(DemoService.this, "\nLat : "+location.getLatitude()+"\nLog : "+location.getLongitude(), Toast.LENGTH_SHORT).show();
saveToFile(location);
locationManager.removeUpdates(this);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
Toast.makeText(DemoService.this,"Got onLocationChanged..",Toast.LENGTH_SHORT).show();
System.out.println("Got onStatusChanged");
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
Toast.makeText(DemoService.this,"Got onLocationChanged..",Toast.LENGTH_SHORT).show();
System.out.println("Got onProviderEnabled");
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
Toast.makeText(DemoService.this,"Got onLocationChanged..",Toast.LENGTH_SHORT).show();
System.out.println("Got onProviderDisabled");
}