0

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

Pingpong
  • 7,681
  • 21
  • 83
  • 209

1 Answers1

3

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

Leon
  • 8,404
  • 2
  • 9
  • 52
  • Can you provide links to IntentFilter, Intent.ActionSend, ComponentName, Categories? for the completeness of an answer. – Pingpong Apr 09 '19 at 20:25
  • Are users with MSFT ending from Microsoft? – Pingpong Apr 09 '19 at 20:25
  • @Pingpong I have update my answer, hope it helpful to you.MSFT is on behalf of Microsoft, not all MSFT is from Microsoft. – Leon Apr 10 '19 at 02:59
  • Do you have any updates? If the reply help to you, please mark the reply as answer. – Leon Apr 12 '19 at 01:34
  • If you have some questions about it, you could post it here, if not, please mark it as answer, it is very important for us. – Leon Apr 12 '19 at 01:35
  • Do you know how to send data to other app, but I don't want to lauch its activity? – Pingpong Apr 12 '19 at 14:15