首页 > 解决方案 > 我怎样才能在两个不同的活动中获得这些意图值

问题描述

mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
    @Override
    public void onInfoWindowClick(Marker marker) {
        int title = Integer.parseInt(marker.getTitle());
        Log.d("Result", String.valueOf(marker));
        //pass these value in two diffrent activities to update sqlite database
        Intent a = new Intent(MapsActivity.this, Abc.class);
        a.putExtra("customer_id", title);
        startActivity(a);
}

标签: android

解决方案


您必须创建两个Intents才能这样做。

Intent a = new Intent(MapsActivity.this, Abc.class);
a.putExtra("customer_id", title);
startActivity(a);

Intent b = new Intent(MapsActivity.this, <service-name>.class);
b.putExtra("customer_id", title);
startService(b);

在您的 Activity 中,按如下方式访问它:

@Override
 protected void onCreate(Bundle savedInstanceState) {
    ...
    int customerId = getIntent().getIntExtra("customer_id", 0);
    ...
 }

在您的服务中按如下方式访问它:

 @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
      final int customerId = intent.getIntExtra("customer_id",0);
      return Service.START_NOT_STICKY;
  }

推荐阅读