我看到很多类似的问题和答案,但我找不到适合我情况的正确答案
这是我的方法:
我有一个带有通知的前台服务,只要我愿意,它就会持续多久,现在我想在这个前台服务中检查设备锁定和解锁事件
即:当我点击开始时,我希望设备在 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);
}
任何帮助将不胜感激