I’m working on organizing my application and I need some help sending the GPS coordiates to another class that sends them to a database. When the send button in the Main activity is pressed, it starts an intent that begins the LocationActivity. From there, I’d like to send the GPS coordinates to the SendActivity which sends them to a database (through a PHP program). How do I send the coordinates to SendActivity? I comment a section where I tried but it doesn’t work. Any help would be appreciated. Thanks!
LocationActivity.java:
public class LocationActivity extends Activity{
private LocationManager locManager;
private LocationListener locListener;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startLocation();
}
private void startLocation()
{
//get a reference to the LocationManager
locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
//checked to receive updates from the position
locListener = new LocationListener() {
public void onLocationChanged(Location location) {
SendActivity.send(location); //What to do here?
}
public void onProviderDisabled(String provider){
//labelState.setText("Provider OFF");
}
public void onProviderEnabled(String provider){
//labelState.setText("Provider ON ");
}
public void onStatusChanged(String provider, int status, Bundle extras){
//Log.i("", "Provider Status: " + status);
}
};
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);
}
}
SendActivity.java:
public class SendActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
void send(Location loc)
{
Log.i("", String.valueOf(loc.getLatitude() + " - " + String.valueOf(loc.getLongitude())));
String lat = String.valueOf(loc.getLatitude());
String lon = String.valueOf(loc.getLongitude());
SharedPreferences shared = getSharedPreferences("PEOPLE_PREFERENCES", MODE_PRIVATE);
final String usr_id2 = shared.getString("USR_ID", "none");
if (lat != "0" && lon != "0")
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://example.com/test/example.php");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("lat", lat));
nameValuePairs.add(new BasicNameValuePair("lon", lon));
nameValuePairs.add(new BasicNameValuePair("id", usr_id2));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost);
}
catch (ClientProtocolException e) {
// TODO Auto-generated catch block
}
catch (IOException e) {
// TODO Auto-generated catch block
}
}
}
}
No need to create an Activity to get LatLong..
Following is the class to get LatLong :