Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass data both ways between different android applications?

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 it has to be two different applications.

I tried with startActivityForResult, but this only seems to work between activies of the same application. When calling an activity from a different package, startActivityForResult returns immediately with RESULT_CANCELED. There seems to be the possibility to solve this with a Service, but isn't that a bit oversized for just some string vars?

Is there an easy and clean way to do this?

Here 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's 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>

Anybody seeing the mistake? App B always returns immediately with RESULT_CANCELED

EDIT: Right now I'm getting a android.content.activitynotfoundexception no activity found to handle intent { act=com.example.testapp.MESSAGE (has extras) } error. What am I doing wrong?

like image 831
qefzec Avatar asked Sep 23 '13 18:09

qefzec


People also ask

How do you store and pass data between activities in Android?

The easiest way to do this would be to pass the session id to the signout activity in the Intent you're using to start the activity: Intent intent = new Intent(getBaseContext(), SignoutActivity. class); intent. putExtra("EXTRA_SESSION_ID", sessionId); startActivity(intent);


2 Answers

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/

like image 101
Code_Yoga Avatar answered Sep 30 '22 14:09

Code_Yoga


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

like image 37
Ranjit Avatar answered Sep 30 '22 14:09

Ranjit