繁体   English   中英

将数据从一个应用程序发送到另一个应用程序中的特定活动

[英]Send Data From One App to A Specific Activity in Another App

我想将数据从一个应用程序发送到另一个应用程序中的特定活动。

下面的代码将数据发送到另一个应用程序中的主要活动。 但是我想在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();

以下是适用于android的应用程序,但在Xamarin Android实现它存在问题。

Android:另一个应用程序的通话活动

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

            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。 在此处输入图片说明

有我的演示代码。 您应该先运行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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM