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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:17:21+00:00 2026-06-04T13:17:21+00:00

I’m trying to register a broadcast receiver so that when the screen turns on,

  • 0

I’m trying to register a broadcast receiver so that when the screen turns on, airplane mode turns on as well. It WORKS as long as I stay in the activity where I register it, but as soon as I leave the activity, it stops working. Android Manifest is static so I can’t use that and besides, the Android Manifest method does not allow you to use SCREEN_ON because Android doesn’t want you to run a bunch of things when the screen wakes up so it has to be done by using registerReceiver AFAIK.

My activity:-

public class WakeActivity extends Activity {

IntentFilter screenon = new IntentFilter("android.intent.action.SCREEN_ON");

//Skipped a bunch of code here to keep it relevant. Remember, the broadcast receiver
//does in fact work while I'm in the activity, so the problem isn't with
//the missing code here. Still, let me know if I'm missing something.

//The following method is called within onCreate()

protected void airplane(int i) {
    Screen_On screenon_airplane = new Screen_On();
    if (i == 0) {
        screenon_airplane.airplanei = 0;
        registerReceiver(screenon_airplane, screenon);
    } else if (i == 1) {
        screenon_airplane.airplanei = 1;
        registerReceiver(screenon_airplane, screenon);
    } else if (i == -1) {
        unregisterReceiver(screenon_airplane);
    }
}
}

My Broadcast Receiver:-

public class Screen_On extends BroadcastReceiver {

public int airplanei;

@Override
public void onReceive(final Context context, Intent intent) {
    boolean isEnabled = Settings.System.getInt(
            context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON,
            0) == 1;
    if (airplanei == 0) {
        if (isEnabled != false) {
            Settings.System.putInt(context.getContentResolver(),
                    Settings.System.AIRPLANE_MODE_ON, 0);
            Intent turnplaneoff = new Intent(
                    Intent.ACTION_AIRPLANE_MODE_CHANGED);
            turnplaneoff.putExtra("state", 0);
            context.sendBroadcast(turnplaneoff);
        }
    } else if (airplanei == 1) {
        if (isEnabled == false) {
            Settings.System.putInt(context.getContentResolver(),
                    Settings.System.AIRPLANE_MODE_ON, 1);
            Intent turnplaneon = new Intent(
                    Intent.ACTION_AIRPLANE_MODE_CHANGED);
            turnplaneon.putExtra("state", 1);
            context.sendBroadcast(turnplaneon);
        }
    }
}
}

LogCat Error:-

05-17 23:44:24.886: E/ActivityThread(2435): Activity com.dragonheart.autodroid.ActionActivities.WakeActivity has leaked IntentReceiver com.dragonheart.autodroid.BroadCastRecievers.Screen_On@414271b0 that was originally registered here. Are you missing a call to unregisterReceiver()?

Could the error have something to do with not telling the Broadcast Receiver what to in onPause() or onDestroy()?…Although I thought once it’s registered, it would work until unregistered, regardless of the activity…

  • 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-04T13:17:22+00:00Added an answer on June 4, 2026 at 1:17 pm

    I’m not sure that your broadcast receiver will work once the activity is stopped, since your receiver has been registered programmatically and not statically through manifest (since android do not let you do so).

    I’ve looked at your code trying to understand what the code does, but that’s not simple from just a couple of lines…

    But anyway, why dont you create a service in which you register the listener to the screen state? In this way the service will not stop execution, even after the activity has stopped.

    And every time the service notice the screen turn on/off you can send a message to your main activity (check documentation).

    See this previous question, with example on how to use LocalBroadcastReceiver: LINK

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:
I am doing a simple coin flipping experiment for class that involves flipping a

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.