Issue with “BiometricPrompt” parameters as it expects Fragment/FragmentActivity as the first paramter. I am using it in the ForegroundService

So basically I am trying to develop an App Lock application where the user can select which app they want to lock using Biometric stored in the device. I have already fetched the list of apps and store the package name in the shared preference once it is locked.

Now I want to create a foreground service to check when an app is launch is it present in my List of locked apps in the Shared Preference. For this I have created the Foreground service, but the problem is the BiometricPrompt only accepts Fragment/FragmentActivity as the 1st Parameter.

This is my ForegroundAppLockService.kt

class ForegroundAppLockService : Service() {

    private lateinit var biometricPrompt: BiometricPrompt

    private val notificationId = 12345
    private val channelId = "app_lock_channel"
    private val channelName = "App Lock Service"

    override fun onCreate() {
        super.onCreate()
        createNotificationChannel()
        biometricPrompt = createBiometricPrompt()
        startAppLaunchAccessibilityService()
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        startForeground(notificationId, createNotification())
        return START_STICKY
    }

    override fun onBind(intent: Intent?): IBinder? {
        return null
    }

    private fun createBiometricPrompt(): BiometricPrompt {
        val executor = ContextCompat.getMainExecutor(applicationContext)
        val callback = object : BiometricPrompt.AuthenticationCallback() {
            override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
                super.onAuthenticationSucceeded(result)
                Toast.makeText(applicationContext, "App unlocked!", Toast.LENGTH_SHORT).show()
            }
        }

        return BiometricPrompt(this, executor, callback)
    }

    private fun createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel(
                channelId,
                channelName,
                NotificationManager.IMPORTANCE_DEFAULT
            )
            val notificationManager = getSystemService(NotificationManager::class.java)
            notificationManager.createNotificationChannel(channel)
        }
    }

    private fun createNotification(): Notification {
        val pendingIntent: PendingIntent =
            PendingIntent.getActivity(this, 0, Intent(), PendingIntent.FLAG_UPDATE_CURRENT)

        return NotificationCompat.Builder(this, channelId)
            .setContentTitle("App Lock Service")
            .setContentText("Monitoring app launches")
            .setSmallIcon(R.drawable.icon)
            .setContentIntent(pendingIntent)
            .build()
    }

    private fun startAppLaunchAccessibilityService() {
        val intent = Intent(this, AppLaunchAccessibilityService::class.java)
        startService(intent)
    }

    companion object {
        fun startService(context: Context) {
            val intent = Intent(context, ForegroundAppLockService::class.java)
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                context.startForegroundService(intent)
            } else {
                context.startService(intent)
            }
        }

        fun startBiometricAuthentication(context: Context) {
            val serviceIntent = Intent(context, ForegroundAppLockService::class.java)
            ContextCompat.startForegroundService(context, serviceIntent)
        }

        fun stopService(context: Context) {
            val intent = Intent(context, ForegroundAppLockService::class.java)
            context.stopService(intent)
        }
    }
}

And this is the AppLaunchAccessibilityService.

class AppLaunchAccessibilityService : AccessibilityService() {

    private lateinit var sharedPreferences: SharedPreferences

    override fun onCreate() {
        super.onCreate()
        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
    }

    override fun onAccessibilityEvent(event: AccessibilityEvent?) {
        if (event?.eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
            val packageName = event.packageName?.toString()
            packageName?.let {
                if (isAppLocked(packageName)) {
                    showBiometricPrompt()
                }
            }
        }
    }

    override fun onInterrupt() {

    }

    private fun isAppLocked(packageName: String): Boolean {
        val lockedPackages = getLockedPackages()
        return lockedPackages.contains(packageName)
    }

    private fun getLockedPackages(): Set<String> {
        val lockedPackagesKey = "key_locked_packages"
        return sharedPreferences.getStringSet(lockedPackagesKey, HashSet()) ?: HashSet()
    }

    private fun showBiometricPrompt() {
        ForegroundAppLockService.startBiometricAuthentication(this)
    }
}

I want the Biometric prompt to be shown whenever user clicks on any app that is present in my lockedApps.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật