1

在我的应用程序中,我想获取移动用户的更新位置,并且我想在一定时间的周期性间隔之后或在用户移动一定距离(比如 500 米)之后将其连续发送到服务器。我需要完成这些事情在背景中。我知道为此我必须实现服务类。但我不知道如何做到这一点。我做了一些工作。任何人都可以在这个问题上帮助我。我在服务课上做了以下事情。

public class BackGroundService extends Service implements LocationListener{

    public static final String Tag = BackGroundService.class.getName();
    LocationManager myLocationManager;
    Location myLocation;
    LocationListener myLocationListener;


    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    public void OnCreate()
    {
        super.onCreate();
        Log.d(Tag, "Service Started");

        android.os.Debug.waitForDebugger();

        Criteria criteria = new Criteria();
        criteria.setPowerRequirement(Criteria.POWER_LOW);
        criteria.setAccuracy(Criteria.ACCURACY_LOW);

        String locationProvider = myLocationManager.getBestProvider(criteria, true);

        myLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        myLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000*60*5, 500, myLocationListener);
        myLocation = myLocationManager.getLastKnownLocation(locationProvider);


    }

    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub
        longitude = location.getLongitude(); 
        latitude = location.getLatitude();

    }

    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub
        Log.d(Tag, "Provider is disabled");
    }

    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub
        Log.d(Tag, "Location Provider is enabled");
    }

    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }
}

从这里我想知道如何获取用户的当前纬度/经度并将其发送到服务器。

4

1 回答 1

1

我的问题的答案是......

public class LocationService extends Service{

@Override
public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();
    final LocationManager mlocmag = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    final LocationListener mlocList = new MyLocationList();
    final Location loc = mlocmag.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    UpdateWithNewLocation(loc); // This method is used to get updated location. 
    mlocmag.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocList);
}

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
}

@Override
public void onStart(Intent intent, int startId) {
    // TODO Auto-generated method stub
    super.onStart(intent, startId);
}
 private void UpdateWithNewLocation(final Location loc) {
        // TODO Auto-generated method stub

        if(loc!= null)
        {
        final double lat =loc.getLatitude(); // Updated lat
        final double Long = loc.getLongitude(); // Updated long


        ConnectMySQL obj = new ConnectMySQL();
        obj.call(lat,Long); // Call this method when location is updated and save the data.

        }

        else 
        {
            String latLongStr = "No lat and longitude found";
              Toast.makeText(this, "Your location is "+latLongStr ,Toast.LENGTH_LONG).show();
        }


    }
    public class MyLocationList implements LocationListener
    {

        public void onLocationChanged(Location arg0) {
            // TODO Auto-generated method stub
            UpdateWithNewLocation(arg0);
        }

        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(),"GPS Disable ", Toast.LENGTH_LONG).show();
        }

        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(),"GPS enabled", Toast.LENGTH_LONG).show();
        }

        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub

        }

    }
于 2012-06-12T09:54:01.553 回答