繁体   English   中英

无论如何,是否有意在没有开始活动的情况下额外发送意图数据?

[英]Is there anyway to send data in an intent put extra without starting an activity?

因此,我从Web服务获取数据并使用RecyclerView适配器。在RecyclerView.adapter的onBindviewholder方法中,我想将数据传递给MainActivity中的recyclerView,但也要将数据(项目)传递给MyMapsActivity.onBindViewholder具有setOnClickListener和setOnLongClickListener作为正在使用的控件。 有没有一种方法可以使用无法启动活动的意图来发送数据(项目),还是有一种方法可以在onBindViewholder方法中连接新按钮,因为发生的事情是在应用程序启动时直接进入MyMapsActivity这是预期的。 有没有一种方法可以通过使用可以与onBindViewholder进行交互的新按钮来控制它,还是有一种无需启动MyMapsActivity就可以传递intent.putExtra的方法。也许我的理解是有缺陷的,但这是我的方法和活动代码:

onBindViewholder

public void onBindViewHolder(DataItemAdapter.ViewHolder holder, int position) {
        final DataItem item = mItems.get(position);


    try {
        holder.titletext.setText(item.getTitle());
        holder.companyText.setText(item.getCompany());
        holder.cityText.setText(item.getCity());
        holder.salarytext.setText(""+ item.getSalary());
        holder.descriptionText.setText(item.getDescription());
        holder.responsibilityText.setText(item.getResponsibility());
        holder.latText.setText(""+ item.getLatitude());
        holder.lngText.setText(""+ item.getLongitude());
        holder.phoneText.setText(item.getPhone());
        holder.provinceText.setText(item.getProvince());
    } catch (Exception e) {
        e.printStackTrace();
    }
    //click
    holder.mView.setOnClickListener(new View.OnClickListener() {//getting viewholder class and ctor
        @Override
        public void onClick(View v) {


            Intent intent = new Intent(mContext,DetailsActivity.class);
            intent.putExtra(ITEM_KEY,item);
           mContext.startActivity(intent);
        }
    });
    //long click
    holder.mView.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            Toast.makeText(mContext, "long click: " + item.getTitle(), Toast.LENGTH_SHORT).show();
            return false;
        }
    });

        // !!!!!----this is the intent Im talking about----!!!
        Intent intent = new Intent(mContext,MyMapActivity.class);
        intent.putExtra(ITEM_KEY,item);
         mContext.startActivity(intent);

}

我的地图方法:

    public void onMapReady(GoogleMap googleMap) {
            final DataItem item = getIntent().getExtras()
.getParcelable(DataItemAdapter.ITEM_KEY); //--------gets intent frm dataItemAdapter

    if (item == null) {
        throw new AssertionError("null data recived");
    }
    mapReady = true;
    mMap = googleMap;
    LatLng latLngToronto = new LatLng(43.733092, -79.264254);
   // LatLng latLnghome = new LatLng(43.656729, -79.377162);

    CameraPosition target = CameraPosition.builder().target(latLngToronto).zoom(15).tilt(65).build();
    mMap.moveCamera(CameraUpdateFactory.newCameraPosition(target));
    mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);


    markerDisplay(item.getTitle(),item.getLatitude(),item.getLongitude());//------maps jobs marker-------new


    //add markers and
    //instantiate
   //   mMap.addMarker(mp);
  //  mMap.addMarker(md);


} 

您不需要调用startAcivity方法。 有sendBroadcast方法用于与其他组件共享意图。

 String CUSTOM_ACTION = "com.example.YOUR_ACTION";

 Intent i = new Intent();
 i.setAction(CUSTOM_ACTION)
 intent.putExtra(ITEM_KEY,item);
 context.sendBroadcast(intent);

然后,您可以使用BroadcastReceiver接收您的意图

public class MyReciever extends BroadcastReceiver {
@Override
    public void onReceive(Context context, Intent intent) { 
        String action = intent.getAction();
        if(action!=null && action.equals(CUSTOM_ACTION)){
           //do something 
        }
    }
}

使用自定义IntentFilter将接收器注册到您活动的onCreate方法中

IntentFilter filter = new IntentFilter(CUSTOM_ACTION);
registerReceiver(myReceiver, filter);

并且不要忘记在onDestroy上取消注册

unregisterReceiver(myReceiver);

PS

有很多方法可以在类和组件之间传输数据。 这取决于您的目的。
具有意图的广播接收器非常适合服务和活动之间的数据传输以及应用程序之间的数据传输。

如果您需要从不同位置访问数据,则可以使用其他方法。
如果数据需要存储在磁盘上,那么该数据库是合适的。
要将数据存储在RAM中,可以使用单独的类,并将该类存储为Singleton或存储到Application类中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM