简体   繁体   English

在不同过程中的两个活动之间传递数据

[英]Pass data between two Activities in different procceses

I need to pass Objects between two different processes, I read all the materials about it but I still have some questions 我需要在两个不同的过程之间传递Objects ,我阅读了有关它的所有材料,但仍然有一些疑问

1 .I can pass objects between different processes through AIDL , but its complicated and messy, I also read about he Messenger 1。我可以通过AIDL在不同processes之间传递对象,但是它复杂且混乱,我还阅读了有关Messenger

to transfer messages, but cant I pass with it an Object that implements Parcelable like that? 传输消息,但是我不能通过它传递实现这样的ParcelableObject吗?

Bundle b = new Bundle();
b.putParcelable("MyObject", (Parcelable) object);
msg.setData(b);

handler.sendMessage(msg);

And in the Handler to extract it? 并在Handler中提取它? if yes so what is the advance of AIDL over Messenger ...only the multithreading issue? 如果是的话,那么AIDLMessenger有何进步?仅是多线程问题?

2 .To work across different processes I can with Messenger and AIDL , but cant I use Bundle in Activities without all those services like some Android app work today that can get objects as input: 2。要跨不同的processes工作,我可以使用MessengerAIDL ,但不能在没有所有这些服务(例如某些Android应用程序)的服务的情况下使用Bundle in Activities ,这些服务可以获取对象作为输入:

intent.putExtra("MyParcelableObject", object);

First of all implement java.io.Serializable interface in your POJO class, like this: 首先在POJO类中实现java.io.Serializable接口,如下所示:

public class MyPojo implements Serializable {

}

Secondly, in your MainActivity where you want to pass objects write something like this: 其次,在您要传递对象的MainActivity中 ,编写如下代码:

Bundle bundle = new Bundle();
bundle.putSerializable("",""); // to hold list
bundle.putInt("",""); // to hold int
bundle.putString("", ""); // to hold String
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra(bundle);
startActivity(intent);

Finally, in your SecondActivity you can get/read data, in the following way 最后,在SecondActivity中,您可以通过以下方式获取/读取数据

Bundle bundle = getIntent().getExtras();
ArrayList<MyPojo> arrayList= (ArrayList<MyPojo>) bundle.getSerializable("list");
int myPosition = bundle.getInt("position");
String strValue = bundle.getString("myString");

If you just want to pass data between components residing in two different processes you can use the similar semantics as the ones used to share objects between two activities.For example, communicating between activity in Process 1 to a service in Process 2 can happen in following manner: 如果只想在驻留在两个不同进程中的组件之间传递数据,则可以使用与在两个活动之间共享对象所使用的语义相似的语义。例如,在以下过程中可能发生进程1中的活动与进程2中的服务之间的通信方式:

From the activity in process 1 来自过程1中的活动

 Intent mIntent = new Intent(this, MyService.class);
    Bundle extras = mIntent.getExtras();
    extras.putString(key, value); 
    startService(mIntent);

In the service of process 2 为流程2服务

public int onStartCommand (Intent intent, int flags, int startId)
{
     super.onStartCommand(intent, flags, startId);

     Bundle bundle = intent.getExtra();

}

Also while sharing objects use Parcelable instead of Serializable as they are more lightweight. 同样,在共享对象时,请使用Parcelable而不是Serializable,因为它们更轻巧。

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

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