文章目录
- 问题1、 Could not find 3 Android X assemblies, make sure to install the following NuGet packages
- 解决办法1:
- 解决办法2:
- 问题2、Java.Lang.IllegalArgumentException: 'com.lhd. iml6yu: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles.'
- 解决办法1:
- 解决办法2:
- 解决办法3:
- 安卓各个版本命名以及对应API关系
- 同系列文章推荐
问题1、 Could not find 3 Android X assemblies, make sure to install the following NuGet packages
在做通知(Notification)的时候,按照微软官方文档,是需要引用 Android.Support.V4
Nuget包,
但是在引用了这个包以后会出现如下错误
严重性 代码 说明 项目 文件 行 禁止显示状态
错误 Could not find 3 Android X assemblies, make sure to install the following NuGet packages:
- Xamarin.AndroidX.Lifecycle.LiveData
- Xamarin.AndroidX.Legacy.Support.V4
You can also copy-and-paste the following snippet into your .csproj file:
<PackageReference Include="Xamarin.AndroidX.Lifecycle.LiveData" Version="2.2.0.3" />
<PackageReference Include="Xamarin.AndroidX.Legacy.Support.V4" Version="1.0.0.5" />
解决办法1:
按照错误提示引用如下两个nuget包
执行
NuGet\Install-Package Xamarin.AndroidX.Lifecycle.LiveData -Version 2.2.0.3
NuGet\Install-Package Xamarin.AndroidX.Legacy.Support.V4 -Version 1.0.0.5
或者
包引用
<PackageReference Include="Xamarin.AndroidX.Lifecycle.LiveData" Version="2.2.0.3" />
<PackageReference Include="Xamarin.AndroidX.Legacy.Support.V4" Version="1.0.0.5" />
解决办法2:
原本就可以不引用 Android.Support.V4
,只需要在程序中using AndroidX.Core.App;
问题2、Java.Lang.IllegalArgumentException: ‘com.lhd. iml6yu: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles.’
主要是在做通知的时候下面这段代码出现的问题
var stopServicePendingIntent = PendingIntent.GetActivity(this, 0, stopServiceIntent, PendingIntentFlags.UpdateCurrent);
一个完整的方法代码
/// <summary>
/// Builds a PendingIntent that will display the main activity of the app. This is used when the
/// user taps on the notification; it will take them to the main activity of the app.
/// </summary>
/// <returns>The content intent.</returns>
PendingIntent BuildIntentToShowMainActivity()
{
var notificationIntent = new Intent(this, typeof(MainActivity));
notificationIntent.SetAction(Constants.ACTION_MAIN_ACTIVITY);
notificationIntent.SetFlags(ActivityFlags.SingleTop | ActivityFlags.ClearTask);
notificationIntent.PutExtra(Constants.SERVICE_STARTED_KEY, true);
//就是这里有问题
var pendingIntent = PendingIntent.GetActivity(this, 0, notificationIntent, PendingIntentFlags.UpdateCurrent);
return pendingIntent;
}
解决办法1:
如果不考虑版本问题,固定在API31+以上使用,可以修改代码如下
注意最后一个枚举类型
//注意最后一个枚举类型
var stopServicePendingIntent = PendingIntent.GetActivity(this, 0, stopServiceIntent, PendingIntentFlags.Immutable);
解决办法2:
将项目的targetSdkVersion由31改为30
解决办法3:
根据不同系统版本创建带有不同flag的PendingIntent,具体代码实现如下:
PendingIntent pendingIntent;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
pendingIntent = PendingIntent.GetActivity(this, 0, stopServiceIntent, PendingIntentFlags.Immutable);
} else {
pendingIntent = PendingIntent.GetActivity(this, 0, notificationIntent, PendingIntentFlags.UpdateCurrent);
}
安卓各个版本命名以及对应API关系
同系列文章推荐
.net开发安卓入门 - 环境安装
.net开发安卓入门 - Hello world!
.net开发安卓入门 - 基本交互(Button,输入EditText,TextView,Toast)
.net开发安卓入门 - 布局与样式
.net开发安卓入门 - Activity
.net开发安卓入门 - Notification(通知)