Pingpong
Pingpong

Reputation: 8009

Send Data From One App to A Specific Activity in Another App

I want to send data from one app to a specific activity in another app.

The code below sends data to the main activity in another app. But I want to specify an activity within IntentReceiver.

IntentSender

Intent intent = this.ApplicationContext.PackageManager.GetLaunchIntentForPackage("com.IntentReceiver");
intent.PutExtra("Message", "Hello");
StartActivity(intent);

IntentReceiver

var message = Intent.GetStringExtra("Message");
Toast.MakeText(this, $"OnResume {message}", ToastLength.Long).Show();

Below is for android, but I have problem implementing it in Xamarin Android.

Android : Call activity of another application

Upvotes: 0

Views: 632

Answers (1)

Leon Lu
Leon Lu

Reputation: 9234

In the first app, you can need the following code to open the specific activity in second app.

            Intent intent = new Intent("android.intent.action.SEND");
            //this first parameter is pageckage name of secound app,the secound parameter is specific activiy name totally

            ComponentName componentName = new ComponentName("reciverApp.reciverApp", "reciverApp.reciverApp.Activity1");
            intent.SetComponent(componentName);
            intent.PutExtra("Message", "Hello");
            StartActivity(intent);

In the second app, open the specific activity. Add annotation,you should add Exported = true Name = "reciverApp.reciverApp.Activity1" IntentFilter

    [Activity(Label = "Activity1",Exported = true,Name = "reciverApp.reciverApp.Activity1")   ]
[IntentFilter(new[] { Intent.ActionSend }, Categories = new[] { Intent.CategoryDefault })]
public class Activity1 : Activity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.layout1);


    }
    protected override void OnResume()
    {

        var message = Intent.GetStringExtra("Message");
        if (message != null)
        {

            Toast.MakeText(this, $"OnResume {message}", ToastLength.Long).Show();
        }
        base.OnResume();

    }

There is a GIF of my running demo. enter image description here

There is code of my demo. You should run the reciverApp firstly, then run the sendApp https://github.com/851265601/OpenAnotherActvityDemo

If you have some doubts about this circumstances, there is a simple article about sending simple data to other apps.

https://developer.android.com/training/sharing/send

This scenario contains Intent/Intentfilter, If you want to know more details about it, this link is helpful. https://developer.android.com/guide/components/intents-filters#PendingIntent

Upvotes: 3

Related Questions