2012-05-26 23 views
1

在我的应用程序中,我想要获取移动用户的更新位置,并且我希望在某段时间的周期性间隔后或用户旅行某些(例如500米)距离之后不断地将其发送到服务器。我需要这些东西在后台完成。我知道这一点,我必须实施服务类。但我并没有完全知道如何做到这一点。我在这方面做了一些工作。 任何人都可以在这个问题上帮助我。 我在服务类中做了以下事情。如何发送更新的长/纬到android的服务器?

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 

    } 
} 

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

回答

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 

     } 

    } 
+0

简单易懂.....谢谢... – kyogs

+0

@kyogs谢谢哥们。 – Akshay

相关问题