我正在进行Android应用程序开发,我陷入了这一点:
我有2个活动:第一个叫做CurrentLoc,它让我获得当前位置,在获得位置后,我点击一个按钮,将我带到2号活动,称为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)
{
}
}
}
Run Code Online (Sandbox Code Playgroud)
}
使用Intent附加功能:您可以Intent.putExtra在调用之前将位置纬度和经度复制到Intent中startActivity.
编辑:实际上,位置是Parcelable,所以你可以使用putExtra将它直接传递给Intent ,如下所示:
@Override
public void onLocationChanged(Location loc)
{
passToActivity(log);
}
Run Code Online (Sandbox Code Playgroud)
然后定义passToActivity为
void passToActivity(Location loc)
{
Intent i = new Intent();
// configure the intent as appropriate
// add the location data
i.putExtra("LOCATION", loc);
startActivity(i);
}
Run Code Online (Sandbox Code Playgroud)
然后你可以使用getParcelableExtra来检索第二个Activity中的值.
| 归档时间: |
|
| 查看次数: |
4898 次 |
| 最近记录: |