2

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.

Ken White
  • 123,280
  • 14
  • 225
  • 444
user1937012
  • 1,031
  • 11
  • 20
  • You might want to look at: https://stackoverflow.com/questions/58790318/phone-vibration-stops-working-after-screen-is-off-locked – Dave Nottage Mar 02 '21 at 19:42
  • 1
    I've rolled back your edit. It is not appropriate here to edit the question to add the solution. If you've found that solution and want to share it with others, write an answer in the space below that is provided for that purpose. See [Can I answer my own question?](http://stackoverflow.com/help/self-answer) for more information. (You can copy the code and text from the rolled back question in the [history](https://stackoverflow.com/posts/66435965/revisions).) – Ken White Mar 02 '21 at 21:36

1 Answers1

2

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;
user1937012
  • 1,031
  • 11
  • 20