【问题标题】:Passing parameters between service and activity在服务和活动之间传递参数
【发布时间】:2013-01-14 13:09:25
【问题描述】:

将浮点值从服务传递到活动的代码:

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

在activity中获取float值的代码:

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

问题是无论从服务作为浮点值传递什么,我在活动中都只得到 0.0。

代码有什么问题?

编辑

我把activity中的代码改成了

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

没用。

【问题讨论】:

  • 在从捆绑包中获取价值之前,您是否添加了extras = getIntent().getExtras();
  • 正如 ρяσѕρєя K 提到的,你应该从 onStartCommand 的意图中获取你的额外内容,而不是构建新的。
  • new Bundle 是空的,因为它是新的...
  • @ρяσѕρєяK - 我现在试过了,似乎没有用。还是 0。
  • @Torcellite :首先确保您在 onCreate 方法中尝试此代码,如果您仍然为空,那么也尝试float value=getIntent().getFloatExtra("floatvalue",0);

标签: android android-intent service android-activity bundle


【解决方案1】:

试试这个:

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

由于您在启动之前将浮动添加到您的意图中,因此您应该从该意图而不是捆绑包中获取浮动。

【讨论】:

  • 在服务中最好从onStartCommand中获取intent.getFloat,因为onStartCommand可以被多次调用,而onCreate在一个生命周期内只能调用一次。
  • 你能解释一下为什么这是回答 OP 的问题吗?此答案因长度而被标记。
  • @JonasG.Drange 你能澄清一下你刚才说的话吗?
  • Nader,请用简短的解释来扩展您的答案,说明为什么这段代码可以解决问题。见stackoverflow.com/questions/how-to-answer
  • @NaderAyyad 我收到了这个错误The method getFloat(String) is undefined for the type Intent
【解决方案2】:

像这样在你的服务中定义一个监听器:

// 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);
        }
    }
}
}

并像这样在您的活动中注册它:

 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);

欲了解更多信息,请查看此链接:https://stackoverflow.com/a/7709140/779408

【讨论】:

  • 这与我的问题有什么关系?我只是想传递一个原始的浮点值。
  • @Torcellite 你必须自定义它。我改变了它。再看一遍。
  • Torcellite 的问题是如何将参数从服务传递到“要新建的活动”。答案是如何将参数或任何东西传递给已经运行的活动。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-10
  • 2019-01-07
  • 1970-01-01
  • 1970-01-01
  • 2021-09-21
  • 2013-05-21
  • 2023-03-30
相关资源
最近更新 更多