Android 10 , WakeUP

Viewed 287

got here an App with Messaging support. And while I can download the Messages in the Background , the Vibration does not work if the Screen is off.

So I tried this :

procedure TMain.doVibration(milisec: Integer);
var Vibrator : JVibrator;
    PowerManager : JPowerManager;
begin
  PowerManager := TJPowerManager.Wrap(TAndroidHelper.Context.getSystemService(TJContext.JavaClass.POWER_SERVICE));

  if PowerManager.isInteractive = false then PowerManager.wakeUp(1000);

  Vibrator := TJVibrator.Wrap((SharedActivityContext.getSystemService(TJContext.JavaClass.VIBRATOR_SERVICE) as ILocalObject).GetObjectID);
  Vibrator.vibrate(milisec);
end;

Unfortunately this does not work, most of the time it simply locks up my app . Sometimes I get a permission error . In Delphi I set the permission for WAKE_LOCK to true.

I would just like to vibrate even if the Screen is Off , is this possible?

Thank you.

1 Answers

I solved it like this :

procedure TMain.MessageVibrate;
var
  Vibrator : JVibrator;
  PowerManager : JPowerManager;
  WakeLock : JPowerManager_WakeLock;
begin
    PowerManager := TJPowerManager.Wrap(TAndroidHelper.Context.getSystemService(TJContext.JavaClass.POWER_SERVICE));

    WakeLock := PowerManager.newWakeLock(TJPowerManager.JavaClass.FULL_WAKE_LOCK or TJPowerManager.JavaClass.ACQUIRE_CAUSES_WAKEUP,StringToJString('NE_PACK'));
    WakeLock.acquire;

    sleep(500); // if the device is in deep sleep, sometimes it does not vibrate if I do not wait

    Vibrator := TJVibrator.Wrap((SharedActivityContext.getSystemService(TJContext.JavaClass.VIBRATOR_SERVICE) as ILocalObject).GetObjectID);
    Vibrator.vibrate(500);

    WakeLock.release;
    WakeLock:=nil;
end;
Related