当前位置: 首页 > 知识库问答 >
问题:

当更多的数据进来时,我将如何让我的活动持续执行计算

栾耀
2023-03-14

我正在尝试创建一个应用程序,该应用程序根据心电信号计算心率,心电信号通过蓝牙接收并发送到应用程序。我已经能够创建代码来检测ecg信号的峰值(这将是一个500个值的数组),计算心率并将其输出到屏幕。到目前为止,阵列是硬编码的。但我希望确保每次覆盖数组时,都会再次执行计算,并将新值输出到屏幕。因此,它本质上需要不断重复。我尝试在以下循环中插入整个代码:

while(true) { }

如下所示:

private class ProcessECG extends Thread {
    public ProcessECG() {

    }

        public void run() {

        heartRateValues = (TextView) findViewById(R.id.heartRateValues);

        double rawHeartData[] = {0.024, -0.016, -0.032, 0.016, -0.008, 0.024, 0.016, -0.016, 0.016, -0.016, 0.0, 0.112, 0.096, -0.264, -0.272, 0.136, 0.272, 0.096, 0.088, -0.016, 0.024, -0.024, -0.008, -0.04, -0.032, 0.008, -0.024, 0.008, -0.016, 0.016, 0.024, -0.024, -0.016, 0.016, -0.016, 0.016, -0.016, 0.016, 0.024, -0.008, 0.008, 0.056, 0.056, 0.008, 0.016, -0.032, -0.032, 0.0, -0.024, 0.008, -0.016, 0.024, -0.016, 0.024, -0.016, 0.016, -0.016, 0.016, 0.016, -0.016, 0.024, -0.016, -0.008, 0.024, -0.016, 0.024, 0.024, -0.016, -0.008, 0.016, 0.024, -0.016, 0.016, -0.016, -0.016, 0.024, -0.016, 0.024, -0.008, 0.024, -0.016, 0.024, 0.024, -0.016, -0.016, 0.024, -0.016, 0.016, 0.016, -0.016, -0.016, 0.024, 0.016, -0.024, -0.016, 0.024, 0.024, -0.016, 0.016, -0.016, -0.016, 0.048, 0.08, -0.064, -0.08, -0.216, -0.144, 0.2, 0.208, 0.024, 0.024, -0.008, 0.008, -0.032, -0.032, 0.008, -0.024, 0.016, 0.016, -0.024, -0.024, 0.016, 0.016, -0.024, -0.016, 0.016, -0.024, 0.016, -0.008, 0.016, -0.008, 0.032, 0.008, 0.048, 0.032, -0.016, 0.0, -0.032, -0.032, 0.0, -0.024, 0.024, 0.024, -0.016, -0.024, 0.024, -0.016, 0.016, 0.024, -0.016, -0.016, 0.024, 0.024, -0.016, -0.016, 0.024, 0.024, -0.016, -0.016, 0.016, 0.024, -0.016, -0.016, 0.024, -0.016, 0.024, -0.016, 0.024, -0.008, 0.024, 0.016, -0.016, -0.016, 0.024, -0.016, 0.016, -0.016, 0.016, -0.008, 0.024, 0.016, -0.016, -0.008, 0.024, -0.016, 0.024, 0.016, -0.024, 0.008, -0.024, -0.008, 0.024, 0.024, -0.016, -0.008, 0.016, -0.008, 0.08, 0.064, -0.264, -0.232, 0.192, 0.224, 0.072, 0.056, -0.032, 0.016, -0.032, -0.032, -0.008, -0.032, 0.008, 0.016, -0.016, -0.024, 0.016, -0.016, 0.016, 0.024, -0.016, 0.016, -0.024, 0.016, -0.016, -0.008, 0.024, 0.0, 0.04, 0.056, 0.0, 0.024, -0.024, 0.0, -0.032, -0.032, 0.016, -0.024, 0.016, -0.016, 0.016, 0.024, -0.016, 0.024, -0.008, -0.016, 0.016, -0.016, 0.016, 0.016, -0.016, 0.024, -0.016, 0.024, -0.016, -0.016, 0.024, -0.016, 0.016, -0.016, 0.024, 0.024, -0.016, 0.024, -0.016, -0.016, 0.016, -0.016, 0.024, 0.024, -0.024, 0.016, -0.016, 0.024, -0.016, 0.016, -0.016, -0.016, 0.016, -0.016, 0.024, 0.024, -0.016, -0.008, 0.024, -0.008, 0.104, 0.08, -0.28, -0.28, 0.12, 0.256, 0.128, 0.12, -0.032, 0.008, -0.016, -0.04, -0.008, -0.032, 0.008, 0.0, -0.024, -0.016, 0.024, -0.016, 0.016, 0.024, -0.024, -0.024, 0.016, 0.016, -0.016, -0.016, 0.024, 0.0, 0.056, 0.056, 0.0, 0.016, -0.032, 0.0, -0.032, -0.032, 0.016, -0.024, 0.016, -0.024, 0.016, -0.016, 0.016, 0.016, -0.016, -0.016, 0.024, 0.016, -0.016, 0.016, -0.016, -0.016, 0.024, -0.008, 0.024, -0.016, 0.024, -0.016, 0.024, -0.016, 0.024, -0.016, 0.024, 0.024, -0.016, 0.024, -0.016, 0.016, -0.008, -0.016, 0.016, -0.016, 0.016, 0.016, -0.016, 0.024, -0.016, -0.016, 0.024, -0.016, 0.024, 0.024, -0.032, 0.008, -0.032, -0.008, 0.024, 0.024, -0.008, 0.024, -0.016, -0.016, 0.024, 0.112, -0.072, -0.088, -0.272, -0.128, 0.272, 0.248, -0.008, 0.024, -0.024, 0.0, -0.048, -0.032, 0.0, -0.032, 0.008, -0.032, 0.016, -0.024, 0.016, 0.016, -0.016, 0.024, -0.016, 0.016, -0.024, -0.016, 0.016, -0.016, 0.024, 0.008, 0.056, 0.04, -0.008, 0.016, -0.032, -0.04, 0.0, -0.024, 0.016, 0.016, -0.024, 0.016, -0.016, 0.024, -0.016, -0.016, 0.016, -0.016, 0.024, -0.016, 0.016, 0.024, -0.008, 0.024, -0.016, -0.016, 0.024, -0.016, 0.016, 0.016, -0.016, -0.016, 0.024, 0.024, -0.016, -0.008, 0.024, 0.016, -0.024, -0.008, 0.024, 0.024, -0.032, 0.024, -0.016, -0.008, 0.016, -0.016, 0.024, -0.016, 0.024, 0.024, -0.016, 0.024, -0.016, -0.016, 0.016, -0.024, 0.008, -0.008, 0.024, 0.024, -0.016, 0.024, -0.016, -0.016, 0.04, 0.096, -0.152, -0.256, -0.112, -0.088, 0.264, 0.248, 0.008, -0.024, 0.008, 0.0, -0.04, -0.032, 0.0, -0.032, 0.008, -0.024, 0.016, -0.024, 0.016, -0.016, 0.024, -0.024, 0.016, 0.016, -0.024, -0.016, 0.024, -0.008, 0.032, 0.008, 0.056, 0.04, -0.016, 0.008, -0.032, 0.0, -0.032, -0.032, 0.016, -0.016, 0.016, 0.016, -0.016, 0.024, -0.016, -0.016, 0.024, -0.016, 0.024, -0.016, 0.016, 0.024, -0.016, 0.024, -0.016, -0.024, 0.024, -0.016, 0.024, -0.016, 0.016, -0.016, 0.016, 0.024, -0.016, 0.024, -0.008, -0.016, 0.024, -0.024, 0.016, -0.016, 0.016, -0.016, 0.024, 0.024, -0.016, -0.008, 0.024, 0.016, -0.024, -0.032, 0.008, -0.008, 0.024, 0.024, -0.016, 0.016, -0.016, -0.016, 0.064, 0.104, -0.224, -0.264, 0.024, 0.04, 0.248, 0.176, -0.024, -0.032, 0.016, -0.032, 0.0, 0.0, -0.032, 0.016, -0.024, -0.032, 0.008, -0.016, 0.016, 0.016, -0.032, 0.016, -0.024, 0.016, -0.016, -0.016, 0.024, -0.008, 0.04};


        double max = 0;
        for (int i = 0; i <rawHeartData.length; i++) {
            if (rawHeartData[i] > max) {
                max = rawHeartData[i];
            }
        }
        double threshold = 0.7*max;



        while (true) {

            //Calculate Heart Rate from list "Raw Data"
            int upflag = 0;
            int last = 1;
            int p = 0;
            int t = 0;
            int count = 0;
            //List<Double> heartRate2 = new ArrayList<Double>();
            int heartRate[] = new int[7];

            for (int i = 0; i < rawHeartData.length; i++) {
                if (rawHeartData[i]> threshold){
                    if (upflag == 0){
                        if (last > 0){
                            t = i - last;
                            p = 100*60/t;
                            //100 is the sampling rate

                            heartRate[count]= p;
                            count = count + 1;
                            //int k = (int) p;
                            //heartRate2.add(p);
                        }
                        last = i;
                    }
                    upflag = 50;
                }

                else {
                    if (upflag > 0){
                        upflag = upflag -1;
                    }
                }

            }
            //Change array to String and display last value
            String heartRate3 = Arrays.toString(heartRate);
            String[] bits = heartRate3.split(",");
            String lastValue = bits[bits.length-1];

            heartRateValues.setText(lastValue + "bpm");

        }

        }


}

