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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:25:16+00:00 2026-06-06T19:25:16+00:00

Problem: I am trying to write an app that executes some code when the

  • 0

Problem:

I am trying to write an app that executes some code when the phone is plugged into a power source which is determined to be a PC, as opposed to an AC port. I also want this app to have a gui interface which the user can “force” start that same code.

I have 2 classes so far: one is the main activity and the other one is a class that extends BroadcastReceiver.

Code is below:

MainActivity.java

public class MainActivity extends Activity{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    public void func1(View view){ //stuff        }

    public void func2(View view){ //stuff        }
}

ChargingOnReceiver.java

public class ChargingOnReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
        CharSequence text = "Plug status = " + isUSBConnected(context);
        int duration = Toast.LENGTH_SHORT;
        Toast toast = Toast.makeText(context, text, duration);
        toast.show();
    }
    public static boolean isUSBConnected(Context context){
        Intent intent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
        int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
        return plugged == BatteryManager.BATTERY_PLUGGED_USB;
    }
}

AndroidManifest.xml

        <receiver android:name=".ChargingOnReceiver">
        <intent-filter>
            <action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
        </intent-filter>
    </receiver>

Side Question:

So a quick first question: How can I call methods in MainActivity from ChargingOnReceiver (e.g. func1 or fucn2)? For example, my isUSBConnected() function used to be in MainActivity but I couldn’t find a way to call it from ChargingOnReceiver so I moved it to ChargingOnReceiver.

Main Question:

The code errors out with:
*FATAL EXCEPTION: main:: java.lang.RuntimeException: Unable to start receiver com.example.ChargingOnReceiver: android.content.ReceiverCallNotAllowedException: IntentReceiver components are not allowed to register to receive intents
*

I kind of understand why it is erroring out, as I’m trying to register a receiver within a receiver, but could someone point me in the direction of how I would be able to do this correctly?

  • 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-06T19:25:17+00:00Added an answer on June 6, 2026 at 7:25 pm

    Instead of:

    context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    

    use:

    context.getApplicationContext().registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    

    This is annoying — registerReceiver() should be smarter than this — but it’s the workaround for this particular case.

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

Sidebar

Related Questions

I'm trying to write a simple app that should mute my mobile phone for
I'm trying to write a re-usable .NET Assembly that implements WCF. My current problem
I am trying to write an app that simply acts as an interface to
I'm trying to write a very simple app that will do just one very
I am trying to write a WinForms app, that allows searching for a files,
I am trying to make a simple Facebook app that takes some data from
I am trying to write some unit tests that test the endpoints for my
I am trying to write a Django app which creates events in a specific
I am trying to write an app that will connect to a database, extract
Ruby 1.9.1, OSX 10.5.8 I'm trying to write a simple app that parses through

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.