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

穿精又带淫゛_ 提交于 2019-12-18 03:38:06

问题


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 Nexus 4 has been on today.

Android shows me how long the screen has been on since last charge via Setting > Battery > Screen, but I'm not sure how to parse this data, or how to limit it to the current day. My research so far indicates that batterystats.bin is involved with screen tracking outlined above, and that I might be able to use ACTION_SCREEN_ON and ACTION_SCREEN_OFF for this, but I'm not sure if that's exactly what I'm looking for.

Any ideas?


回答1:


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>



回答2:


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);


来源:https://stackoverflow.com/questions/21673696/how-do-i-track-screen-on-time-on-android

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!