1

I tried to develop an app that can record how long a people spend on their cellphone. It suppose to start a chronometer once I open the app, and pause while the screen is locked. The problem is I can stop it with a pause button but I cannot do it with lock the phone.

package com.example.tony.screentimecounter812;

import android.annotation.TargetApi;
import android.app.KeyguardManager;
import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.os.PowerManager;
import android.os.SystemClock;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Chronometer;

public class MainActivity extends AppCompatActivity {
    public Chronometer chronometer;
    private long pauseOffset;
    public boolean running = false;
    boolean screenStill = true;

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    public void screenStill () {
        chronometer.start();
        while (screenStill) {
            KeyguardManager mKeyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);

            if (running) {
                assert mKeyguardManager != null;
                if (mKeyguardManager.isKeyguardLocked()) {
                    chronometer.stop();
                    pauseOffset = SystemClock.elapsedRealtime() - chronometer.getBase();
                    running = false;
                }
            }
            if (!running) {
                assert mKeyguardManager != null;
                if (!mKeyguardManager.isKeyguardLocked()) {
                    chronometer.start();
                    chronometer.setBase(SystemClock.elapsedRealtime() - pauseOffset);
                    running = true;
                }
            }
        }
    }



    @TargetApi(Build.VERSION_CODES.LOLLIPOP_MR1)
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP_MR1)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        chronometer = findViewById(R.id.chronometer);
        screenStill ();


}
Yannick Huber
  • 607
  • 2
  • 16
  • 35
tonymimi
  • 11
  • 3

1 Answers1

0

You could check if the phonescreen is turned on instead of if it is locked

This can be done by using:

PowerManager powermanager = (PowerManager) getSystemService(Context.POWER_SERVICE);
boolean isScreenOn = powermanager.isInteractive();

"When this method returns true, the device is awake and ready to interact with the user (although this is not a guarantee that the user is actively interacting with the device just this moment). The main screen is usually turned on while in this state." https://developer.android.com/reference/android/os/PowerManager#isInteractive()

This works as a workaround but the results may slightly differ. I am for example not sure if this this methods also return true when the screen gets turned on because of a notification!

Yannick Huber
  • 607
  • 2
  • 16
  • 35