但是,这一直导致我的应用程序崩溃,我得到了以下错误:

03-15 14:22:50.809  10094-10300/es.pymasde.blueterm E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-1165
Process: es.pymasde.blueterm, PID: 10094
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
        at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6247)
        at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:867)
        at android.view.View.requestLayout(View.java:17364)
        at android.view.View.requestLayout(View.java:17364)
        at android.view.View.requestLayout(View.java:17364)
        at android.view.View.requestLayout(View.java:17364)
        at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:360)
        at android.view.View.requestLayout(View.java:17364)
        at android.widget.TextView.checkForRelayout(TextView.java:6855)
        at android.widget.TextView.setText(TextView.java:4047)
        at android.widget.TextView.setText(TextView.java:3905)
        at android.widget.TextView.setText(TextView.java:3880)
        at es.pymasde.blueterm.ProcessActivity$ProcessECG.run(ProcessActivity.java:164)

当涉及到setText时,它会出错,但当代码位于while循环之外时,它会非常好地工作。针对我的错误,我找到了以下链接:Android“只有创建视图层次结构的原始线程才能接触其视图。”然而,片段中出现错误,我仍然不确定如何使用runOnUIThread。这是最好的解决方案吗?或者,对于如何确保我的计算在新数据到来时不断实时执行,有什么想法吗?

