Detect when the ANDROID Screen Goes Off ?

By | January 18, 2011

This is the main Java file that extends activity.
Create a file named ScreenON_OFF_ACTIVITY.java and place the below code in it.

package com.pack;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;

public class ScreenON_OFF_ACTIVITY extends Activity {

  public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         onCreate();
  }
  public void onCreate() {

         // initialize receiver
     System.out.println("onCreate1 ");
         IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
         filter.addAction(Intent.ACTION_SCREEN_OFF);
         BroadcastReceiver mReceiver = new ScreenReceiver();
         registerReceiver(mReceiver, filter);
         System.out.println("onCreate ");
     }

     @Override
     protected void onPause() {
         // when the screen is about to turn off
         if (ScreenReceiver.screenOff) {
             // this is the case when onPause() is called by the system due to a screen state change
             System.out.println("SCREEN TURNED OFF");
         } else {
             // this is when onPause() is called when the screen state has not changed
           System.out.println("this is when onPause() is called when the screen state has not changed ");

         }
         super.onPause();
     }
     @Override
     protected void onResume() {
         // only when screen turns on
         if (!ScreenReceiver.screenOff) {
             // this is when onResume() is called due to a screen state change
             System.out.println("SCREEN TURNED ON");
         } else {
             // this is when onResume() is called when the screen state has not changed
           System.out.println(" this is when onResume() is called when the screen state has not changed ");
         }
         super.onResume();
     }
 }

This is the class that extends the Broadcast receiver class that receives the notifications.
create a java file and name it ScreenReceiver.java and place the following code in it.

package com.pack;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class ScreenReceiver extends BroadcastReceiver {

    public static boolean screenOff;

    @Override
    public void onReceive(Context context, Intent intent) {

      System.out.println("onReceive ");
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            screenOff = true;
            System.out.println("SCREEN TURNED OFF on BroadcastReceiver");
        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
            screenOff = false;
            System.out.println("SCREEN TURNED ON on BroadcastReceiver");
        }
        Intent i = new Intent(context, UpdateService.class);
        i.putExtra("screen_state", screenOff);
        context.startService(i);
    }

}

This the class that updates the service name it UpdateService.java.

package com.pack;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;

public class UpdateService extends Service {

    public void onCreate() {
        super.onCreate();
        // register receiver that handles screen on and screen off logic

        IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        BroadcastReceiver mReceiver = new ScreenReceiver();
        registerReceiver(mReceiver, filter);
    }

    @Override
    public void onStart(Intent intent, int startId) {
        boolean screenOn = intent.getBooleanExtra("screen_state", false);
        if (!screenOn) {
         System.out.println("Screen is off");
        } else {
            System.out.println("Screen is on");
        }
    }

 @Override
 public IBinder onBind(Intent intent) {
 return null;
 }
}

Leave a Reply

Your email address will not be published. Required fields are marked *