简体   繁体   English

第一项活动第二项活动获得纬度经度地理位置。 如何将信息转移回第一活动?

[英]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.

相关问题 如何从第二活动到第一活动获取数据 - How to get data from 2nd Activity to 1st Activity 如何在第一个活动中调用第二个活动值? - how to call 2nd activity value in 1st activity? 按下后如何将数据从第二个活动传递到第一个活动? - android - How to pass data from 2nd activity to 1st activity when pressed back? - android 当我返回到第一个活动时,第二个活动未调用第二个活动的onCreate()函数 - When I return back to 1st Activity the onCreate() function of 2nd Activity is not called in 2nd time 将第1个活动的值传递给android中的第2个活动 - Pass the value from 1st activity to 2nd activity in android 完成第二个活动(由第一个活动调用)后如何在第一个活动中调用onCreate方法 - How to call onCreate method in 1st activity after finishing 2nd activity(which is called by the 1st activity) 从第一个活动转到第二个活动时出错 - error when going from 1st activity to 2nd activity 如何在不按后退按钮和关闭按钮的情况下将数据从第二个活动传递到第一个(弹出窗口)? - How to pass data from 2nd activity to 1st (Popup) without press back button and close button? 从Android中的第一个应用程序开始第二个应用程序的活动 - Start Activity of 2nd app from 1st app in android 从第一活动到第四活动的信息 - info from 1st activity to a fourth activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM