2012-03-09 98 views
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"); 
      } 
+0

消耗少试试这个:刚加入这一行: locationManager.requestLocationUpdates( LocationManager.G PS_PROVIDER,refreshInterval,50,locationListener); – 2012-03-09 06:39:27

+0

有很多不同的方式来获取当前位置的精确度高或低,我看到您使用的是网络提供商,因此您可以在移动的位范围内获得相同的位置,因为它提供了网络塔的位置而不是您的位置。您可能需要轮询GPS。看看这篇文章http://stackoverflow.com/a/6775456/28557 – 2012-03-09 06:50:32

+0

嗨Mayur它不工作,它需要GPS被打开。是不可能获得位置thro网络提供商... – NullPointerException 2012-03-09 06:52:47

回答