2

Code for passing float value from service to activity:

call.putExtra("floatvalue", fv);
call.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(call);

Code for getting float value in the activity:

Bundle extras=new Bundle();
float value = extras.getFloat("floatvalue");

The problem is no matter what is passed as the float value from the service, I get only 0.0 in the activity.

What is the problem with the code?

EDIT

I changed the code in the activity to

Bundle extras=new Bundle();
extras=getIntent().getExtras();
float value = extras.getFloat("floatvalue");

It didnt work.

6
  • 3
    have u added extras = getIntent().getExtras(); before getting value from bundle ? Jan 14, 2013 at 13:14
  • As ρяσѕρєя K mentioned, you should get your extras from intent in onStartCommand, not construct the new one. Jan 14, 2013 at 13:16
  • 1
    new Bundle is empty, as it is new...
    – njzk2
    Jan 14, 2013 at 13:18
  • @ρяσѕρєяK - I tried it now, doesn't seem to be working. It's still 0. Jan 14, 2013 at 13:20
  • @Torcellite : first make sure you are trying this code in onCreate method and if u are getting still null then also try as float value=getIntent().getFloatExtra("floatvalue",0); Jan 14, 2013 at 13:27

2 Answers 2

1

Try this:

float value =  getIntent().getFloatExtra("floatvalue", 0.0f);

Since you added the float to your intent before starting it, you should get the float from that intent not from the bundle.

6
  • In a service it is better to get intent.getFloat from onStartCommand, since onStartCommand can be called several times, but onCreate only once a lifecycle. Jan 14, 2013 at 13:18
  • Can you please explain why this is answering OP's question? This answer was flagged due to length. Jan 14, 2013 at 13:38
  • @JonasG.Drange can u please clarify what you just said?
    – N Jay
    Jan 14, 2013 at 13:40
  • Nader, please expand your answer with a short explanation as to why this piece of code would solve the problem. See stackoverflow.com/questions/how-to-answer Jan 14, 2013 at 13:43
  • @NaderAyyad I got this error The method getFloat(String) is undefined for the type Intent Jan 14, 2013 at 14:24
1

Define a listener in your service like this:

// listener ----------------------------------------------------
static ArrayList<OnNewLocationListener> arrOnNewLocationListener =
        new ArrayList<OnNewLocationListener>();

// Allows the user to set a OnNewLocationListener outside of this class and
// react to the event.
// A sample is provided in ActDocument.java in method: startStopTryGetPoint
public static void setOnNewLocationListener(
        OnNewLocationListener listener) {
    arrOnNewLocationListener.add(listener);
}

public static void clearOnNewLocationListener(
        OnNewLocationListener listener) {
    arrOnNewLocationListener.remove(listener);
}

// This function is called after the new point received
private static void OnNewLocationReceived(float myValue) {
    // Check if the Listener was set, otherwise we'll get an Exception when
    // we try to call it
    if (arrOnNewLocationListener != null) {
        // Only trigger the event, when we have any listener
        for (int i = arrOnNewLocationListener.size() - 1; i >= 0; i--) {
            arrOnNewLocationListener.get(i).onNewLocationReceived(
                    myValue);
        }
    }
}
}

And register it in your Activity like this:

 OnNewLocationListener onNewLocationListener = new OnNewLocationListener() {
            @Override
            public void onNewLocationReceived(float myValue) {

                //use your value here

                MyService.clearOnNewLocationListener(this);
            }
        };

        // start listening for new location
        MyService.setOnNewLocationListener(
                onNewLocationListener);

For more information look at this link: https://stackoverflow.com/a/7709140/779408

3
  • How is this related to my question? I'm just trying to pass a primitive float value. Jan 14, 2013 at 13:25
  • @Torcellite you must customize it. I changed it. look at it again.
    – Bob
    Jan 14, 2013 at 13:28
  • Torcellite's question is how to pass parameter from service to a "to be newly created activity". The answer is how to pass parameter or anything to a already running activity.
    – fangzhzh
    May 1, 2014 at 13:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.