I have a 1st activity that call 2nd activity to get the latitude and longitude of the geo-location.
How to transfer the location that I get from 2nd activity back to 1st activity, so I can display it . Also I want to send it to mysql db on the remote.
This is my 1st activity that call 2nd activity :
public class Outletcheckin extends Activity {
// Progress Dialog
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
EditText inputOutletno;
EditText inputOutletname;
EditText inputOutletLongitude;
EditText inputOutletLatitude;
Button btnGetLocation;
Button btnOutletCheckin;
// url to create new product
private static String url_checkin = "http://192.168.0.245/vcirps/create_product.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
@Override
public void onCreate(Bundle savedInstanceState) {
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads().detectDiskWrites().detectNetwork()
// StrictMode is most commonly used to catch accidental disk or network access on the application's main thread
.penaltyLog().build());
super.onCreate(savedInstanceState);
setContentView(R.layout.checkin);
// Edit Text
inputOutletno = (EditText) findViewById(R.id.inputOutletno);
inputOutletname = (EditText) findViewById(R.id.inputOutletname);
// Create button
Button btnGetLocation = (Button) findViewById(R.id.btnGetLocation);
// button click event
btnGetLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Launching All products Activity
Intent i = new Intent(getApplicationContext(), LbsGeocodingActivity.class);
startActivity(i);
}
});
Button btnOutletCheckin = (Button) findViewById(R.id.btnOutletCheckin);
// button click event
btnOutletCheckin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// creating new product in background thread
new Checkin().execute();
}
});
}
/**
* Background Async Task to Create new product
* */
class Checkin extends AsyncTask<String, String, String {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Outletcheckin.this);
pDialog.setMessage("Check-in..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Creating product
* */
protected String doInBackground(String... args) {
String outletno = inputOutletno.getText().toString();
String outletname = inputOutletname.getText().toString();
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(Outletcheckin.this);
String username = sp.getString("username", "anon");
// String branchno = sp.getString("branchno", "anon");
// Building Parameters
List<NameValuePair params = new ArrayList<NameValuePair ();
params.add(new BasicNameValuePair("username", username));
// params.add(new BasicNameValuePair("branchno", branchno));
params.add(new BasicNameValuePair("outletno", outletno));
params.add(new BasicNameValuePair("outletname", outletname));
// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(url_checkin,
"POST", params);
// check log cat fro response
Log.d("Create Response", json.toString());
// check for success tag
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully created product
Intent i = new Intent(getApplicationContext(), AllProductsActivity.class);
startActivity(i);
// closing this screen
finish();
} else {
// failed to create product
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
pDialog.dismiss();
}
}
}
And this is my 2nd activities.:
public class LbsGeocodingActivity extends Activity {
private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1000000; // in Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 86400000; // in Milliseconds
protected LocationManager locationManager;
protected Button retrieveLocationButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.geolocation);
retrieveLocationButton = (Button) findViewById(R.id.retrieve_location_button);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MINIMUM_TIME_BETWEEN_UPDATES,
MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
new MyLocationListener()
);
retrieveLocationButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showCurrentLocation();
}
});
}
protected void showCurrentLocation() {
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
String message = String.format(
"Current Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude()
);
Toast.makeText(LbsGeocodingActivity.this, message,
Toast.LENGTH_LONG).show();
}
}
Thanks a lot for your advice. I'm new on android and programming.
use startActivityForResult(intent) and put the result in there.
The you can listen for onActivityResult in the first Activity and handle it there.
Links have been posted in your comments ;-)