How do I track “screen on” time on Android?

前端 未结 3 1607
面向向阳花
面向向阳花 2020-12-16 04:24

I\'m trying to do something (theoretically) simple with my first app, but I need some help getting started. I\'d like to build an app that tells me how long the screen on my

相关标签:
3条回答
  • 2020-12-16 04:41

    I know this is a old question but hopefully this will help someone.

    Within my service i listen for SCREEN_ON/SCREEN_OFF actions and grab the difference between the two times and save to sharedpreferences within onDestroy callback.

        BroadcastReceiver mybroadcast = new BroadcastReceiver() {
    
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.i("[BroadcastReceiver]", "MyReceiver");
    
            if(intent.getAction().equals(Intent.ACTION_SCREEN_ON)){
                startTimer = System.currentTimeMillis();
            }
            else if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){
                endTimer = System.currentTimeMillis();
                screenOnTime = endTimer - startTimer;
    
               if(screenOnTime < TIME_ERROR) {
                   times.add(screenOnTime);
               }
    
            }
    
        }
    };
    

    The service is started on boot and when the package is replaced (I find it helpful for debugging).

    public class BootReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            Intent i = new Intent(context, ScreenOnService.class);
            context.startService(i);
        }
    }
    

    And here is my manifest for the broadcast receiver

            <receiver android:name=".BootReceiver">
                <intent-filter>
                    <action android:name="android.intent.action.BOOT_COMPLETED" />
                </intent-filter>
                <intent-filter>
                    <action android:name="android.intent.action.PACKAGE_REPLACED" />
                    <data android:scheme="package" android:path="com.application.test" />
                </intent-filter>
            </receiver>
    
    0 讨论(0)
  • 2020-12-16 04:58

    Better implement as a BroadcastReceiver:

    public class ScreenTimeBroadcastReceiver extends BroadcastReceiver {
    
        private long startTimer;
        private long endTimer;
        private long screenOnTimeSingle;
        private long screenOnTime;
        private final long TIME_ERROR = 1000;
    
        public void onReceive(Context context, Intent intent) {
            Log.i(P.TAG, "ScreenTimeService onReceive");
    
            if(intent.getAction().equals(Intent.ACTION_SCREEN_ON)){
                startTimer = System.currentTimeMillis();
            }
            else if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){
                endTimer = System.currentTimeMillis();
                screenOnTimeSingle = endTimer - startTimer;
    
                if(screenOnTimeSingle < TIME_ERROR) {
                    screenOnTime += screenOnTime;
                }
    
            }
        }
    }
    

    You will need to register the receiver like this (in onCreate activity for example):

        screenTimeBroadcastReceiver = new ScreenTimeBroadcastReceiver();
        IntentFilter lockFilter = new IntentFilter();
        lockFilter.addAction(Intent.ACTION_SCREEN_ON);
        lockFilter.addAction(Intent.ACTION_SCREEN_OFF);
        registerReceiver(screenTimeBroadcastReceiver, lockFilter);
    
    0 讨论(0)
  • 2020-12-16 04:59

    Another solution can be to call

    UsageStatsManager.queryEvents

    with the start of the day as first parameter and the current time as end parameter and then filter the results to check just the

    UsageEvents.Event.SCREEN_INTERACTIVE UsageEvents.Event.SCREEN_NON_INTERACTIVE

    events

    take the timestamp of each of them and sum all the time passed between each

    SCREEN_INTERACTIVE -> SCREEN_NON_INTERACTIVE

    events, this would easily show how much time the screen was interactive, so screen on and the touch enabled.

    There is a possibility that you'll have a SCREEN_INTERACTIVE event that doesn't have any related SCREEN_NON_INTERACTIVE, this is when the

    UsageEvents.Event.DEVICE_SHUTDOWN

    event happen, luckily this event is in the same list returned by the queryEvents method

    remember also to declare and make the user to allow the android.permission.PACKAGE_USAGE_STATS permission as stated here

    ps. i haven't used this yet but looking at the documentation it's the right thing to use, probably the one used by the android settings pps. look also to the other events in the same package, they might be helpful to what you want to accomplish

    0 讨论(0)
提交回复
热议问题