0

I have found current location's address,i have to share it.. i am trying to share by the use of putExtra function.Can u guide me? I am sharing my code to share that obtained address of the current location.. what change do I need to make in my code to accomplish this?

protected void onRestart() {
    super.onRestart();
    gps = new GpsTracker(this);
    open();
}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    if (gps.canGetLocation()) {
        sd.setVisibility(View.VISIBLE);
    }
    open();
    locationshare();
}

private void open() {
    location.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            show();
            add();
        }
    });

}

private void locationshare() {

    share.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            display();
        }
    });
}

public void show() {
    String stringLatitude = String.valueOf(gps.latitude);
    String stringLongitude = String.valueOf(gps.longitude);
    latitude.setText("Latitude :" + stringLatitude);
    longitude.setText("Longitude :" + stringLongitude);
}

public void add() {
    geocoder = new Geocoder(this, Locale.ENGLISH);
    try {
        List<Address> addresses = geocoder.getFromLocation(gps.latitude,
                gps.longitude, 1);
        if (addresses.size() > 0) {
            Address returnedAddress = addresses.get(0);
            StringBuilder strReturnedAddress = new StringBuilder(
                    "Address:\n");
            for (int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++) {
                strReturnedAddress
                        .append(returnedAddress.getAddressLine(i)).append(
                                "\n");
            }
            address.setText(strReturnedAddress.toString());
        } else {
            address.setText("No Address returned!");
        }
    } catch (IOException e) {
        e.printStackTrace();
        address.setText("Canont get Address! Check Network Connection");
    }

}

public void display() {
    Intent sendIntent = new Intent();
    sendIntent.setAction(Intent.ACTION_SEND);

    sendIntent.putExtra(Intent.EXTRA_TEXT,
            "Here display to obtained address");
    sendIntent.putExtra(Intent.EXTRA_SUBJECT, "address");
    sendIntent.setType("text/plain");
    startActivity(Intent.createChooser(sendIntent, "Share Place via"));
}

}

mezoid
  • 28,090
  • 37
  • 107
  • 148
Pergin Sheni
  • 393
  • 2
  • 11

3 Answers3

1

It sounds like the question you have is more along the lines of how to pass data between activities. Is that correct, or am I misunderstanding your question? And it looks like you're on the right track.

If so, this provides a good example of passing data via extras in intents. https://stackoverflow.com/a/6707951/413254

In your add() method, you set the text in some address field... maybe a TextView?

address.setText(strReturnedAddress.toString());

Instead of doing that (or in addition to that), why don't you save that to a String instance variable and pass that value in the intent? You could also grab the text from that TextView.

Following this example, you'd do the following in activity where address is being fetched:

sendIntent.putExtra(Intent.EXTRA_TEXT, addressString);
// or 
sendIntent.putExtra(Intent.EXTRA_TEXT, address.getText().toString());

Note that in this example, Intent.EXTRA_TEXT is just a string. This serves as the "key" to retrieve the value in the next activity. So for example, if you needed to pass an address (String), latitude (double), and longitude(double) in an intent you could do so with the following:

sendIntent.putExtra("myAddressKey", addressString);
sendIntent.putExtra("myLatKey", latitude);
sendIntent.putExtra("myLngKey", longitude);

And then retrieve and use the extra in your other activity

Bundle extras = getIntent().getExtras();
if(extras !=null) {
    String value = extras.getString(Intent.EXTRA_TEXT);
    Double lat = extras.getDouble("myLatKey");
    Double lng = extras.getDouble("myLngKey");
}
Community
  • 1
  • 1
loeschg
  • 29,961
  • 26
  • 97
  • 150
  • +1.I can retrieve "123 Road Street" as text. But i need to access current location's address in this place. Please help me.. – Pergin Sheni Jan 23 '14 at 05:59
  • @user3172066 I updated my answer. Does that make sense? What object type is `address` in your example? – loeschg Jan 23 '14 at 06:07
  • thanks.. It is useful answer. But where to write these Intent codes? – Pergin Sheni Jan 23 '14 at 07:38
  • when i was writing this code sendIntent.putExtra(Intent.EXTRA_TEXT, addressString); – Pergin Sheni Jan 23 '14 at 09:51
  • when i was writing this code.... sendIntent.putExtra(Intent.EXTRA_TEXT, addressString);.... The "addressString" raising error.. Please help me to fix it... – Pergin Sheni Jan 23 '14 at 09:57
  • address = (TextView) findViewById(R.id.address); – Pergin Sheni Jan 23 '14 at 10:30
  • @user3172066 do you understand what I mean when I say "instance variable"? Try the second strategy I suggested using `address.getText().toString()`. – loeschg Jan 23 '14 at 16:09
  • I got current location of user.next I want to pass this address into share location activity so please help me what can i do????? – Pergin Sheni Jan 24 '14 at 05:29
  • @user3172066, I'm not sure what else to tell you without knowing what your specific confusion is. Did you try the suggestion I gave you? What happened? – loeschg Jan 24 '14 at 15:36
  • Yes i can share the location by using.. sendIntent.putExtra(Intent.EXTRA_TEXT, address.getText().toString()); – Pergin Sheni Jan 27 '14 at 06:14
  • i need to display longitude and latitude with the address. If i use sendIntent.putExtra(Intent.EXTRA_TEXT, longitude.getText().toString()); sendIntent.putExtra(Intent.EXTRA_TEXT, latitude.getText().toString()); It does not display. what changes can i make? – Pergin Sheni Jan 27 '14 at 06:18
0

Refer this link

http://developer.android.com/training/sharing/send.html

you have to parse your addresses.get(0).toString() at

sendIntent.putExtra(Intent.EXTRA_TEXT, "Here display to obtained address");

BSKANIA
  • 1,317
  • 15
  • 27
0

In case if you are looking to share your current location address to another application you can use Intent.ACTION_SEND and put the obtained address in the Intent.EXTRA_TEXT. If you want to share the address between the activities of a same application then you can share it like below

Intent intent = new Intent(Activity1.this, Activity2.class);
intent.putExtra("Address","The Obtained Address");

If you want to pass the location object then please refer this link

Community
  • 1
  • 1
  • In this code...intent.putExtra("Address","The Obtained Address");.. I need to change the obtained address based on our location.. It must be changed automaticaly. – Pergin Sheni Jan 23 '14 at 10:21