Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>public class MyApplication extends Application { private int lastInteractionTime; private Boolean isScreenOff = false; public void onCreate() { super.onCreate(); // ...... startUserInactivityDetectThread(); // start the thread to detect inactivity new ScreenReceiver(); // creating receive SCREEN_OFF and SCREEN_ON broadcast msgs from the device. } public void startUserInactivityDetectThread() { new Thread(new Runnable() { @Override public void run() { while(true) { Thread.sleep(15000); // checks every 15sec for inactivity if(isScreenOff || getLastInteractionTime()&gt; 120000 || !isInForeGrnd) { //...... means USER has been INACTIVE over a period of // and you do your stuff like log the user out } } } }).start(); } public long getLastInteractionTime() { return lastInteractionTime; } public void setLastInteractionTime(int lastInteractionTime) { this.lastInteractionTime = lastInteractionTime; } private class ScreenReceiver extends BroadcastReceiver { protected ScreenReceiver() { // register receiver that handles screen on and screen off logic IntentFilter filter = new IntentFilter(); filter.addAction(Intent.ACTION_SCREEN_ON); filter.addAction(Intent.ACTION_SCREEN_OFF); registerReceiver(this, filter); } @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { isScreenOff = true; } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) { isScreenOff = false; } } } } </code></pre> <p><strong>isInForeGrnd ===> logic is not shown here as it is out of scope of the question</strong></p> <p>You can wake the lock to the cpu by using the device code below-</p> <pre><code> if(isScreenOff || getLastInteractionTime()&gt; 120000 || !isInForeGrnd) { //...... means USER has been INACTIVE over a period of // and you do your stuff like log the user out PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); boolean isScreenOn = pm.isScreenOn(); Log.e("screen on.................................", "" + isScreenOn); if (isScreenOn == false) { PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE, "MyLock"); wl.acquire(10000); PowerManager.WakeLock wl_cpu = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyCpuLock"); wl_cpu.acquire(10000); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload