开发者

Passing location data to another activity in Android

开发者 https://www.devze.com 2023-03-23 17:11 出处:网络
I\'m working on an Android application development and i\'m stuck in this point: I have 2 activities: First one called CurrentLoc and it gets me the current position and after getting the position i

I'm working on an Android application development and i'm stuck in this point:

I have 2 activities: First one called CurrentLoc and it gets me the current position and after getting the position i click on a button that take me to activity number 2 which called Sms.

What i need to do is that i want to pass the position data that i have received in the first activity to the second activity when i click the button...

Here is my code for the first activity :

    public class Tester2Activity extends Activity { 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        startService(new Intent(Tester2Activity.this,SS.class));

        LocationManager mlocManager = (LocationManager)getSystemService       (Context.LOCATION_SERVICE);
        LocationListener mlocListener = new MyLocationListener();
        mlocManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, 0, 0,     mlocListener);


        Button button1 = (Button) findViewById(R.id.widget30);
         button1.set开发者_Python百科OnClickListener(new View.OnClickListener() {
              public void onClick(View v) {

                  Intent hg = new Intent(Tester2Activity.this, Sms.class);
                  startActivity(hg);



        }
    });



        public class MyLocationListener implements LocationListener
    {

        @Override
        public void onLocationChanged(Location loc)
        {
            loc.getLatitude();
                       loc.getLongitude();

                  //This is what i want to pass to the other activity when i click on the button


        }


        @Override
        public void onProviderDisabled(String provider)
        {

        }

        @Override
        public void onProviderEnabled(String provider)
        {

        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras)
        {
        }


    }

}


Use the Intent extras: you can just copy the location latitude and longitude into the Intent using Intent.putExtra before you call startActivity.

EDIT: actually, Location is Parcelable, so you can pass it directly to the Intent using putExtra, like this:

@Override
    public void onLocationChanged(Location loc)
    {
        passToActivity(log);
    }

and then define passToActivity as

void passToActivity(Location loc)
{
   Intent i = new Intent();

   // configure the intent as appropriate

   // add the location data
   i.putExtra("LOCATION", loc);
   startActivity(i);
}

and then you can use getParcelableExtra to retrieve the value in the second Activity.


In your FirstActivity

Intent hg = new Intent(Tester2Activity.this, Sms.class);
hg.putExtra("latitude",""+latitude);
hg.putExtra("longitude",""+longitude);
startActivity(hg);

In the Second Activity

Bundle bundle = getIntent().getExtras();
             double lat=bundle.getDouble("latitude");
             double lon=bundle.getDouble("longitude");
0

精彩评论

暂无评论...
验证码 换一张
取 消