Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified

最近在写代码时碰到如下错误:

java.lang.IllegalArgumentException: com.example.imdemo: 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.

出错的代码主要是在创建通知栏消息时抛出的,主要代码如下:

private void createNotification() {
    Intent intent = new Intent(this, OtherActivity.class);
    // 这一行报错
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 123, intent, PendingIntent.FLAG_ONE_SHOT);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("测试")
            .setContentText("收到一条通知消息")
            .setContentIntent(pendingIntent)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setAutoCancel(true);
    Notification build = builder.build();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.createNotificationChannel(
                new NotificationChannel(NOTIFICATION_CHANNEL_ID, "测试测试", NotificationManager.IMPORTANCE_HIGH)
        );
    }
    NotificationManagerCompat.from(this).notify(notificationId++, build);
}

查了一番资料后,发现如下几个修复的方法:

  1. 将项目的targetSdkVersion由31改为30

  2. 如果不想改targetSdkVersion,那就在在创建PendingIntent的时候判断当前系统版本,根据不同系统版本创建带有不同flag的PendingIntent,具体代码实现如下:

    PendingIntent pendingIntent;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
        pendingIntent = PendingIntent.getActivity(this, 123, intent, PendingIntent.FLAG_IMMUTABLE);
    } else {
        pendingIntent = PendingIntent.getActivity(this, 123, intent, PendingIntent.FLAG_ONE_SHOT);
    }
    
  3. 直接在项目中依赖下面的库

    dependencies {
      // For Java
      implementation 'androidx.work:work-runtime:2.7.1' 
     
      // For Kotlin
      implementation 'androidx.work:work-runtime-ktx:2.7.1'
    }
    

这里最推荐的是使用上面的方法2。

  • 44
    点赞
  • 54
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 11
    评论
Introduction At first, .NET was no more than a distant echo in my consciousness. For a long time—it seems like years, and actually, it was years—I heard the distant beating of publicity drums regarding .NET, and I couldn't care less. My concern has always been to work with high- quality programming tools that are solid, rather than playing with the latest technology toy, and I am ever suspicious of publicity campaigns. Visual Basic 6 worked well enough for me, my consulting clients, and the readers of my books. Why should I bother taking the time to understand this .NET thing until it was tried-and-true technology? What's the .NET Strategy? Before we get to my "Aha!" moment—and I've had several epiphanies related to .NET and Visual Basic .NET—I would like to step back for a second and consider exactly what .NET is and what it is not. This is particularly confusing because some things that Microsoft calls .NET are a matter of "vision," some are marketing terms, and others—such as Visual Basic .NET—are grounded technologies that are here today. (I mean, what could be more grounded as a technology than a programming language?) Pushing aside the clouds of confusion that swirl around the terminology, Microsoft's .NET strategy involves three related offerings: • .NET Framework and Visual Studio .NET, a runtime platform and development environment that works with languages including Visual Basic .NET • .NET Enterprise Servers, a set of enterprise server products such as Biztalk Server, Exchange Server, Mobile Information Server, and SQL Server, which happen to have been given the .NET moniker for marketing purposes • .NET MyServices, also sometimes called Hailstorm, which is a vision for creating services, such as the Passport user authentication service, that can be drawn upon as utilities by myriad distributed applications The domain of this book is the first of these: applications written in the Visual Basic .NET language, using the Visual Studio development environment, targeting the .NET Framework as a runtime platform. You can think of the runtime platform that .NET applications are written for as being analogous to the Java runtime "sandbox" that Java applications target, except that, at this point, the .NET Framework is primarily deployed on Microsoft Windows platforms.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yubo_725

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值