我不能让OnNewIntent开火。我已经阅读了几十篇关于这个问题的文章,并尝试了所有的代码组合。
无论我使用LaunchMode.SingleTask还是SingleTop,它都不会触发,总是通过OnCreate方法传递。
我在这里做错了什么?我是不是遗漏了什么?我需要添加什么才能使其正常工作?
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.OS;
using Android.Runtime;
using System;
using System.Threading.Tasks;
using Xamarin.Forms;
using static MyApp.ClipboardMgr;
namespace MyApp.Droid
{
//[Activity(Label = "SplashActivity")]
[Activity(LaunchMode = LaunchMode.SingleTask, Theme = "@style/Theme.Splash",
MainLauncher = true, NoHistory = true)]
//Can't get this to work with LaunchMode.SingleTop or SingleTask. Always creates a new instance.
[IntentFilter(new[] { Intent.ActionProcessText },
Categories = new[] { Intent.CategoryDefault },
DataMimeType = @"text/plain", Icon = "@drawable/icon", Label = "MyApp")]
public class SplashActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
try
{
//taking these out for readability
//AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
//TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
//AndroidEnvironment.UnhandledExceptionRaiser += AndroidEnvironment_UnhandledExceptionRaiser;
base.OnCreate(savedInstanceState);
// Create your application
StartActivity(typeof(MainActivity));
//don't want to do this here, better to do it in event
if (Intent.Action == Intent.ActionProcessText)
{
//always comes here
HandleProcessTextIntent();
}
}
catch(Exception e)
{
App.LogException(e);
throw;
}
}
/// <summary>
/// this is not firing!
/// </summary>
/// <param name="intent"></param>
protected override void OnNewIntent(Intent intent)
{
base.OnNewIntent(intent);
HandleProcessTextIntent();
}
void HandleProcessTextIntent()
{
string input = Intent.GetStringExtra(Intent.ExtraProcessText).Trim();
if (input == string.Empty)
return;
ClipMgr.SetText(input);
}
}
}发布于 2020-06-01 14:00:12
你没有错过任何东西,因为当我用我自己的意图测试你的activity时,它工作得很好,并且当使用SingleTask或SingleTop时会调用OnNewIntent(),下面是我如何从另一个项目调用activity的方法(我是在本地安卓系统中做的):
ComponentName component = new ComponentName("com.example.textHandlerDemo", "com.example.textHandlerDemo.TextHandlerActivity");
Intent intent = new Intent();
intent.setAction(Intent.ACTION_PROCESS_TEXT);
intent.setComponent(component);
startActivity(intent);我做了一些研究,发现了一些可以解释为什么OnNewIntent()永远不会被触发的原因:
根据the guidance of ACTION_PROCESS_TEXT的说法,它提到:
您可以将此作为提示,以提供将更改的文本返回到发送应用程序的功能,从而替换选定的文本。这是因为您的活动实际上是使用startActivityForResult()启动的
所以,当你在浮动文本选择工具栏中点击你的应用程序的标签时,安卓将启动你在startActivityForResult(),中注册到ActionProcessText的活动,并使用startActivityForResult(),你的活动将作为当前活动的一个子活动启动(从Android doc of startActivityForResult中暗示,也就是说,安卓将把你的活动放入当前应用的堆栈中,而不是重定向到已经运行的应用程序。
这样,您的目标活动仍将从OnCreate()调用,因为它与正在运行的应用程序隔离。
此外,浮动文本选择工具栏用于在应用程序中提供快速的文本操作体验,如this blog中提到的那样,我没有找到任何开关来启用/禁用重定向到应用程序,如果你使用的是LaunchMode.SingleTask或SingleTop,你可能需要在OnCreate()和OnNewIntent()中处理意图。
https://stackoverflow.com/questions/62028716
复制相似问题