使用startActivityForResult整个Android应用 [英] Using startActivityForResult across Android applications

查看:160
本文介绍了使用startActivityForResult整个Android应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了,有一个应用程序的<意图过滤器/> ,以便其他应用程序可以使用启动 startActivityForResult()。在本次活动结束后它已创建了一个对象,它是这样的:

I have written an application that has an <intent-filter/> so that other applications can start it using startActivityForResult(). When this activity is finished it has created an object that is like:

(应用程序A)

 public class MyObject implements Serializable {
      private String name;
      private String[] items
 }

和设置是它的结果是:

(应用程序A)

 getIntent().putExtra("Extra_MyObject", myObject);
 setResult(RESULT_OK, getIntent());

所以这是recieving这一意图次活动有一个问题,它是怎么知道的意图额外名接受我将如何创建收到的目标丢回一个为MyObject 不在此应用程序的一部分?

So the second activity that is recieving this intent has an issue, how does it know the name of the intent extra to receive and how would I create the object received and cast it back into a MyObject that is not part of of this application?

(应用程序B)

  @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == MY_REQ_CODE){
        if(resultCode == RESULT_OK){
                 // So here how do I know what the intent data name is i.e. "Extra_MyObject"
                 // and how would I know the structure to cast the object back to a MyObject that isn't part of this project?

        }
    }
}

我也只是模仿为MyObject 类中的第二个应用程序,并将其转换为吗? 还是有其他的选择吗?

Would I just mimic the MyObject class in the second application and cast it to that? or are there other options?

推荐答案

您可以使用一个库如杰克逊到对象序列化到你,那么你反序列化的另一端一个JSON字符串。

You could use a library like Jackson to serialize your object to a JSON string which you then you deserialize on the other end.

这是更加灵活,将消除您的数据版本不一致被传来传去的问题,如果您决定添加,例如一个额外的字段。

It's much more flexible and will eliminate the problem of inconsistent versions of your data being passed around if you decide to add for instance an extra field.

此外,两个应用程序不再需要维持一个类在同一个命名空间,甚至由同一个名字称呼他们。

Also, the two apps no longer need to maintain a class in the same namespace or even call them by the same name.

和作为最后一点,你不再需要发布一个罐子里,只有你的文档就足够了。

And as a final note, you no longer need to publish a jar, only your documentation will suffice.

这篇关于使用startActivityForResult整个Android应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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