【问题标题】:Passing location data to another activity in Android将位置数据传递给 Android 中的另一个活动
【发布时间】:2019-06-06 15:32:46
【问题描述】:

我正在开发一个 Android 应用程序,但我被困在这一点上:

我有 2 项活动: 第一个叫做 CurrentLoc,它让我获得当前位置,在获得位置后,我点击一个按钮,将我带到名为 Sms 的活动编号 2。

我需要做的是,当我单击按钮时,我想将我在第一个活动中收到的位置数据传递给第二个活动......

这是我第一个活动的代码:

    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)
        {
        }


    }

}

【问题讨论】:

  • 你的问题解决了吗?

标签: java android


【解决方案1】:

使用Intent 附加功能:您可以在调用startActivity 之前使用Intent.putExtra 将位置纬度和经度复制到Intent 中。

编辑:实际上,Location 是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...谢谢再次
  • 也请查看我的相关问题:stackoverflow.com/questions/24851013/…
【解决方案2】:

在您的 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 方法中读取任何内容,我不知道为什么?????
  • 您需要从 onLocationChanged 方法调用 Intent说;
  • 我试过了,但同样的问题仍然存在...... onButton 点击​​无法读取 (hg) 的意图,因此我无法开始第二个活动......提前致谢。 ..
  • @Rasel 请也查看我的相关问题:stackoverflow.com/questions/24851013/…
  • 你可以通过 outState.putParcelable("key", location) 原因 Location 实现 Parcelable 接口
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-28
  • 1970-01-01
  • 2011-09-14
相关资源
最近更新 更多