-->

Passing an object from one application to another

2019-05-11 15:30发布

问题:

After lot of googeling, I could not find any way to pass a object from one application to other application.Though I know that we can pass object from one activty to other activity using Parcel but how to do this between applications? My object is this

public class MyObject
{
    private String name;
    public String getName()
    {
        return this.name;
    }
    public void setName(String name)
    {
        this.name=name;
    }
}

Then how to do this like we do for passing object between activities

intent.putExtra("key",new MyOject());

回答1:

I'm assuming you're in charge of both applications.

Which of these situations is true?

  • Both applications are dependent on a single data store
  • The applications are fairly independent, but the user might want to share some data from one to the other

If the first situation is true, the normal way would be to create an Android Content Provider (as suggested previously) or a Service, which exposes an API using the AIDL language. Both applications would then depend upon the Content Provider or the Service.

See here for information about AIDL and services: http://developer.android.com/reference/android/app/Service.html http://developer.android.com/guide/components/aidl.html

If however the applications are peers, then the existing answers about Intents are good. Either one application can trigger another simply using startActivity, or perhaps you want to provide a way for a user to choose to share the object from one to the other. Look at this: http://developer.android.com/training/sharing/send.html



回答2:

One suggestion could be using ContentProviders

from reference:

Content providers are one of the primary building blocks of Android applications, providing content to applications. They encapsulate data and provide it to applications through the single ContentResolver interface.

Another suggestion could be using SharedPreferences, with getSharedPreferences with a correct mode. But as you can see in the reference some modes are deprecated since API Level 17.

Hope this helps, if not comment below



回答3:

There is another best way to use these objects and methods in a library which is global and add that library to your android project.You can declare your own classesin that library,and anther feature is jar file you can place taht classes in jar file and you can re use it.... this link may be useful to you..............How to create your own library for Android development to be used in every program you write?



回答4:

If you have to share relatively very small data use Shared Preferences which is easy to implement.

public static void addData(Context context, String data) {
    SharedPreferences sharedPref = context.getSharedPreferences(app_name, 0);
    SharedPreferences.Editor editor = sharedPref.edit();
    editor.putString("Name", data);
    editor.commit();
    }

public static String getData(Context context) {
    SharedPreferences sharedPref = context.getSharedPreferences(app_name, 0);
    String fontSize = sharedPref.getString("Name", "default");
    return fontSize;
    }

Alternatively, you could also use a Content Provider if you have fairly large data.



回答5:

Unfortunately you can't share an object between applications as-is if its not Parcelable.

But you can do this with a help of services and AIDL: http://developer.android.com/guide/components/aidl.html



回答6:

Passing object b/w activities :

public class MyObject  implements Parcelable{

    private String name;
    public String getName()
    {
    return this.name;
    }
    public void setName(String name)
    {
     this.name=name;
    }

    public MyObject() {

    }

    // Parcelling part
    public MyObject(Parcel in){
        this.name = in.readString();
    }


    @Override
    public int describeContents() {
        return 0;
    }


    @Override
    public void writeToParcel(Parcel dest, int arg1) {
        dest.writeString(this.name);
    }



    public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
        public MyObject createFromParcel(Parcel in) {
            return new MyObject(in); 
        }

        public MyObject[] newArray(int size) {
            return new MyObject[size];
        }
    };
}


//set Intent
Intent i= new Intent();
MyObject  o;
i.putExtra("obj", o);



//getIntent
Bundle data = getIntent().getExtras();
MyObject obj = data.getParcelable("obj");