Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7166141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:21:56+00:00 2026-05-28T14:21:56+00:00

My aim is to start a service that listens for changes to the device’s

  • 0

My aim is to start a service that listens for changes to the device’s screen state (on or off) and acts upon those changes. I am aware that this is not ideal, but, nonetheless, it’s what I am trying to accomplish.

For some reason, my broadcast receiver only seems to fire when the screen comes on, but not when it goes off. In addition, logcat reveals numerous ANRs and it appears that the service is being repeatedly killed and restarted.

I followed a tutorial found :here

Here is my relevant code:

ScreenReceiver.java

    public class ScreenReceiver extends BroadcastReceiver {

        private boolean screenOff;

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
                screenOff = true;
            } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
                screenOff = false;
            }
            Intent i = new Intent(context, UpdateService.class);
            i.putExtra("screen_state", screenOff);
            context.startService(i);
        }

    }

UpdateService.java (Updated as per suggestion, now causes Force Close)

    public class UpdateService extends IntentService {

        public UpdateService(String name) {
        super(name);
        // TODO Auto-generated constructor stub
    }

        @Override
        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
        protected void onHandleIntent(Intent intent) {
            boolean screenOn = intent.getBooleanExtra("screen_state", false);
            if (!screenOn) {



                String command8 = "*******";
                String command9 = "*********";
                    int timeout = 5;

   try {
   RootTools.Result result = new RootTools.Result() {
   @Override
   public void process(String line) throws Exception {
   // Do something with current line;
   // Maybe store it using setData()
   }

   @Override
   public void onFailure(Exception ex) {
   // Do something if we failed while trying to run a command or read its output

   setError(1);
   }

   @Override
   public void onComplete(int diag) {


   }

   };

   RootTools.sendShell(
   new String[] {

   command8,
   command9},
   timeout,
   result
   );
   if(0 != result.getError())
   return;
   //Do something with getData() if needed.
   } catch (IOException e) {
   //Handle exception
   } catch (InterruptedException e) {
   //Handle exception
   } catch (RootToolsException e) {
   //TODO Auto-generated catch block
   e.printStackTrace();
   }

            } else {

             String command8 = "*******";
                String command9 = "*******";
                 String command10 = "********";
                    String command11 = "********";
                    int timeout = 5;

   try {
   RootTools.Result result = new RootTools.Result() {
   @Override
   public void process(String line) throws Exception {
   // Do something with current line;
   // Maybe store it using setData()
   }

   @Override
   public void onFailure(Exception ex) {
   // Do something if we failed while trying to run a command or read its output

   setError(1);
   }

   @Override
   public void onComplete(int diag) {
   //TODO

   }

   };

   RootTools.sendShell(
   new String[] {

   command8,
   command9,
   command10,
   command11},
   timeout,
   result
   );
   if(0 != result.getError())
   return;
   //Do something with getData() if needed.
   } catch (IOException e) {
   //Handle exception
   } catch (InterruptedException e) {
   //Handle exception
   } catch (RootToolsException e) {
   //TODO Auto-generated catch block
   e.printStackTrace();
   }
            }
        }

        @Override
        public IBinder onBind(Intent arg0) {
            // TODO Auto-generated method stub
            return null;
        }

}

The Broadcast Receiver is started by a button press, with this code:

 IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
    filter.addAction(Intent.ACTION_SCREEN_OFF);
    BroadcastReceiver mReceiver = new ScreenReceiver();
    registerReceiver(mReceiver, filter);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T14:21:57+00:00Added an answer on May 28, 2026 at 2:21 pm

    onStart() is not merely deprecated, but is called on the main application thread. The definition of ANR is that you are spending too much time on the main application thread. Please move your onStart() logic into a background thread, perhaps by subclassing IntentService and putting the logic in onHandleIntent().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a set of Meeting rooms and meetings in that having start date
Let's suppose that we have poor MVC framework without modules support. Our aim is
My aim is to restore an Oracle database back to it's previous state after
my aim is that in the function Dummy i can change the controls like
@echo off goto start :start if exist console.ccmf ( PAUSE ren console.ccmf console.bat ping
@echo off start /wait notepad start worpad This is the code i have written
I want to create a IM service, that use XMPP. I don't want to
The aim is to not even start $foo if there is no input. This
For quite a long time I've wanted to start a pet project that will
AIM: I would like to tail the last line of the file.txt and input

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.