Android 为什么我的代码没有在安卓8+;(奥利奥+;)?

Android 为什么我的代码没有在安卓8+;(奥利奥+;)?,android,xamarin.android,android-webview,android-shortcut,pinned-shortcut,Android,Xamarin.android,Android Webview,Android Shortcut,Pinned Shortcut,我正在尝试锁定我的应用程序的一些动态快捷方式,它们将在用户创建自定义时间时创建 我有两个版本的代码,我正在使用JavaScriptInterface从WebView启动代码,但是没有一个能像预期的那样工作,因为其中一个正在尝试打开Play Store,第二个在我从应用程序创建快捷方式时说:“应用程序不存在” 这一个正在启动播放商店: [Export] [JavascriptInterface] public void PinCustomTime(string time) { var ma

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

我有两个版本的代码,我正在使用JavaScriptInterfaceWebView启动代码,但是没有一个能像预期的那样工作,因为其中一个正在尝试打开Play Store,第二个在我从应用程序创建快捷方式时说:“应用程序不存在”

这一个正在启动播放商店:

[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.:

  • 我所有的测试都是在Android Pie下完成的
  • 我已经在C#中用Xamarin.Android构建了代码,但是如果您有 在Kotlin或Java中,我可以迁移它

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

new Intent(Intent.ActionView).SetData(Android.Net.Uri.Parse(context.PackageName))
要启动特定活动,请将其替换为(在java中):

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);
    }
}
我得到了以下方面的支持:


感谢您的评论,您只能在Android 8.0(API级别26)和laterHi@LeoZhu MSFT上创建固定的快捷方式。最初的问题是关于Android Oreo+,我更新了它。你能测试一下我的代码,让我知道为什么它在Android Pie中不起作用吗?我用安卓奥利奥和派做了测试。谢谢。好的,我明天会测试它并告诉你resultHi@LeoZhu MSFT,多亏了atarasenko的答案,我能够修复它,我用C#发布了我的答案。我之前试过你的第二个代码,它试图打开谷歌地图、Chrome等X应用程序,而我的应用程序不在列表中(我认为这甚至不应该发生)。有什么想法吗?此外,您能否提供一个功能齐全的示例,说明您的任何应用程序都有固定的快捷方式?谢谢。奇怪的是,它对我有效(在Emulator API 28上)。当intent指定组件名称时,它将只启动指定的活动(本例中为MainActivity),而不是Google Maps等。可能在我的情况下,唯一的变体是我从webview启动它,但我不认为它会影响你,或者你是怎么想的?我添加了代码的这一部分,以避免将来的误解。我在你的代码的基础上做了一个小的修改,它成功了。谢谢
[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);
    }
}