This video shows the steps to check the Display state of your Android Device. This returns ON if the screen of the device is ON. Else it shows OFF.
I hope you like this video. For any questions, suggestions or appreciation please contact us at: https://programmerworld.co/contact/ or email at: programmerworld1990@gmail.com
Complete source code and other details:
package com.programmerworld.dispalystateapp;
import androidx.appcompat.app.AppCompatActivity;
import android.hardware.display.DisplayManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.Display;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
}
public void buttonDisplayState(View view){
Runnable runnable = new Runnable() {
@Override
public void run() {
DisplayManager displayManager = (DisplayManager) getSystemService(DISPLAY_SERVICE);
if (displayManager.getDisplay(0).getState() == Display.STATE_ON){
textView.setText("Screen Display State is ON");
} else {
textView.setText("Screen Display State is OFF");
}
}
};
Handler handler = new Handler(Looper.getMainLooper());
handler.postDelayed(runnable, 2000); // Delayed by 2 seconds
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="360dp"
android:layout_height="169dp"
android:text="Hello World!"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="149dp"
android:layout_marginTop="55dp"
android:onClick="buttonDisplayState"
android:text="Display State"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Screenshots:
Initial screen:
When screen was left ON during the wait period of 2 seconds:
When screen was turned OFF during the wait period of 2 seconds:
Project Folder:
Complete project folder can be accessed from the below link on Payment of USD 9.
https://drive.google.com/file/d/1YM0WmgaJDpeafGo_UTmPuf4W6k7y0ifr/view?usp=drive_link
Excerpt:
This video demonstrates how to check the display state of an Android device, displaying “ON” if the screen is activated and “OFF” if it is deactivated. The accompanying source code uses the DisplayManager to achieve this. The video also provides a link to the full project folder for purchase and offers support for any inquiries or payments. Screenshots of the display under different conditions are included. For further details or to obtain access to the complete project folder after payment, please visit https://programmerworld.co/contact/ or email programmerworld1990@gmail.com.