Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Targeting S+ (version 31 and above) #102

Open
Hesam-Taleghani opened this issue Aug 10, 2022 · 3 comments
Open

Targeting S+ (version 31 and above) #102

Hesam-Taleghani opened this issue Aug 10, 2022 · 3 comments

Comments

@Hesam-Taleghani
Copy link

Hi! Thanks for t his great project
I work with nfca tags and my app works fine with android 10 but when I want to run it on android 12, It crashes
Caused by: java.lang.IllegalArgumentException: com..: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
E/AndroidRuntime(21272): 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.
E/AndroidRuntime(21272): at android.app.PendingIntent.checkFlags(PendingIntent.java:382)
E/AndroidRuntime(21272): at android.app.PendingIntent.getActivityAsUser(PendingIntent.java:465)
E/AndroidRuntime(21272): at android.app.PendingIntent.getActivity(PendingIntent.java:451)
E/AndroidRuntime(21272): at android.app.PendingIntent.getActivity(PendingIntent.java:415)
E/AndroidRuntime(21272): at com.example.coollog.MainActivity.onResume(MainActivity.kt:13)
E/AndroidRuntime(21272): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1476)
E/AndroidRuntime(21272): at android.app.Activity.performResume(Activity.java:8441)
E/AndroidRuntime(21272): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:5200)

@xdamien7
Copy link

Hello,

It took me a while but this sold the problem for me

1.modify your MainActivity.kt file (search in your project) in the app/src/main/kotlin/com folder tree

`
package com.yourpackage.name

import android.app.PendingIntent
import android.content.Intent
import android.nfc.NfcAdapter
import io.flutter.embedding.android.FlutterActivity

/// Workaround for disabling the default-tag-detection while the app is foreground.
class MainActivity: FlutterActivity() {
override fun onResume() {
super.onResume()

val intent = Intent(context, javaClass).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
val pendingIntent = PendingIntent.getActivity(
context,
0,
intent,
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT // setting the mutability flag 
)

NfcAdapter.getDefaultAdapter(context)?.enableForegroundDispatch(this, pendingIntent, null, null)

}

override fun onPause() {
super.onPause()
NfcAdapter.getDefaultAdapter(context)?.disableForegroundDispatch(this)
}
}

  1. modify your app/build.gradle file

in the bottom of the file add below dependencies.

below is my copy

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

// (Java only)
implementation("androidx.work:work-runtime:2.7.1")

// Kotlin + coroutines
implementation("androidx.work:work-runtime-ktx:2.7.1")

}

Hope this helps (can't guarantee this will work for you, but it may guide you)

@Grrravity
Copy link

Grrravity commented Aug 16, 2023

Bump this :P

Trying @xdamien7 's proposal but would be nice to have this fixed

Edited :
Workaround worked 👍🏼

@ManXliu
Copy link

ManXliu commented Oct 20, 2023

有用

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants