diff --git a/entry/src/main/java/com/github/tbouron/shakedetector/example/slice/MainAbilitySlice.java b/entry/src/main/java/com/github/tbouron/shakedetector/example/slice/MainAbilitySlice.java index cbae18212279d60b1a36310d2463baaa02606019..54bb7c25a84db0a13970c1d0f0d979112d1a73ff 100644 --- a/entry/src/main/java/com/github/tbouron/shakedetector/example/slice/MainAbilitySlice.java +++ b/entry/src/main/java/com/github/tbouron/shakedetector/example/slice/MainAbilitySlice.java @@ -65,7 +65,6 @@ public class MainAbilitySlice extends AbilitySlice { ShakeDetector.create(this, new ShakeDetector.OnShakeListener() { @Override public void OnShake() { - getAbility().getUITaskDispatcher().asyncDispatch(() -> { addStatusMessage("shake_detected"); new ToastDialog(getContext()) .setText("Device shaken!") @@ -73,7 +72,6 @@ public class MainAbilitySlice extends AbilitySlice { .setTransparent(true) .setAutoClosable(true) .show(); - }); } }); if (ShakeDetector.start()) { diff --git a/library/src/main/java/com/github/tbouron/shakedetector/library/ShakeDetector.java b/library/src/main/java/com/github/tbouron/shakedetector/library/ShakeDetector.java index 4eecdc3378d36b813665eb900d0d2d53b0ff6212..8d738044dcfa055aa336a61ca68ab80ebb42a350 100644 --- a/library/src/main/java/com/github/tbouron/shakedetector/library/ShakeDetector.java +++ b/library/src/main/java/com/github/tbouron/shakedetector/library/ShakeDetector.java @@ -1,6 +1,9 @@ package com.github.tbouron.shakedetector.library; import ohos.app.Context; +import ohos.eventhandler.EventHandler; +import ohos.eventhandler.EventRunner; +import ohos.eventhandler.InnerEvent; import ohos.sensor.agent.CategoryOrientationAgent; import ohos.sensor.bean.CategoryOrientation; import ohos.sensor.data.CategoryOrientationData; @@ -58,6 +61,26 @@ public class ShakeDetector implements ICategoryOrientationDataCallback { private float mLastX; private float mLastY; private float mLastZ; + /** + * 消息 + */ + public static final int CODE_DOWNLOAD_FILE1 = 1; + /** + * handler + */ + private EventHandler myEvenHandler = new EventHandler(EventRunner.getMainEventRunner()){ + @Override + protected void processEvent(InnerEvent event) { + super.processEvent(event); + if (event == null) { + return; + } + int eventId = event.eventId; + if(eventId == CODE_DOWNLOAD_FILE1){ + mShakeListener.OnShake(); + } + } + }; /** * 摇动设备后要调用的回调的接口定义 @@ -200,7 +223,7 @@ public class ShakeDetector implements ICategoryOrientationDataCallback { } if (direction >= mThresholdShakeNumber) { direction = 0; - mShakeListener.OnShake(); + myEvenHandler.sendEvent(CODE_DOWNLOAD_FILE1); mSensorBundles.clear(); } }