سؤال

How would you identify the user isn't using the mobile for 30 mins?

Using Jiro to see it's in horizontal position?

Is there any Android built-in flag for that?

هل كانت مفيدة؟

المحلول

You should try working with events based on the display and when it has been enabled the last time.

Register a Broadcast Receiver for ACTION_SCREEN_ON, ACTION_SCREEN_OFF and ACTION_USER_PRESENT and save the timestamp properly.

Please note, that the Screen-Events could be also fired by applications like WhatsApp if they automatically enable the display to show a new message. Due to this fact should you rather stick to ACTION_USER_PRESENT.

Here is some code:

Android-Manifest.xml

<receiver android:name=".UserPresentBroadcastReceiver">
  <intent-filter>
    <action android:name="android.intent.action.USER_PRESENT" />
  </intent-filter>
</receiver>

Broadcast Receiver

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class UserPresentBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context arg0, Intent intent) {

        /*Sent when the user is present after 
         * device wakes up (e.g when the keyguard is gone)
         * */
        if(intent.getAction().equals(Intent.ACTION_USER_PRESENT)){

        }
    }

}

Credits for the code go to Chathura Wijesinghe

Note: You will need a seperate thread (preferably a daemon-thread) to compare that timestamp with the current time.

نصائح أخرى

Listen for ACTION_SCREEN_OFF to start timer, and clear timer only if you receive ACTION_USER_PRESENT, so you won't accidentally clear your timer when the screen is turned on by some apps.

For above method you can include the following when you start your timer. This way you take the auto-lock delay into account when the screen is off.

Settings.Secure.getLong(getContentResolver(), "lock_screen_lock_after_timeout", 5000);

Alternatively, you can use KeyguardManager to check if the lock screen is enabled every 1ms or so after screen is off.

In general, the idea is lock screen --> idle; no lock screen --> not idle

You can check if the screen is turned off by calling isScreenOn method.

Note: This only can be used for 2.1 and above

Also you can also use Intent.ACTION_SCREEN_OFF to determine the status of your screen. Check this link for an example.

Reference: isScreenOn and this.

A lot depends on how you define "Idle" , screen off need not mean your device is idle , the most common secnario been if there is a app holding a PARTIAL_WAKE_LOCK

  1. ) One way to tackle the problem would what many users have suggested here to listen to SCREEN_ON /OFF and ACTION_USER_PRESENT in combination with keygaurd or windowmanager api to detect if user input is happening.

Though this wont work if you have background service running let say like utorrent app.

Hence if you want to use screen on /off option I would
suggest you also check for active wakelocks("adb shell dumpsys power" to find current active wakelocks held by the device )

There is one more way you can try though it may a bit tedious (depends how accurately you want to define IDLE )

Use Systrace(LINK) feature exposed by android to track cpu frequency and host of other parameter this way you can monitor various degrees of "idle-ness" as well.

there are dedicated options to track CPU load and cpu idle events

1.) Android 4.3 onwards(api 18 and above) :use "idle"( CPU Idle) and "load" ( CPU Load) as list category options.

2.) Android 4.2 and below (below api 18) : use "-i, --cpu-idle" to trace cpu idle events and "-l, --cpu-load" for cpu load percentage ( though for your need cpu idle events shall do the job ).

go through the link for detailed description, thanks.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top