0
在我的應用程序中,我設置了10秒的計時器以通過調用locationManager.requestLocationUpdates()獲取當前位置;但問題是,我不能獲得更新的位置,而不是它給同一緯度以及其中應用程序已啓動經度..無法獲取更新的位置
下面是寫在服務我的代碼..幫助和建議表示讚賞
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");
}
消耗少試試這個:剛加入這一行: locationManager.requestLocationUpdates( LocationManager.G PS_PROVIDER,refreshInterval,50,locationListener); – 2012-03-09 06:39:27
有很多不同的方式來獲取當前位置的精確度高或低,我看到您使用的是網絡提供商,因此您可以在移動的位範圍內獲得相同的位置,因爲它提供了網絡塔的位置而不是您的位置。您可能需要輪詢GPS。看看這篇文章http://stackoverflow.com/a/6775456/28557 – 2012-03-09 06:50:32
嗨Mayur它不工作,它需要GPS被打開。是不可能獲得位置thro網絡提供商... – NullPointerException 2012-03-09 06:52:47