共有1个答案

姬昀
2023-03-14

好吧,无论如何,您都需要在用户界面线程上setText

但是,根据您正在处理的数据量和频率,您可能希望在不会导致UI线程过多工作的间隔内执行此操作。

 类似资料:
  • 我有一个托管3个片段的活动,我的活动包含一些我希望我的第一个片段使用的数据。我尝试调用以下方法,以便活动可以与片段通信: 对于活动类和: 对于fragment类,但它在运行时对空对象引用错误调用虚拟方法“void com.tex.lightweatherforecast.FirstFragment.doSomething(java.lang.String)”。 我的活动代码是: 任何帮助将不胜感激

  • 问题内容: 有人可以从下面的数据中告诉我我的会话将持续多长时间吗?-我不确定哪个告诉我 问题答案: 通常,您可以说_session.gc_maxlifetime_ 指定自上次更改会话数据( 而不是 上次调用!)以来的最长生存时间。但是PHP的会话处理有点复杂。 因为会话数据由垃圾收集器,其仅由称为除去用的概率_session.gc_probability合_ 由devided session.gc

  • 正如问题所述,我有一个机器人,我正在使用它向所有被邀请加入的行会发送特定频道的消息。首先,在我的控制实验中,这个功能在我用3台服务器做的控制实验中起作用,我用机器人需要的通道来中继我所有的消息。然后,当我的bot公开时,一些成员的服务器中没有此通道,这导致我的bot在执行我的测试/中继功能时显示。 我希望bot能够将我的中继消息发送到我指定的频道名称,并忽略其服务器中没有该频道的所有人,并继续将消

  • 我有一个按钮和EditText值。我使用Intent将EditText值从Activity1发送到Activity2。 我在Activity2上使用来自Activity1的EditText值获得Datavalue,并在使用意图单击按钮后将新的Datavalue Activity2发送到Activity3。 我在Activity3页面上有一些数据价值。 如何让这起作用?

  • 我的问题是,我已经创建了一个传输控制协议,当它停留没有传输任何数据约1小时,它已经从服务器断开连接,但它不通知我,它已断开连接,我应该发送保持活动的数据包到服务器吗?或者我应该发送保持活动的数据包从服务器到客户端?或者我应该发送两个?

  • 所以我为我的玩家制作了一个跳跃动画,我一直在想如何制作它,以便当我的玩家接触地面/平台时,它将停止播放我的跳跃动画,并将播放我的空闲动画。我试图让它播放动画,当玩家按下空格键,玩家的摔倒是 我想知道的是,它在做什么:https://gyazo.com/5cfda577628e5596d033b11a7486bbf9?token=6f82939da2c6b2363232b3e3f379aa07 我的