How to transfer data between different Android applications?

What is 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 applications, but it should be two different applications.

I tried with startActivityForResult, but this only seems to work between actions of the same application. When invoking an operation from another package, startActivityForResult immediately returns RESULT_CANCELED. It seems there is an opportunity to resolve this using the Service, but is it not too simple for some lines?

Is there a simple and clean way to do this?

Here is the code I tried to use for startActivityForResult:

//App A: Intent intent = new Intent(); intent.setAction("com.example.testapp.MESSAGE"); Bundle b = new Bundle(); b.putString("loginToken", "263bhqw3jhf6as4yf8j0agtz8h2hj2z9j3hg3g3ggh34uzh2h2ui78h3i9wdnj89x"); intent.putExtra("MyData", b); startActivityForResult(intent, TEST_REQUEST); @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { Log.d("pairing", "onActivityResult called"); // Check which request we're responding to if (requestCode == TEST_REQUEST) { // Make sure the request was successful Log.d("pairing", "got result, resultCode: " + resultCode); if (resultCode == RESULT_OK) { // The Intent data Uri identifies which contact was selected. if (data.hasExtra("returnMessage")) { Toast.makeText(this, data.getExtras().getString("returnMessage"), Toast.LENGTH_LONG).show(); } } } } // App B: Intent result = new Intent(); Bundle b = new Bundle(); b.putString("returnValue", "this is the returned value"); result.putExtra("MyData", b); setResult(Activity.RESULT_OK, result); Log.d("pairing", "RESULT_OK set"); finish(); //App B Manifest <activity android:name="com.example.testapp" android:launchMode="singleTop" android:screenOrientation="portrait" android:windowSoftInputMode="adjustPan" > <intent-filter> <action android:name="com.example.testapp.MESSAGE" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="text/plain" /> </intent-filter></activity> 

Does anyone see a mistake? Appendix B always returns immediately with RESULT_CANCELED

EDIT: Right now, I am getting android.content.activitynotfoundexception. No activity was found to process the intent {act = com.example.testapp.MESSAGE (has additional features)}. What am I doing wrong?

+6
source share
6 answers

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

+1
source

AIDL is one of the 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 tutorial below http://manishkpr.webheavens.com/android-aidl-example/

+2
source

SharedPreferences can help you in this regard.

0
source

I have two applications that I transfer 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 unusual 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 AndroidManifest.xml (for App2) has the following entries for one of the actions

  <intent-filter> <action android:name="com.xxx.yyy.MESSAGE"/> <category android:name="android.intent.category.DEFAULT"/> <data android:mimeType="text/plain"/> </intent-filter> 
0
source

You can exchange messages between two services belonging to these applications (even if the applications are from two different packages) and exchange data with these messengers.

0
source
 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
source

Source: https://habr.com/ru/post/954442/


All Articles