在Android中向另一个活动传递位置数据

4
我正在进行Android应用程序开发,目前遇到了困难:
我有两个活动: 第一个名为CurrentLoc,它可以获取我的当前位置,获取位置后,我点击按钮进入第二个名为Sms的活动。
我需要做的是当我点击按钮时,将第一个活动中接收到的位置数据传递给第二个活动...
下面是第一个活动的代码:
    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.setOnClickListener(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)
        {
        }


    }

}

你解决了你的问题吗? - Dr.jacky
2个回答

9
使用Intent的额外功能:在调用startActivity之前,您可以使用Intent.putExtra将位置纬度和经度直接复制到Intent中。
编辑:实际上,位置是可序列化的Parcelable对象,因此您可以使用putExtra直接将其传递给Intent,如下所示:
@Override
    public void onLocationChanged(Location loc)
    {
        passToActivity(log);
    }

然后定义passToActivity如下:

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

   // configure the intent as appropriate

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

然后您可以使用getParcelableExtra在第二个Activity中检索该值。


谢谢你的帮助,我尝试了同样的方法,但是没有成功,我认为按钮可能是问题所在...那么我应该在哪里放置按钮的onClick呢?再次感谢。 - Malik
请也查看我的相关问题:http://stackoverflow.com/questions/24851013/send-data-from-one-activity-to-another-regularly - Dr.jacky

1

在你的FirstActivity中

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

在第二个活动中
Bundle bundle = getIntent().getExtras();
             double lat=bundle.getDouble("latitude");
             double lon=bundle.getDouble("longitude");

谢谢你的帮助,但是它没有起作用,因为Intent无法从onLocationChanged方法中读取任何内容,我不知道为什么? - Malik
你需要从onLocationChanged方法中调用Intent吗?你只需在获取loc.getLatitude()和loc.getLongitude()的同时将值分配给两个变量lat和lon,然后在按钮点击时按照我说的方式调用活动即可。 - Rasel
我尝试过了,但是仍然存在同样的问题...onButton点击无法读取意图(hg),因此我无法启动第二个活动...提前致谢... - Malik
@Rasel 请也看一下我的相关问题:http://stackoverflow.com/questions/24851013/send-data-from-one-activity-to-another-regularly - Dr.jacky
您可以使用outState.putParcelable("key", location)将其传递,因为Location实现了Parcelable接口。 - Edhar Khimich

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接