Avatar ·

خطأ startForeground بعد التحديث إلى Android 8.1: كيف أصلحه؟

مرحباً بالجميع! أطور تطبيقاً بلغة Kotlin يستخدم خدمة خلفية لتتبع الموقع. بعد تحديث الجهاز إلى Android 8.1 (Oreo)، توقفت خدمتي عن العمل وتتعطل مع الخطأ `Context.startForegroundService() did not then call Service.startForeground()`. إليك كود الخدمة الخاص بي: ```kotlin class LocationService : Service() { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { val notification = NotificationCompat.Builder(this, "channel_id") .setContentTitle("الخدمة نشطة") .setContentText("تتبع الموقع") .setSmallIcon(R.drawable.ic_location) .build() startForeground(1, notification) return START_STICKY } override fun onBind(intent: Intent?): IBinder? = null } ``` وأقوم بتشغيلها هكذا: ```kotlin val intent = Intent(this, LocationService::class.java) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {