无法从活动获得的数据服务 [英] Unable to get data from Activity to Service

查看:101
本文介绍了无法从活动获得的数据服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是pretty新本,但有人告诉我,我可以在这里得到很好的帮助。一位朋友和我自己都玩弄创建Android应用(使用ADT)

I am pretty new to this but I was told I could get good help here. A friend and myself are playing around with creating Android apps (using ADT)

下面是我们如何试图使程序:一个活动,用户在X轴和Y轴加速计设置阈值。当用户点击按钮开始,startService被调用,并开始TiltService。

Here is how we are trying to make the program: in an activity, a user sets threshold values for the X and Y axis on the accelerometer. When the user hits button "Start", startService is invoked and starts TiltService.

TiltService被设计在后台总是在电话上,而无需用户交互来运行。 TiltService不断阈与加速度值相比较,如果它们是关闭会振动。

TiltService is designed to run in the background always on the phone without user interaction. TiltService constantly compares the threshold with the accelerometer values and will vibrate if they are off.

我的问题是,我似乎无法得到正确的putExtra()的数据。我已重写我的服务onStartCommand,但我得到的消息不可达code当我保存的getExtras()来捆绑。

My problem is I can't seem to get the putExtra() data correctly. I have overridden the onStartCommand in my service but I get the message "unreachable code" when I save the getExtras() to a bundle.

下面是相关code(我可以张贴整个事情,只是不想阻塞页)

Here is the relevant code (I can post the whole thing, just do not want to clog up page)

TiltMeterActivity

TiltMeterActivity

private Intent dataIntent = null;
private float FORWARD_THRESHOLD = 4f;
private float BACKWARD_THRESHOLD = -3f;
private float UPWARD_THRESHOLD = 3f;
private float DOWNWARD_THRESHOLD = -3f;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        bstart = (Button)findViewById(R.id.btnStart);

        dataIntent = new Intent();
        dataIntent.setClass(TiltMeterActivity.this, TiltService.class);
        dataIntent.putExtra("fowardThreshold", FORWARD_THRESHOLD);
        dataIntent.putExtra("backwardThreshold", FORWARD_THRESHOLD);
        dataIntent.putExtra("upwardThreshold", UPWARD_THRESHOLD);
        dataIntent.putExtra("downwardThreshold", DOWNWARD_THRESHOLD);

        bStart.setOnclickListener(new View.OnClickListener() {
            public void onClick(View v) {
                startService(dataIntent);
            }
        });
    }

TiltService

TiltService

private Bundle thresholdData = new Bundle();

private float Z_FORWARD_THRESHOLD = 0f;
private float Z_BACKWARD_THRESHOLD = 0f;
private float Y_UPWARD_THRESHOLD = 0f;
private float Y_DOWNWARD_THRESHOLD = 0f;

public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub
        return super.onStartCommand(intent, flags, startId);


        thresholdData = intent.getExtras(); // The problem area (Eclipse says this is unreachable code)


        Z_FORWARD_THRESHOLD = thresholdData.getFloat("forwardThreshold");
        Z_BACKWARD_THRESHOLD = thresholdData.getFloat("backwardThreshold");
        Y_UPWARD_THRESHOLD = thresholdData.getFloat("upwardThreshold");
        Y_DOWNWARD_THRESHOLD = thresholdData.getFloat("downwardThreshold");

        return START_STICKY;
    }

我想我理解怎么可能意图传递数据的基础上,但我想我不知道。它是明显的,我缺少的是什么?感谢您的帮助。

I thought I understood the basic of how Intents could pass data, but I guess I don't. Is it obvious what I am missing? Thanks for any help

推荐答案

收益中声明你的 onstartcommand 到函数的最后一行。

put the return statement in your onstartcommand to the last line of the function.

这篇关于无法从活动获得的数据服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