Search code examples
google-chromegoogle-chrome-appidle-timerchrome-app-developer-tool

Use the chrome.idle API to restart application


I have a Chrome kiosk app that basically just uses webview to allow someone to browse through a catalog.

I found the chrome.idle API, and believe I understand how to set idle time and query if the device is idle, but can I have it restart the application when the state changes to idle or at least navigate back to a set URL?

The end goal is to have the catalog reset itself for the next user after being left idle for a set period of time.

https://developer.chrome.com/apps/idle


Solution

  • Well, the documentation is pretty clear..

    First, you need to declare in the manifest that you want to use this API, as it needs a permission.

    "permissions" : ["idle"],
    

    You could go with a poll-based approach as you suggested, but why? There's an event provided. So, we go on to use that.

    You need to inform Chrome how long an interval without user input you consider an idle state.

    chrome.idle.setDetectionInterval(120); // 120 seconds
    

    Lastly, you need to react to a change to an idle state.

    chrome.idle.onStateChanged.addListener(function(newState) {
      if(newState == "idle") {
        // Reset the state as you wish
      }
    });