安卓 10 唤醒

时间:2021-03-02 08:40:50

标签: android delphi firemonkey

这里有一个支持消息传递的应用程序。虽然我可以在后台下载消息,但如果屏幕关闭,则振动不起作用。

所以我尝试了这个:

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;

不幸的是,这不起作用,大多数情况下它只是锁定了我的应用程序。有时我会收到权限错误。在 Delphi 中,我将 WAKE_LOCK 的权限设置为 true。

即使屏幕关闭,我也想振动,这可能吗?

谢谢。

1 个答案:

答案 0 :(得分:1)

我是这样解决的:

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;