Shawn Lien
Shawn Lien

Reputation: 471

Android fail to pass variable from broadcast receiver to other class

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.

Upvotes: 0

Views: 1657

Answers (1)

AAnkit
AAnkit

Reputation: 27549

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

Upvotes: 1

Related Questions