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

时间:2019-04-08 13:23:39

标签: android xamarin.android

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

1 个答案:

答案 0 :(得分:1)

在第一个应用程序中,您可能需要以下代码才能在第二个应用程序中打开特定活动。

            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);

在第二个应用程序中,打开特定活动。添加注释,您应该添加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();

    }

正在运行的演示有一个GIF。 enter image description here

有我的演示代码。您应该先运行reciverApp,然后再运行sendApp https://github.com/851265601/OpenAnotherActvityDemo

如果您对此情况有疑问,可以参阅一篇简单的文章,向其他应用程序发送简单数据。

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

此方案包含Intent / Intentfilter,如果您想了解有关它的更多详细信息,此链接将很有帮助。 https://developer.android.com/guide/components/intents-filters#PendingIntent