Suspend Shutdown hooks
The TomTom BRIDGE allows you to intervene when it is about to be suspended or shut down. It will
look for apps listening for the ACTION_REQUEST_CONFIRM_SUSPEND_SHUTDOWN intent and if one is
installed, control over the suspend/shutdown flow is handed over to this app. This can be useful for
situations where you do not want the device to suspend/shutdown because some process is not finished
or users first need to log out of your own systems, for example.
Make sure to use a priority higher than 5 for this intent-filter, to make sure your app will be called. The default handler uses priority 5 so to supersede it your priority must be higher. So your AndroidManifest.xml will contain this:
<!-- Permissions needed --><uses-permission android:name="android.permission.DEVICE_POWER" /><uses-permission android:name="android.permission.SHUTDOWN" />...<!-- Priority needed > 5 --><intent-filter android:priority="10" >s <action android:name="android.intent.action.ACTION_REQUEST_CONFIRM_SUSPEND_SHUTDOWN" /> <category android:name="android.intent.category.DEFAULT" /></intent-filter>The intent comes with two integer extras: poweroff_state and poweroff_reason. The former indicates the state the system is trying to get to, 1 for a shutdown and 2 to be suspended, and the reason gives an indication why the device is trying to get to that state:
| Value | Reason |
|---|---|
| 0 | By user request, eg. the power button was pressed |
| 1 | Device administration policy |
| 2 | Screen timeout |
| 3 | Disconnected from power |
| 4 | Lid switch (from 17.6 onward) |
| 5 | Application request (from 17.6 onward) |
| 6 | HDMI (from 17.6 onward) |
| 7 | Sleep button (from 17.6 onward) |
Once you have decided to proceed with the suspend/shutdown it’s the apps responsibility now to make that happen! These are the calls you can use:
private void suspend() { try { finish() final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); pm.goToSleep(SystemClock.uptimeMillis()); } catch (final SecurityException e) { Log.e(TAG, "SecurityException ", e); }}
private void shutdown() { try { finish(); final Intent shutdownIntent = new Intent("android.intent.action.ACTION_REQUEST_SHUTDOWN"); startActivity(shutdownIntent); } catch (final SecurityException e) { Log.e(TAG, "SecurityException ", e); }}To prevent the suspend/shutdown from happening, the only thing you need to do is ignore it: don’t suspend or request a shutdown from your app and nothing will happen.