Android 应用程序在定位到 Android 12 时崩溃

Zub*_*hah 1 javascript android android-pendingintent android-studio android-12

我的应用程序在定位到 Android 12 时崩溃了

这是崩溃日志:

java.lang.RuntimeException: Unable to create service com.kdr.snipping.CheckRecentRun: java.lang.IllegalArgumentException: com.kdr.snipping: 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.
        at android.app.ActivityThread.handleCreateService(ActivityThread.java:4500)
        at android.app.ActivityThread.access$1700(ActivityThread.java:247)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2072)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loopOnce(Looper.java:201)
        at android.os.Looper.loop(Looper.java:288)
        at android.app.ActivityThread.main(ActivityThread.java:7839)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
     Caused by: java.lang.IllegalArgumentException: com.kdr.snipping: 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.
        at android.app.PendingIntent.checkFlags(PendingIntent.java:375)
        at android.app.PendingIntent.buildServicePendingIntent(PendingIntent.java:724)
        at android.app.PendingIntent.getService(PendingIntent.java:686)
        at com.kdr.snipping.CheckRecentRun.setAlarm(CheckRecentRun.java:44)
        at com.kdr.snipping.CheckRecentRun.onCreate(CheckRecentRun.java:35)
        at android.app.ActivityThread.handleCreateService(ActivityThread.java:4487)
            ... 9 more
Run Code Online (Sandbox Code Playgroud)

我在 Gradle 中使用以下实现

implementation 'androidx.appcompat:appcompat:1.4.1'
    implementation 'com.google.android.material:material:1.5.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
    implementation 'com.google.android.gms:play-services-ads-lite:20.6.0'
    implementation 'com.google.android.gms:play-services-ads:20.6.0'
    implementation 'androidx.work:work-runtime:2.7.1'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
Run Code Online (Sandbox Code Playgroud)

希望有人可以帮助我,谢谢

小智 5

随着 Android 12 的推出,您现在必须设置标志 PendingIntent.FLAG_IMMUTABLE 或 PendingIntent.FLAG_MUTABLE。

PendingIntent.getActivity(context, requestCode, intent, PendingIntent.FLAG_IMMUTABLE)
Run Code Online (Sandbox Code Playgroud)

或者

PendingIntent.getActivity(context, 1337, openAppIntent, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_CANCEL_CURRENT)
Run Code Online (Sandbox Code Playgroud)

在这里查看更多内容