【问题标题】:how to get the location latitude and longitude and update from back ground service如何从后台服务获取位置经纬度和更新
【发布时间】:2011-09-16 10:47:01
【问题描述】:

我正在开发示例应用程序。在此应用程序中,我想在用户使用 android 手机在路上移动时获取更新的位置纬度和经度。我已经实现了 Location Manager 类,如下所示:

 private LocationManager locationManager;
 private LocationListener locationListener;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);    

    locationListener = new GPSLocationListener();

    locationManager.requestLocationUpdates(
        LocationManager.GPS_PROVIDER, 
        0, 
        0, 
        locationListener);
    }

       private class GPSLocationListener implements LocationListener 
{
    @Override
    public void onLocationChanged(Location location) {
        if (location != null) {
       Toast.makeText(getBaseContext(), 
                    "Latitude: " + location.getLatitude() + 
                    " Longitude: " + location.getLongitude(), 
                    Toast.LENGTH_SHORT).show();
           }
        } 

}

如何从后台获取更新的位置经纬度?

请任何人帮助我。

【问题讨论】:

    标签: android latitude-longitude locationmanager


    【解决方案1】:

    您需要在您的服务中“实现”“LocationListener”。

    查看http://androidgps.blogspot.com/2008/09/simple-android-tracklogging-service.html

    【讨论】:

      【解决方案2】:

      如果您打算在应用程序未运行时获取纬度和经度,您可以使用在后台获取纬度和经度的服务并执行您想做的任何任务。并且不要忘记在不需要时删除更新,因为就设备电池使用而言,获取更新是非常昂贵的操作。

      还有一件事,您不需要检查位置是否为空值,因为只有在您的提供者获得位置时才会调用 onLocationChanged()。

      虽然我也是安卓新手。这可能会对您有所帮助。您必须查看 android 文档,这些 Service 类方法实际上做了什么以及何时调用它们。 这不是完整的代码。您已经自己实现了 locationlistener 。这只是一个显示普通类的示例程序:

      import java.util.Timer;
      import java.util.TimerTask;
      import android.app.Service;
      import android.content.Intent;
      import android.os.IBinder;
      import android.util.Log;
      
      public class BackService extends Service {
      
      private MyTimerTask mTimerTask;
      private Timer mTimer;
      
      @Override
      public void onCreate() {
          mTimer = new Timer();
          mTimerTask = new MyTimerTask();
      } 
      @Override
      public int onStartCommand(Intent intent, int flags, int startId) {
      
          mTimer.schedule(mTimerTask, 0, 500);
          Log.d("onStartCommand","onStartCommand called...");
          return START_STICKY;
      }
      
      @Override
      public IBinder onBind(Intent intent) {
          // TODO Auto-generated method stub
          return null;
      }
      
      private class MyTimerTask extends TimerTask {
      
          @Override
          public void run() {
              // TODO Auto-generated method stub
              Log.i("BackService","BackService is running...");
              doSomethingWithLocation();
          }
      
      }
      
      
      }
      

      【讨论】:

        猜你喜欢
        • 2017-03-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多