title: React Native: Detect if a mobile device is in use without user interaction and while app is in background
body: <ul>
  <li><strong>Goal</strong>: I want to find out if a device that has my React Native app installed is in use or has been in use looking a certain timeframe in the past. If an allowed inactivity timeframe is exceeded, I want to take an action.</li>
  <li><strong>Constraint</strong>: This detection of user activity should happen without the user having to interact with the app itself. Sleep detection apps already do this by using sensors apparently but accessing sensors while in background doesn't work for me in React Native (see table below).</li>
  <li><strong>Related question</strong>: Is there a working method in purely native (Java/Kotlin, Swift/Objective-C) if I would not use React Native?</li>
  </ul>
  <p>Following the methods I evaluated but was not able to achieve a working and reliable result with.</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>Method</th>
  <th>Problem</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>AppState</td>
  <td>would require the user to actively use the app</td>
  </tr>
  <tr>
  <td>battery drain</td>
  <td>unreliable because of varying battery life between devices</td>
  </tr>
  <tr>
  <td>screen lock</td>
  <td>did not find a way to get this information</td>
  </tr>
  <tr>
  <td>screen on time</td>
  <td>did not find a way to get this information</td>
  </tr>
  <tr>
  <td>motion (magnetometer)</td>
  <td>did not find a way to read sensors while the app is in background using <code>expo-sensors</code>, <code>react-native-sensors</code> with <code>react-native-background-fetch</code></td>
  </tr>
  <tr>
  <td>(geo)location</td>
  <td>would not detect use while the device is still</td>
  </tr>
  <tr>
  <td>app open event</td>
  <td>the assumption is to get different results based on the app/device state but <code>Linking.openURL()</code> for a supported URL resolves in every state</td>
  </tr>
  <tr>
  <td>permission request</td>
  <td>the assumption is to get different results based on the app/device state but <code>PermissionAndroid.request()</code> doesn't execute in background using <code>react-native-background-fetch</code></td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>Another thing to have in mind: Background tasks can only be executed every ~15 minutes. Indeed, there exist foreground services but they would be the second choice.</p>