How to pass data both ways between different android applications?

后端 未结 6 1105
甜味超标
甜味超标 2021-02-15 10:38

What\'s the easiest way to pass string variables from one application to another and also return values back? I have access to the source code of both apps, but

相关标签:
6条回答
  • 2021-02-15 11:14
    public class MyParcelable implements Parcelable {
        // You can include parcel data types
        private int mData;
        private String mName;
    
        // We can also include child Parcelable objects. Assume MySubParcel is such a Parcelable:
        private MySubParcelable mInfo;
    
        // This is where you write the values you want to save to the `Parcel`.  
        // The `Parcel` class has methods defined to help you save all of your values.  
        // Note that there are only methods defined for simple values, lists, and other Parcelable objects.  
        // You may need to make several classes Parcelable to send the data you want.
        @Override
        public void writeToParcel(Parcel out, int flags) {
            out.writeInt(mData);
            out.writeString(mName);
            out.writeParcelable(mInfo, flags)
        }
    
        // Using the `in` variable, we can retrieve the values that 
        // we originally wrote into the `Parcel`.  This constructor is usually 
        // private so that only the `CREATOR` field can access.
        private MyParcelable(Parcel in) {
            mData = in.readInt();
            mName = in.readString();
            mInfo = in.readParcelable(MySubParcelable.class.getClassLoader());
        }
    
        public MyParcelable() {
            // Normal actions performed by class, since this is still a normal object!
        }
    
        // In the vast majority of cases you can simply return 0 for this.  
        // There are cases where you need to use the constant `CONTENTS_FILE_DESCRIPTOR`
        // But this is out of scope of this tutorial
        @Override
        public int describeContents() {
            return 0;
        }
    
        // After implementing the `Parcelable` interface, we need to create the 
        // `Parcelable.Creator<MyParcelable> CREATOR` constant for our class; 
        // Notice how it has our class specified as its type.  
        public static final Parcelable.Creator<MyParcelable> CREATOR
                = new Parcelable.Creator<MyParcelable>() {
    
            // This simply calls our new constructor (typically private) and 
            // passes along the unmarshalled `Parcel`, and then returns the new object!
            @Override
            public MyParcelable createFromParcel(Parcel in) {
                return new MyParcelable(in);
            }
    
            // We just need to copy this and change the type to match our class.
            @Override
            public MyParcelable[] newArray(int size) {
                return new MyParcelable[size];
            }
        };
    }
    

    READ HERE

    0 讨论(0)
  • 2021-02-15 11:27

    AIDL is one means of communication between two different applications using Interfaces

    http://developer.android.com/guide/components/aidl.html

    You can find a working sample in the below tutorial http://manishkpr.webheavens.com/android-aidl-example/

    0 讨论(0)
  • 2021-02-15 11:27

    You can exchange messengers between two services that belong to those apps (even if the apps are from two different packages) and communicate using those messengers.

    0 讨论(0)
  • 2021-02-15 11:28

    you can use ContentProvider.This is a better way than others.

    0 讨论(0)
  • 2021-02-15 11:29

    SharedPreferences might help you in this regard.

    0 讨论(0)
  • I have two apps that i pass data to/from.

    App1...
    Intent i = new Intent("com.xxx.yyy.MESSAGE");
    Bundle b = new Bundle();
    b.putString("AAA", getAAA());
    i.putExtra("MyData", b);
    startActivityForResult(i, "myProcess");
    

    nothing fancy there...

    App2...in onResume()...

     Intent i = getIntent();
     if (i != null && i.getAction().equals("com.xxx.yyy.MESSAGE") {
        ...get the data from the bundle
     }
    

    note that the AndroidManifest.xml (for App2) has the following entries for one of the activities

     <intent-filter>
        <action android:name="com.xxx.yyy.MESSAGE"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <data android:mimeType="text/plain"/>
     </intent-filter>
    
    0 讨论(0)
提交回复
热议问题