Get device uptime

Updated in Android

Android operating system is built on top of Linux which provides the same system tools that are commonly found in Unixes. This tutorial is going to show how to get the uptime information and show it to the user.

This tutorial uses the Handler class to fire the UI updates, so you might to want to check that class out if it’s completely new to you.

Basics

The uptime information is available from the SystemClock class. It provides two distinct uptime information, the whole uptime since device boot and the uptime without deep sleep time. I.e. you can get the either the whole time or the time the device has been used.

Example

package com.tanelikorri.android.uptime;

import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.Locale;
import java.util.concurrent.TimeUnit;

public class UptimeActivity extends AppCompatActivity {

    private TextView wholeView;
    private TextView withoutView;

    private Handler updateHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_uptime);

        wholeView = findViewById(R.id.uptime_whole_count);
        withoutView = findViewById(R.id.uptime_without_sleep_count);

        updateHandler = new Handler();

        updateUptimes();

    }

    private void updateUptimes() {

        // Get the whole uptime
        long uptimeMillis = SystemClock.elapsedRealtime();
        String wholeUptime = String.format(Locale.getDefault(),
                "%02d:%02d:%02d",
                TimeUnit.MILLISECONDS.toHours(uptimeMillis),
                TimeUnit.MILLISECONDS.toMinutes(uptimeMillis)
                        - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS
                        .toHours(uptimeMillis)),
                TimeUnit.MILLISECONDS.toSeconds(uptimeMillis)
                        - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS
                        .toMinutes(uptimeMillis)));
        wholeView.setText(wholeUptime);

        // Get the uptime without deep sleep
        long elapsedMillis = SystemClock.uptimeMillis();
        String withouUptime = String.format(Locale.getDefault(),
                "%02d:%02d:%02d",
                TimeUnit.MILLISECONDS.toHours(elapsedMillis),
                TimeUnit.MILLISECONDS.toMinutes(elapsedMillis)
                        - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS
                        .toHours(elapsedMillis)),
                TimeUnit.MILLISECONDS.toSeconds(elapsedMillis)
                        - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS
                        .toMinutes(elapsedMillis)));
        withoutView.setText(withouUptime);

        // Call updateUptimes after one second
        updateHandler.postDelayed(new Runnable() {

            @Override
            public void run() {
                updateUptimes();
            }
        }, 1000);
    }
}

In the above example the uptimes are fetched from the SystemClock class in the updateUptimes method. First the method fetches the times and then it creates “hh:mm:ss” representations from the times with the TimeUnit class. And finally a runnable is posted to the handler class so that the uptimes on the UI are updated every second.

Screenshots

Uptimes

Source code

Source code for the whole example project is available here

Further reading