Detect screen on and off when app closed

Hi i have problem to detect the screen on and screen off events when my app closed. I can only detect the events when my app is active. I tried to place a broadcast receiver to receive an ACTION_SCREEN_ON and ACTION_SCREEN_OFF intents in androidmanifest.xml file, but it doesn't work. Please help me...

By sokdara asked on 2015-09-25
answer to this question

By yuk.dara

You can't receive the SCREEN_ON and SCREEN_OFF intents by placing the broadcast receiver in manifest file. A workable solution is to register the broadcast receiver in a service. You follow the steps below to implement the solution.
1. You create a broadcast receiver.

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class ScreenReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent){
Log.e("Log", "The screen is on.");
Log.e("Log", "The screen is off.");

2. Then create a service to register the receiver.
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
public class ScreenService extends Service{
private BroadcastReceiver sReceiver;
public IBinder onBind(Intent arg){
return null;
public int onStartCommand(Intent intent,int flag, int startIs){
// Detect screen off
IntentFilter filter=new IntentFilter(Intent.ACTION_SCREEN_ON);
sReceiver=new ScreenReceiver();
public void onDestroy(){
3. Finally, in the manifest file you declare the service.
 <service android:name=".ScreenService" />

2015-09-25 Reply

This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.