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

问题描述

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

我有两个版本的代码,我正在使用JavaScriptInterface从WebView启动代码,但它们都没有按预期工作,因为其中一个正在尝试打开 Play 商店,而第二个说:“应用程序没有'不存在”当我从应用程序创建快捷方式时。

这个正在启动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)
        {

        }
    }
}

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

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

谢谢你的支持。

PS:

标签: androidxamarin.androidandroid-webviewandroid-shortcutpinned-shortcut

解决方案


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

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

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

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

推荐阅读