خطأ PendingIntent: FLAG_IMMUTABLE أو FLAG_MUTABLE إلزامي لـ Android 12+
مرحباً بالجميع! أنا مبتدئ في تطوير Android باستخدام Kotlin، وواجهت مشكلة عند الانتقال إلى targetSdk 31 (Android 12). أستخدم MediaSessionCompat لإدارة مشغل الوسائط. عند إنشاء PendingIntent لزر "تشغيل/إيقاف مؤقت" في الإشعار (Notification)، يظهر الخطأ التالي:
Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
الكود الخاص بي يبدو هكذا:
```kotlin
val intent = Intent(this, MainActivity::class.java)
val pendingIntent = PendingIntent.getActivity(this, 0, intent, 0)
val playAction = NotificationCompat.Action(
R.drawable.ic_play,
"Play",
pendingIntent
)
```
أفهم أنه يجب إضافة علم (flag)، لكنني لست متأكداً أي واحد يجب اختياره وكيف سيؤثر ذلك على عمل الإشعارات. كما سمعت أن هناك مشكلة أيضاً مع BroadcastReceiver الخاص بـ MediaButtonReceiver. أرجو توضيح كيفية إصلاح هذا الخطأ بشكل صحيح لجميع PendingIntent المرتبطة بـ MediaSession؟ شكراً مسبقاً.