在 Android 12 上提示电话号码时在 PendingIntent 中设置可变性标志

cha*_*ura 5 android android-pendingintent android-12

在 Android 12 中,需要指定 PendingIntent 的可变性。在签名屏幕中,使用 HintRequest 类,并 通过getHintPickerIntent方法返回PendingIntent。代码如下,

 HintRequest hintRequest = new HintRequest.Builder().setPhoneNumberIdentifierSupported(true).build();
 PendingIntent intent = Credentials.getClient(activity).getHintPickerIntent(hintRequest);//crashes here on android 12
Run Code Online (Sandbox Code Playgroud)

logcat中的错误是这样的,

java.lang.IllegalArgumentException: 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.
Run Code Online (Sandbox Code Playgroud)

我的问题是如何在创建待处理意图后设置可变性标志。

小智 4

该问题是因为身份验证库没有最新的 SDK 版本 (31) 要求。我遇到了类似的问题,在更新构建 Gradle 中的最新身份验证库后它得到了修复。

将您的auth库版本更新到20.2.0或更高版本

implementation "com.google.android.gms:play-services-auth:20.2.0"
Run Code Online (Sandbox Code Playgroud)