当我从 Xamarin.Android 中的静态快捷方式启动应用程序时未安装应用程序

问题描述

我最近为我的应用添加了一些快捷方式。应用运行良好,静态快捷方式按预期显示

preview 1

但是,当我点击其中任何一个时,我收到以下错误,导致应用无法启动:

未安装应用程序

这些是我的 XML:

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="tk.supernova.tmtimer.tk.supernova.tmtimer" android:versionName="2.1.0.0" android:installLocation="auto" android:versionCode="320">
    <uses-permission android:name="android.permission.READ_OWNER_DATA" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_OWNER_DATA" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="com.android.vending.BILLING" />
    <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />

    <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="30" />
    <application android:theme="@style/Launcher" android:icon="@drawable/icon" android:label="@string/app_name" android:requestLegacyExternalStorage="true">
        <activity android:name="tk.supernova.tmtimer.MainActivity" android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <Meta-data android:name="android.app.shortcuts" android:resource="@xml/shortcuts" />
        </activity>
    </application>
</manifest>

shortcuts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<shortcuts
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:targetApi="25">
    <shortcut
        android:shortcutId="time1"
        android:enabled="true"
        android:icon="@drawable/timeline_clock_outline"
        android:shortcutShortLabel="@string/Time1"
        android:shortcutLongLabel="@string/Time1">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetPackage="tk.supernova.tmtimer.tk.supernova.tmtimer"
            android:targetClass="tk.supernova.tmtimer.MainActivity">
            <extra
                android:name="customTime"
                android:value="30;45;60" />
        </intent>
        <categories android:name="android.shortcut.conversation" />
    </shortcut>
    <shortcut
        android:shortcutId="time2"
        android:enabled="true"
        android:icon="@drawable/timeline_clock_outline"
        android:shortcutShortLabel="@string/Time2"
        android:shortcutLongLabel="@string/Time2">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetPackage="tk.supernova.tmtimer.tk.supernova.tmtimer"
            android:targetClass="tk.supernova.tmtimer.MainActivity">
            <extra
                android:name="customTime"
                android:value="300;360;420" />            
        </intent>
        <categories android:name="android.shortcut.conversation" />
    </shortcut>
</shortcuts>

这是 MainActivity 类的介绍:

namespace tk.supernova.tmtimer
{
    [Activity(Label = "@string/app_name",MainLauncher = true,WindowSoftInputMode = SoftInput.AdjustPan)]
    public partial class MainActivity : AppCompatActivity
    {    
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            Platform.Init(this,savedInstanceState);

            var context = Platform.AppContext;
            var activity = Platform.CurrentActivity;
    
            SetTheme(Resource.Style.MyBaseTheme_NoActionBar);
            SetContentView(Resource.Layout.Main);

我知道我的名称 (tk.supernova.tmtimer.tk.supernova.tmtimer) 与主命名空间 (tk.supernova.tmtimer.tk.supernova.tmtimer) 不同。我也试过 使用 {applicationId},但没有用。我还在 Stack 中检查了几个想法,但没有一个在我的案例中起作用。此外,我无法轻易更改“错误的包名称”,因为该应用已在 Play Store 中发布了大约 3 年。

另外,我确定包名是正确的:

package name

此外,Android.App.Application.Context.PackageName 返回 tk.supernova.tmtimer.tk.supernova.tmtimer。预期的结果。

知道我做错了什么吗?

附注:

  • 我正在运行 Android 11 的模拟器中进行测试。
  • 我什至在手机上进行了身体测试,但没有奏效。

解决方法

我从 Microsoft Forum 那里得到了解决方案。我刚刚将这个属性:Name = tk.supernova.tmtimer.MainActivity" 添加到我的 MainActivity 中,如下所示:

[Activity(Label = "@string/app_name",Name = "tk.supernova.tmtimer.MainActivity",MainLauncher = true,WindowSoftInputMode = SoftInput.AdjustPan)]
public partial class MainActivity : AppCompatActivity
{
    //...
}

我个人认为这是一个 Xamarin.Android 错误。我认为在常规的 Android 应用程序中不会发生同样的情况,但如果有人进行测试,请随时将其添加到评论部分,我将更新此答案。