Android 第一个活动获取经纬度地理位置的第二个活动。如何将信息传输回第一个活动?

Android 第一个活动获取经纬度地理位置的第二个活动。如何将信息传输回第一个活动?,android,android-intent,transfer,Android,Android Intent,Transfer,我有一个称为第二个活动的第一个活动,用于获取地理位置的纬度和经度 如何将我从第二个活动获得的位置转移回第一个活动,以便我可以显示它。我还想把它发送到远程的mysql数据库 这是我的第一个活动,称为第二个活动: public class Outletcheckin extends Activity { // Progress Dialog private ProgressDialog pDialog; JSONParser jsonP

我有一个称为第二个活动的第一个活动,用于获取地理位置的纬度和经度

如何将我从第二个活动获得的位置转移回第一个活动,以便我可以显示它。我还想把它发送到远程的mysql数据库

这是我的第一个活动,称为第二个活动:

      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();
                }

            }
        }

非常感谢你的建议。我是android和编程新手

使用StartActivityForresultint并将结果放入其中

您可以在第一个活动中侦听onActivityResult并在那里处理它


链接已张贴在您的评论中-

我建议您阅读此页面:您可以使用startactivityforresult而不是startactivityuse setResult来添加数据并从onActivityResult获取数据。
      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();
                }

            }