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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:05:08+00:00 2026-06-13T15:05:08+00:00

I am trying to work on a simple android app that when the device

  • 0

I am trying to work on a simple android app that when the device is plugged in, i.e. via a dock, the screen aquires a wake lock so that the screen does not turn off, no matter what app their in, and when unplugged the wake lock is released.

At the moment I don’t have any service runing as I was thinking it wasn’t needed. It receives the broadcast intent for power connected and power disconnected. When the power is connected it seems to successfully aquires the wake lock but when the power is disconnected it tries to release the wake lock but instead throws a null pointer exception. I am guessing its because the app isn’t necessarily running and there is no service so the variable doesn’t get kept.

Below is the code I am using that receives the broadcasts.

public class BroadcastReceiveDetection extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) 
    {
        ManageScreenLight manageScreenLight = new ManageScreenLight(context);
        String action = intent.getAction();

        if (action == Intent.ACTION_POWER_CONNECTED)
        {
            manageScreenLight.receivedPowerConnected();
        }
        else if (action == Intent.ACTION_POWER_DISCONNECTED)
        {
            try {
                manageScreenLight.receivedPowerDisconnected();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

Below is the code that aquires and releases the wake lock.

Context context;
    PowerManager.WakeLock wakeLock;
    public ManageScreenLight(Context context)
    {
        this.context = context;
    }

    public void receivedPowerConnected()
    {
        Toast.makeText(context, "Power connected", Toast.LENGTH_LONG).show();

        PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
        wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "ScreenStay");
        wakeLock.acquire();
    }

        public void receivedPowerDisconnected() throws IOException
{
    try
    {
        Toast.makeText(context, "Power disconnected", Toast.LENGTH_LONG).show();
        wakeLock.release();
    }
    catch (Exception ex)
    {
        Toast.makeText(context, ex.toString(), Toast.LENGTH_LONG).show();
    }
}

Is there a way that this can work I would there need to be a service that is running when the wake lock is aquired, and once power is disconnected release and stop the service.

Thanks for any help you can provide.

  • 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-13T15:05:09+00:00Added an answer on June 13, 2026 at 3:05 pm

    Well, based on the BroadcastReceiver documentation, it looks like onReceive() doesn’t get called when your application is killed, which makes sense. Android won’t launch your application if it has killed it just to call onReceive(). You will need to use a service if you want to continue receiving/responding to these events.

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

Sidebar

Related Questions

I'm trying to build a simple Android app that increments a number displayed every
I am trying to create a simple 3-D app for android that will have
I'm trying to do an Android app that needs to work with Google spreadsheet
I am trying to program a simple Android app that will stream an internet
I am trying to write a simple android app that switches off my phone
I am trying to work with a simple HTTPService. The problem is that my
I'm trying to get a very simple Spring @mvc app to work and I'm
I have a simple Class Hierarchy that I am trying to get to work
I'm trying to do a simple date format, it does work great, it's very
I'm trying to create a ProgressDialog for an Android-App (just a simple one showing

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.