检查前台服务中的设备锁解锁事件

时间:2017-12-16 07:08:51

标签: android broadcastreceiver android-notifications foregroundnotification

我看到很多类似的问题和答案,但我无法找到适合我情况的正确答案

这是我的方法:
我有一个带有通知的前端服务,只要我想要,它会持续一段时间,现在我想检查设备锁定并解锁此前台服务中的事件

即: 当我点击开始时我希望设备在1秒后锁定自己,从那里开始,每当我解锁它时,它应该启动另一个特定服务,当我锁定它时,它应该停止该特定服务,直到我关闭整个前景服务

这是我的服务:

   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);
}

任何帮助将不胜感激

0 个答案:

没有答案
相关问题