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 8056915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:51:39+00:00 2026-06-05T08:51:39+00:00

I’ve a problem with an app I’m coding. I need to receive ACTION_SCREEN_ON, ACTION_SCREEN_OFF

  • 0

I’ve a problem with an app I’m coding.
I need to receive ACTION_SCREEN_ON, ACTION_SCREEN_OFF and ACTION_USER_PRESENT intents everytime they’re broadcasted, so my app of course stays in background. At the moment my app is made by a settings activity and a service. ScreenReceiver is a BroadcastReceiver that gets the ACTION_SCREEN_* intents, while UnlockReceiver gets the ACTION_USER_PRESENT intent. The service registers and unregisters the receivers:

public class MainService extends Service {
    ScreenReceiver screenReceiver = null;
    UnlockReceiver unlockReceiver = null;

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

    @Override
    public void onStart(Intent intent, int startId) {
        doStart();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        doStart();

        return START_STICKY;
    }

    public void doStart() {
        if(screenReceiver != null && unlockReceiver != null)
            return;

        IntentFilter filter;

        if(screenReceiver == null) {
            filter = new IntentFilter();
            filter.addAction(Intent.ACTION_SCREEN_ON);
            filter.addAction(Intent.ACTION_SCREEN_OFF);

            screenReceiver = new ScreenReceiver();
            registerReceiver(screenReceiver, filter);
        }

        if(unlockReceiver == null) {
            filter = new IntentFilter();
            filter.addAction(Intent.ACTION_USER_PRESENT);

            unlockReceiver = new UnlockReceiver();
            registerReceiver(unlockReceiver, filter);
        }
    }

    @Override
    public void onDestroy() {
        if(screenReceiver != null)
            unregisterReceiver(screenReceiver);

        if(unlockReceiver != null)
            unregisterReceiver(unlockReceiver);
    }
}  

But sometimes Android kills my service to free some RAM and then restarts it. The time between the kill and the respawn is usually around 5 seconds, but sometimes this can be enought to miss some intents causing problems to the users of my app. Those intents can be registered only trought registerReceiver, so I can’t register them in the manifest. How could I listen to those intents without being killed or missing some?

Thanks!

  • 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-06-05T08:51:41+00:00Added an answer on June 5, 2026 at 8:51 am

    How could I listen to those intents without being killed or missing some?

    You don’t. You rewrite your app to either not use those broadcasts or to be more resilient in the case of missing broadcasts. After all, it is not just “Android kills my service to free some RAM and then restarts it”, but task managers and the like that can get rid of your service.

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

Sidebar

Related Questions

I am using Paperclip to handle profile photo uploads in my app. They upload
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I need to clean up various Word 'smart' characters in user input, including but
I am currently running into a problem where an element is coming back from
I need a function that will clean a strings' special characters. I do NOT
I have thousands of HTML files to process using Groovy/Java and I need to

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.