Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing data from one app to another one [duplicate]

I have 2 android apps:

app1 - Activity11 --> Activity12 --> Activity13

app2 - Activity21 --> Activity22 --> Activity23

I want to pass from one app to the second Activity of the other one, passing some datas.

app1 - Activity11 -->(switch to app2)--> Activity22 --> Activity23

What steps have i to follow? Do you know some tutorial?

I haven't write code for now, because I don't know where to start.

Thanks in advance.

like image 207
Salvatore Avatar asked May 24 '13 10:05

Salvatore


People also ask

Can apps take data from other apps?

Just as an app can send data to other apps, it can also receive data from other apps as well. Think about how users interact with your application and what data types you want to receive from other applications.

Does clear data delete everything?

Clear cache: Deletes temporary data. Some apps can open slower the next time you use them. Clear data storage: Permanently deletes all app data. We recommend trying to delete from inside the app first.

How can I transfer data from another app in Android?

It is nowadays a common feature in Android social app. To receive data from another app, it is necessary to configure your Activity at AndroidManifest. xml properly and handle the Intent containing the action and data sent from system. Implement this sharing feature only needs 2 step and cost around 10 mins.


1 Answers

You need to look into android Intents and Intent Filters. Check this training article for example.

This blog post can also be an interesting read.

A quick example for sending text (other types are in the links on top);

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(sendIntent);

And for receiving:

void onCreate (Bundle savedInstanceState) {
    ...
    // Get intent, action and MIME type
    Intent intent = getIntent();
    String action = intent.getAction();
    String type = intent.getType();

    if (Intent.ACTION_SEND.equals(action) && type != null) {
        if ("text/plain".equals(type)) {
            handleSendText(intent); // Handle text being sent
        } else if (type.startsWith("image/")) {
            handleSendImage(intent); // Handle single image being sent
        }
    } else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) {
        if (type.startsWith("image/")) {
            handleSendMultipleImages(intent); // Handle multiple images being sent
        }
    } else {
        // Handle other intents, such as being started from the home screen
    }
    ...
}

void handleSendText(Intent intent) {
    String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
    if (sharedText != null) {
        // Update UI to reflect text being shared
    }
}

void handleSendImage(Intent intent) {
    Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
    if (imageUri != null) {
        // Update UI to reflect image being shared
    }
}

void handleSendMultipleImages(Intent intent) {
    ArrayList<Uri> imageUris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
    if (imageUris != null) {
        // Update UI to reflect multiple images being shared
    }
}

You'll have to update the manifest also:

<activity android:name=".ui.MyActivity" >
    <intent-filter>
        <action android:name="android.intent.action.SEND" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="image/*" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.SEND" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="text/plain" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.SEND_MULTIPLE" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="image/*" />
    </intent-filter>
</activity>
like image 60
hcpl Avatar answered Nov 08 '22 18:11

hcpl