[英]1st activity 2nd activity to get the latitude longitude geo-location. How to transfer info back to 1st activity?
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. 我也想将其发送到远程的mysql db。
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. 我是android和编程新手。
use startActivityForResult(intent) and put the result in there. 使用startActivityForResult(intent)并将结果放在那里。
The you can listen for onActivityResult in the first Activity and handle it there. 您可以在第一个Activity中监听onActivityResult并在那里进行处理。
Links have been posted in your comments ;-) 链接已张贴在您的评论中;-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.