我想将数据从一个应用程序发送到另一个应用程序中的特定活动。
下面的代码将数据发送到另一个应用程序中的主要活动。但是我希望在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
中实现它时遇到了问题。
发布于 2019-04-09 00:12:14
在第一个应用程序中,您需要下面的代码来打开第二个应用程序中的特定活动。
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
这个场景包含意图/内容过滤器,如果你想知道更多关于它的细节,这个链接是有帮助的。https://developer.android.com/guide/components/intents-filters#PendingIntent
https://stackoverflow.com/questions/55574528
复制相似问题