How to determine if a mobile phone is idle?

How would you determine that a user has not been using a mobile phone for 30 minutes?

Using Jiro to view horizontally?

Is there any built-in Android flag?

+6
source share
4 answers

You should try to work with events based on the display and when it was last turned on.

Register the broadcast receiver for ACTION_SCREEN_ON , ACTION_SCREEN_OFF and ACTION_USER_PRESENT and save the timestamp correctly.

Note that screen events can also be triggered by applications such as WhatsApp if they automatically activate the display to display a new message. In this regard, you should adhere to ACTION_USER_PRESENT .

Here is the 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 (eg when the keyguard is gone) * */ if(intent.getAction().equals(Intent.ACTION_USER_PRESENT)){ } } } 

Credits for code go to Chathura Wijesinghe

Note. To compare this timestamp with the current time, you need a separate thread (preferably a daemon thread).

+4
source

Listen to ACTION_SCREEN_OFF to start the timer, and clear the timer only if you received ACTION_USER_PRESENT , so you will not accidentally clear your timer when you turn on the screen with some applications.

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

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

Alternatively, you can use KeyguardManager , to see if the lock screen is enabled every 1 ms after the shutdown screen.

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

+4
source

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

Note: This can only be used for version 2.1 and higher.

You can also use Intent.ACTION_SCREEN_OFF to determine the status of your screen. Check this out for an example.

Link: isScreenOn and.

+2
source

Much depends on how you define "Idle", the screen off does not mean that your device does not work, the most common secnario was if there is an application containing PARTIAL_WAKE_LOCK

  • ) One way to solve this problem is that many users have suggested listening to SCREEN_ON / OFF and ACTION_USER_PRESENT in combination with keygaurd or windowmanager api here to determine if the user is logging in.

Although this will not work if you have work with a background service, let them say as a utorrent application.

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

There is another way to try, although it can be a bit tedious (it depends on how accurately you want to define IDLE)

Use the Systrace ( LINK ) function set by android to track the processor frequency and many other parameters in this way, you can control the various degrees of "inactivity" -ness ".

There are special options for monitoring CPU usage and CPU downtime.

1.) Android 4.3 and higher (api 18 and higher): use "idle" (CPU Idle) and "load" (processor load) as parameters of the list category.

2.) Android 4.2 and below (below api 18): use "-i, --cpu-idle" to track cpu idle events and "-l, --cpu-load" for the percentage of CPU load (although for your CPU need inactive events should do the job).

follow the link for a detailed description, thanks.

+1
source

All Articles