2011-07-27 27 views
1

我工作的一个Android应用程序开发和我陷进了这一点:传递位置数据到另一个活动的Android

我有2个活动: 第一个叫CurrentLoc,它让我的当前位置并获得位置后,我点击一个按钮,带我到活动号码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) 
    { 
    } 


} 

}

+0

你有没有解决问题了吗? –

回答

5

使用Intent演员:你可以复制第l在拨打startActivity之前,使用Intent.putExtra进入Intent的经纬度。

编辑:实际上,位置是Parcelable,这样你就可以使用putExtra它直接传递到意图,就像这样:

@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来检索第二个活动中的值。

+0

感谢您的帮助,我做了同样的事情,但它没有奏效,我认为按钮是问题...所以我必须把onClick按钮...再次感谢 – Malik

+0

请看我的相关问题太:http://stackoverflow.com/questions/24851013/send-data-from-one-activity-to-another-regularly –

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"); 
+0

感谢您的帮助,但它没有工作,因为意图无法读取任何东西onLocationChanged方法,我不知道为什么????? – Malik

+0

你需要从onLocationChanged方法调用Intent吗?您可以在获取loc.getLatitude()和loc.getLongitude()和onButton时,将值分配给两个变量lat,lon。 – Rasel

+0

我试过了,但同样的问题仍然是.... onButton点击无法读取意图是(hg),因此我无法启动第二个活动..预先感谢... – Malik

相关问题