为什么我的代码没有在 Android 8+ (Oreo+) 中启动我的应用程序的固定快捷方式?

Why my code is not launching the pinned shortcut of my App in Android 8+ (Oreo+)?

我正在尝试固定我的应用程序的一些动态快捷方式,它们将在用户创建自定义时间时创建。

我有两个版本的代码,我使用 JavaScriptInterfaceWebView 启动代码,但其中 none 正在按预期工作,因为其中一个正在尝试打开 Play 商店,第二个说: "the App doesn't exist" 当我创建快捷方式时来自 App.

这个正在启动 Play 商店

[Export]
[JavascriptInterface]
public void PinCustomTime(string time)
{
    var manager = context.GetSystemService(Context.ShortcutService) as ShortcutManager;

    if (manager.IsRequestPinShortcutSupported)
    {
        try
        {
            //Create the new intent
            var intent = new Intent(Intent.ActionView);
            //Set the flag of the new task
            intent.AddFlags(ActivityFlags.NewTask);
            //Get the apps from the Play Store
            intent.SetData(Android.Net.Uri.Parse("market://details?id=" + context.PackageName));
            //Set the custom time as a variable
            intent.PutExtra("customTime", time);
            //Set the info of the shortcut
            var info = new ShortcutInfo.Builder(context, $"tmTimer_{DateTime.Now.ToString("yyMMddHHmmss")}")
                    .SetShortLabel("TM Timer")
                    .SetLongLabel("TM Timer")
                    .SetIcon(Icon.CreateWithResource(context, Resource.Drawable.iconInv))
                    .SetIntent(intent)
                    .Build();

            //Set values
            var successCallback = PendingIntent.GetBroadcast(context, /* request code */ 0,
            intent, /* flags */ 0);
            //Creates the shortcut
            manager.RequestPinShortcut(info, successCallback.IntentSender);
        }
        catch (System.Exception ex)
        {

        }
    }
}

这个是说应用不存在:

[Export]
[JavascriptInterface]
public void PinCustomTime(string time)
{
    var manager = context.GetSystemService(Context.ShortcutService) as ShortcutManager;

    if (manager.IsRequestPinShortcutSupported)
    {
        try
        {
            //Set the info of the shortcut with the App to open
            var info = new ShortcutInfo.Builder(context, $"tmTimer_{DateTime.Now.ToString("yyMMddHHmmss")}")
                    .SetShortLabel("TM Timer")
                    .SetLongLabel("TM Timer")
                    .SetIcon(Icon.CreateWithResource(context, Resource.Drawable.iconInv))
                    .SetIntent(new Intent(Intent.ActionView).SetData(Android.Net.Uri.Parse(context.PackageName)))
                    .Build();

            //Create the new intent
            var intent = manager.CreateShortcutResultIntent(info);
            intent.PutExtra("customTime", time);

            //Set values
            var successCallback = PendingIntent.GetBroadcast(context, /* request code */ 0,
            intent, /* flags */ 0);
            //Creates the shortcut
            manager.RequestPinShortcut(info, successCallback.IntentSender);
        }
        catch (System.Exception ex)
        {

        }
    }
}

我尝试了第三个代码,但那个代码试图打开任何不是我自己的应用程序。有没有人经历过类似的事情?或者知道我错过了什么?

我遵循了多个教程和示例,例如:

感谢您的支持。

P.S.:

当用户单击快捷方式时,将启动此 Intent:

new Intent(Intent.ActionView).SetData(Android.Net.Uri.Parse(context.PackageName))

要启动特定的 activity,将其替换为(在 java 中):

Intent i = new Intent(context.getApplicationContext(), MainActivity.class);
i.setAction(Intent.ACTION_VIEW);

C# 的翻译如下:

[Export]
[JavascriptInterface]
public void PinCustomTime(string time)
{
    var manager = context.GetSystemService(Context.ShortcutService) as ShortcutManager;

    if (manager.IsRequestPinShortcutSupported)
    {
        //Create the new intent
        var intent = new Intent(context, typeof(MainActivity));
        //Set the flag of the new task
        intent.SetAction(Intent.ActionView);
        //Set the Time
        intent.PutExtra("customTime", time);
        //Set the info of the shortcut
        var info = new ShortcutInfo.Builder(context, $"tmTimer_{DateTime.Now.ToString("yyMMddHHmmss")}")
                .SetShortLabel("TM Timer")
                .SetLongLabel("TM Timer")
                .SetIcon(Icon.CreateWithResource(context, Resource.Drawable.iconInv))
                .SetIntent(intent)
                .Build();

        //Set values
        var successCallback = PendingIntent.GetBroadcast(context, /* request code */ 0,
        intent, /* flags */ 0);
        //Creates the shortcut
        manager.RequestPinShortcut(info, successCallback.IntentSender);
    }
}

我得到了一些支持:

How to get MainActivity for an Intent created in another class in Xamarin.Droid project?