从一个应用程序传递对象到另一个 [英] Passing object from one application to another

查看:244
本文介绍了从一个应用程序传递对象到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从一个应用程序的活动传递一个自定义对象到另一个主要活动。

我能做到这一点使用intent.putExtra如下所示:

  putExtra(钥匙,OBJ);

这是显示错误,因为putExtra方法不支持此功能。如何才能实现这一目标?

任何帮助是AP preciated。

修改

下面的类的实例,我需要传递给另一个应用程序。我有对象OBJ如下,我需要传递:

  CounterAndNotifier的obj =新CounterAndNotifier(5,本)

obj是对象传递

 公共类CounterAndNotifier
{
私人INT计数器= 0;
私人诠释计数;
私人Runnable接口可运行;公共CounterAndNotifier(诠释计数,可运行可运行)
{
    this.count =计数;
    this.runnable =可运行;
}公共同步无效incrementCounter()
{
    反++;
    如果(反==计数){
        runnable.run();
    }
}
}


解决方案

更​​新2:实例构造函数:

  CounterAndNotifier计数器=新CounterAndNotifier(0,新SerializableRunnable(){
            @覆盖
            公共无效的run(){            }
        });

  SerializableRunnable可运行=新SerializableRunnable(){
        @覆盖
        公共无效的run(){
            / *运行code *在这里/
        }
});
CounterAndNotifier计数器=新CounterAndNotifier(0,可运行);

更新:Runnable接口是一个接口,而不是序列化本身。为了解决这个问题,创建这样一个新的类:

 公共抽象类SerializableRunnable实现Runnable,Serializable接口{
    / *没有什么需要何去何从* /
}

然后更改您的Runnable的引用SerializableRunnable,你应该能够序列化对象放到一个意图没有问题。

您新的对象应该是这样的:

 公共类CounterAndNotifier实现Serializable
{
    私人INT计数器= 0;
    私人诠释计数;
    私人SerializableRunnable可运行;    公共CounterAndNotifier(诠释计数,SerializableRunnable可运行)
    {
        this.count =计数;
        this.runnable =可运行;
    }    公共同步无效incrementCounter()
    {
        反++;
        如果(反==计数){
            runnable.run();
        }
    }
}

您还可以创建Runnable对象以相同的方式,只是一个不同的名称。

原来的答复:
你不能把一个普通对象转换为意图额外的费用。如果你的对象成员都是可序列化的类型,那么你可以只实施序列化接口,然后你可以把对象变成了意图。否则,你可以实施 Parceable 和扁平化你的对象变成一个包裹。

使用序列化需要更少的code,只要你的对象成员都是基本类型的变化。此外,你应该支票是由接收应用程序的支持,首先要确定你应该使用它。

它会是这个样子:

 公共类YourObject实现Serializable {    私有静态最后的serialVersionUID长= -6242440735315628245L;
    / *您的类成员* /}

添加到意图:

 意向意图=新的Intent();
intent.putExtra(YourKey,新YourObject());

走出的意图:

  YourObject的obj =(YourObject)intent.getSerializableExtra(YourKey);

I need to pass a custom object from an activity of one application to the main activity of another.

Will I be able to accomplish this using intent.putExtra as shown:

putExtra("key",obj);

It was showing error since putExtra method does not support this. How can this be achieved?

Any help is appreciated.

EDIT

The following class' instance, I need to pass to another application. I would have the object 'obj' as follows, which I need to pass:

CounterAndNotifier obj = new CounterAndNotifier(5, this)

obj is the object to be passed

public class CounterAndNotifier
{
private int counter = 0;
private int count;
private Runnable runnable;

public CounterAndNotifier(int count, Runnable runnable)
{
    this.count = count;
    this.runnable = runnable;
}

public synchronized void incrementCounter()
{
    counter++;
    if (counter == count) {
        runnable.run();
    }
}
}

解决方案

Update 2: Example constructor:

CounterAndNotifier counter = new CounterAndNotifier(0, new SerializableRunnable() {
            @Override
            public void run() {

            }
        });

or

SerializableRunnable runnable = new SerializableRunnable() {
        @Override
        public void run() {
            /* Running code here */
        }
});
CounterAndNotifier counter = new CounterAndNotifier(0, runnable);

Update: Runnable is an interface and isn't Serializable by itself. To get around this, create a new class like this:

public abstract class SerializableRunnable implements Runnable, Serializable {
    /* Nothing needs to go here */
}

Then change your references of Runnable to SerializableRunnable and you should be able to serialize your object into an intent without a problem.

Your new object would look like this:

public class CounterAndNotifier implements Serializable
{
    private int counter = 0;
    private int count;
    private SerializableRunnable runnable;

    public CounterAndNotifier(int count, SerializableRunnable runnable)
    {
        this.count = count;
        this.runnable = runnable;
    }

    public synchronized void incrementCounter()
    {
        counter++;
        if (counter == count) {
            runnable.run();
        }
    }
}

You can still create your Runnable objects the same way, just with a different name.

Original Answer: You can't put a regular object into an Intent extra. If your object members are all serializable types then you can just implement the Serializable interface and then you can put the object into the Intent. Otherwise you can implement Parceable and flatten your object into a Parcel.

Using Serializable requires much less code changes so long as your object members are basic types. Also, you should check was is supported by the receiving application first to determine which you should use.

It'd look something like this:

public class YourObject implements Serializable {

    private static final long serialVersionUID = -6242440735315628245L;
    /* Your class members */

}

Adding to the intent:

Intent intent = new Intent();
intent.putExtra("YourKey", new YourObject());

Getting out of the intent:

YourObject obj = (YourObject)intent.getSerializableExtra("YourKey");

这篇关于从一个应用程序传递对象到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