身份證讀卡器安卓SDK在安卓12版本targetSdkVersion=32報錯解決
之前的身份證讀卡器安卓SDK版本V1.0.40在安卓12版本,targetSdkVersion=32的時候會出現以下錯誤:
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、將項目的targetSdkVersion由31改為30
2、如果不想改targetSdkVersion,那就在在創建PendingIntent的時候判斷當前系統版本,根據不同系統版本創建帶有不同flag的PendingIntent,具體代碼實現如下:
按如下方式修改DonseeDervice.arr代碼,更新至版本V1.0.41即可在安卓12版本使用。
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
mPendingIntent = PendingIntent.getBroadcast(mContext, 0, new Intent(ACTION_USB_PERMISSION), PendingIntent.FLAG_IMMUTABLE);
}else {
mPendingIntent = PendingIntent.getBroadcast(mContext, 0, new Intent(ACTION_USB_PERMISSION), 0);
}