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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:12:38+00:00 2026-06-08T02:12:38+00:00

Hey I am working on app that need to pass variable receive from broadcastreceiver

  • 0

Hey I am working on app that need to pass variable receive from broadcastreceiver to the service class

I have see this post
How to pass value from Broadcast receiver to Activity in android?
but its not work.

Here is my code

public class BatteryReceiver extends BroadcastReceiver {
int batteryLevel;
int batteryScale;
private int status;
private boolean isCharging;
@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    Log.v("test battery", "battery:" + batteryLevel);
    batteryLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
    batteryScale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 0);

    //Log.v("charging state", "state:" + isCharging);
    status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
    isCharging = status == BatteryManager.BATTERY_STATUS_FULL||status == BatteryManager.BATTERY_STATUS_CHARGING;

    Intent service = new Intent(context, MyService.class);      
    service.putExtra("BatteryLevel", batteryLevel);
    service.putExtra("BatteryScale", batteryScale);
    service.putExtra("Charging", isCharging);
context.startService(service);
}

}

The service class to receive the 3 variable

public class MyService extends Service {
int batteryLevel;
int batteryScale;
private boolean isCharging;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    batteryLevel = intent.getIntExtra("BatteryLevel", batteryLevel);
    batteryScale = intent.getIntExtra("BatteryScale", batteryScale);
    isCharging = intent.getBooleanExtra("Charging", isCharging);

    Log.v("test battery", "battery:" + batteryLevel);
    Log.v("charging state", "state:" + isCharging);

           //other method

           return Service.START_STICKY;
}

At the end the log always show the batterylevel is 0 and charging state is false, which means the varibale not pass to sercive

As people guide me i have assign the value to the variable as show above, but this still not work

here is my mainfest, I think there might be something wrong here

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name=".MyApp">
    </activity>

    <service 
        android:name=".MyService"
        android:enabled="true">
    </service>

    <receiver 
        android:name=".BatteryReceiver">
        <intent-filter>
             <action android:name="android.intent.action.BATTERY_CHANGED"></action>
        </intent-filter>
    </receiver>

</application>

And in receiver part the eclispe return “Exported receiver does not require permission”

In my app the service will keep run in backgroud and try to receive the variable from braodcastreceiver to perform the function

I am not sure which part I do wrong, if you spot the error plase let me know, thank you very much.

  • 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-08T02:12:39+00:00Added an answer on June 8, 2026 at 2:12 am

    you should do something like below, How can you expect Variables having value without assinging into them

    batteryLevel   =    intent.getIntExtra("BatteryLevel", batteryLevel);
     batteryScale  =       intent.getIntExtra("BatteryScale", batteryScale);
       isCharging   =       intent.getBooleanExtra("Charging", isCharging);
    

    and from where Receiver calling Service.

    you should call startService(intent); // this intent should be the one u assigned/set values into.

    intent.putExtra("BatteryLevel", batteryLevel);
    intent.putExtra("BatteryScale", batteryScale);
    intent.putExtra("Charging", isCharging);
    

    like in above code you are setting values into intent but not passing this intent to service, call startService(intent); and in service take these values in onstart() method

    in the receiver u are setting service name as WifiService.class, and your service name is MyService, how it can be possible. make it single name

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

Sidebar

Related Questions

Hey, I'm working on an iPhone app but I've heard from some people that
Hey so I have been working on a project that I want to be
Hey guys, I'm working on a cakephp app that manages a list of alpha
Hey Guys...need help. Working on a project and get this error on the Output
Hey everyone. I am working on an app that displays a modal the first
Hey guys i am currently working on an android app that will allow me
Hey everyone, I'm working an app that includes a webbrowser inside. I've done quite
Hey all, I'm working on the graphics for an app that uses a mix
Hey im trying to get a List working, in that case i have a
hey guys im working on a task to make my story's links like this

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.