Stay organized with collections
Save and categorize content based on your preferences.
TriggerEventListener
abstract class TriggerEventListener
This class is the listener used to handle Trigger Sensors. Trigger Sensors are sensors that trigger an event and are automatically disabled. Sensor.TYPE_SIGNIFICANT_MOTION
is one such example.
SensorManager
lets you access the device's sensors
. Get an instance of SensorManager
by calling Context.getSystemService()
with the argument android.content.Context#SENSOR_SERVICE
.
Here's an example setup for a TriggerEventListener:
class TriggerListener extends TriggerEventListener {
public void onTrigger(TriggerEvent event) {
// Do Work.
// As it is a one shot sensor, it will be canceled automatically.
// SensorManager.requestTriggerSensor(this, mSigMotion); needs to
// be called again, if needed.
}
}
public class SensorActivity extends Activity {
private final SensorManager mSensorManager;
private final Sensor mSigMotion;
private final TriggerEventListener mListener = new TriggerEventListener();
public SensorActivity() {
mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
mSigMotion = mSensorManager.getDefaultSensor(Sensor.TYPE_SIGNIFICANT_MOTION);
}
protected void onResume() {
super.onResume();
mSensorManager.requestTriggerSensor(mListener, mSigMotion);
}
protected void onPause() {
super.onPause();
// Call disable to ensure that the trigger request has been canceled.
mSensorManager.cancelTriggerSensor(mListener, mSigMotion);
}
}
Summary
Public methods |
abstract Unit |
The method that will be called when the sensor is triggered.
|
Public constructors
TriggerEventListener
TriggerEventListener()
Public methods
onTrigger
abstract fun onTrigger(event: TriggerEvent!): Unit
The method that will be called when the sensor is triggered. Override this method in your implementation of this class.
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-02-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-02-10 UTC."],[],[],null,["# TriggerEventListener\n\nAdded in [API level 18](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)\n\nTriggerEventListener\n====================\n\n*** ** * ** ***\n\nKotlin \\|[Java](/reference/android/hardware/TriggerEventListener \"View this page in Java\") \n\n```\nabstract class TriggerEventListener\n```\n\n|---|--------------------------------------------|\n| [kotlin.Any](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html) ||\n| ↳ | [android.hardware.TriggerEventListener](#) |\n\nThis class is the listener used to handle Trigger Sensors. Trigger Sensors are sensors that trigger an event and are automatically disabled. [Sensor.TYPE_SIGNIFICANT_MOTION](/reference/kotlin/android/hardware/Sensor#TYPE_SIGNIFICANT_MOTION:kotlin.Int) is one such example.\n\n[SensorManager](/reference/kotlin/android/hardware/SensorManager) lets you access the device's [sensors](/reference/kotlin/android/hardware/Sensor). Get an instance of [SensorManager](/reference/kotlin/android/hardware/SensorManager) by calling [Context.getSystemService()](../content/Context.html#getSystemService(kotlin.String)) with the argument [android.content.Context#SENSOR_SERVICE](../content/Context.html#SENSOR_SERVICE:kotlin.String).\n\nHere's an example setup for a TriggerEventListener: \n\n```kotlin\nclass TriggerListener extends TriggerEventListener {\n public void onTrigger(TriggerEvent event) {\n // Do Work.\n \n // As it is a one shot sensor, it will be canceled automatically.\n // SensorManager.requestTriggerSensor(this, mSigMotion); needs to\n // be called again, if needed.\n }\n }\n public class SensorActivity extends Activity {\n private final SensorManager mSensorManager;\n private final Sensor mSigMotion;\n private final TriggerEventListener mListener = new TriggerEventListener();\n \n public SensorActivity() {\n mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);\n mSigMotion = mSensorManager.getDefaultSensor(Sensor.TYPE_SIGNIFICANT_MOTION);\n }\n \n protected void onResume() {\n super.onResume();\n mSensorManager.requestTriggerSensor(mListener, mSigMotion);\n }\n \n protected void onPause() {\n super.onPause();\n // Call disable to ensure that the trigger request has been canceled.\n mSensorManager.cancelTriggerSensor(mListener, mSigMotion);\n }\n \n }\n \n```\n\nSummary\n-------\n\n| Public constructors ||\n|------------------------------------------------------------|---|\n| [TriggerEventListener](#TriggerEventListener())`()` \u003cbr /\u003e |\n\n| Public methods ||\n|---------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| abstract [Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) | [onTrigger](#onTrigger(android.hardware.TriggerEvent))`(`event:` `[TriggerEvent](/reference/kotlin/android/hardware/TriggerEvent)!`)` The method that will be called when the sensor is triggered. |\n\nPublic constructors\n-------------------\n\n### TriggerEventListener\n\n```\nTriggerEventListener()\n```\n\nPublic methods\n--------------\n\n### onTrigger\n\nAdded in [API level 18](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nabstract fun onTrigger(event: TriggerEvent!): Unit\n```\n\nThe method that will be called when the sensor is triggered. Override this method in your implementation of this class.\n\n| Parameters ||\n|---------|---------------------------------------------------------------------------------------------|\n| `event` | [TriggerEvent](/reference/kotlin/android/hardware/TriggerEvent)!: The details of the event. |"]]