check for device lock unlock events in foreground service

406 views Asked by At

I saw many many similar questions and answers, but I couldn't find a right answer for my situation

here's my approach:
I have a foregroundservice with notifications which will be last as long as i want ,now i want to check device lock and unlock events in this foreground service

i.e.: when I hit start I want the device to be lock itself after 1 second, from there on, whenever I unlock it, it should start another specific service and again when I lock it, it should stop that specific service until I shutdown the whole foreground service

here's my service :

   if (Objects.equals(intent.getAction(), IZIGaapsConstants.ACTION.STARTFOREGROUND_ACTION)) {

        Intent deviceLockCheck = new Intent(this, IZIGaapsForegroundNotification.class);
        deviceLockCheck .setAction(IZIGaapsConst.LOCK_START);
        PendingIntent preStartIntent = PendingIntent.getService(this, 0, deviceLockCheck , 0);
        NotificationCompat.Action startIt = new NotificationCompat.Action(R.drawable.ic_start_24dp,
                getString(R.string.screen_notification_action_start), precordStartIntent);
        startNotificationForeGround(createRecordingNotification(startIt).build(), IZIGaapsConst.DEVICE_LOCK_NOTIFICATION_ID);

    }else if (intent.getAction().equals(IZIGaapsConstants.ACTION.STOPFOREGROUND_ACTION)){

        stopForeground(true);
        stopSelf();

    }



    if (Objects.equals(intent.getAction(), IZIGaapsConst.LOCK_START)) {

        //i want the lock event to be start from here to check for devecie lock and unlock

    } else if (Objects.equals(intent.getAction(), IZIGaapsConst.LOCK_STOP)) {

        //you know the task :)
    }

    return START_STICKY;
}

private NotificationCompat.Builder createRecordingNotification(NotificationCompat.Action action) {

    Bitmap icon = BitmapFactory.decodeResource(getResources(),
            R.drawable.rec_icon);
    NotificationCompat.Builder notification = new NotificationCompat.Builder(this, IZIGaapsConst.RECORDING_NOTIFICATION_CHANNEL_ID)
            .setContentTitle(getResources().getString(R.string.screen_recording_notification_title))
            .setTicker(getResources().getString(R.string.screen_recording_notification_title))
            .setSmallIcon(R.drawable.rec_icon)
            .setLargeIcon(Bitmap.createScaledBitmap(icon, 128, 128, false))
            .setPriority(Notification.PRIORITY_MIN);
    if (action != null)
        notification.addAction(action);
    return notification;
}

private void startNotificationForeGround(Notification notification, int ID) {
    startForeground(ID, notification);
}

any help will be greatly appreciated

0

There are 0 answers