从磨损到手机发送数据时跳过Android跳帧

时间:2016-06-13 21:27:22

标签: android multithreading accelerometer wear-os

我按照几个教程构建了一个收集我的Android磨损加速度计(x,y,x)的Android应用程序。我想将收集的数据发送到手机以便识别,以便稍后进行分析。一切正常,我有两个按钮来开始/停止收集。我收集数据并将其存储在List中,然后在停止按钮命中后,我使用一个线程将其发送到手机。  我有一些问题:

  1. 为什么我在手机上打印数据时会收到Skipped XXX frames! The application may be doing too much work on its main thread.
  2. 在这里使用AsyncTask吗?或者我应该使用Thread?
  3. 这是我的代码:

    public void onStartClicked(View view) {
        Log.v("StartClicked", "in on start clicked");
        googleClient.disconnect();
        updating = 1;
    }
    
    //when stop button clicked, the gathered data 
    //will be sent to the phone
    public void onStopClicked(View view) {
        Log.v("StartClicked", "in on stop clicked");
        googleClient.connect();
        updating = 0;
        new LongOperation().execute("");
    }
    @Override
    public void onSensorChanged(SensorEvent sensorEvent) {
            Sensor mySensor = sensorEvent.sensor;
            accelerationTextView = (TextView) findViewById(R.id.text);
            if (updating == 1) {
    
                if (mySensor.getType() == Sensor.TYPE_ACCELEROMETER) {
                    float x = sensorEvent.values[0];
                    float y = sensorEvent.values[1];
                    float z = sensorEvent.values[2];
    
    
                    double m = Math.sqrt((sensorEvent.values[0] * sensorEvent.values[0]
                            + sensorEvent.values[1] * sensorEvent.values[1] + sensorEvent.values[2]
                            * sensorEvent.values[2]));
    
                    SensorDataItem item = new SensorDataItem(m, sensorEvent.values[0], sensorEvent.values[1], sensorEvent.values[2], new Date());
                    dataQueue.add(item);
                }
            }
        }
    //when the button stop pressed, this happens
    private class LongOperation extends AsyncTask<String, Void, String> {
    
        @Override
        protected String doInBackground(String... params) {
            //
            NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes(googleClient).await();
            for (Node node : nodes.getNodes()) {
                try {
                    Thread.sleep(1000);
                    MessageApi.SendMessageResult result = Wearable.MessageApi.sendMessage(googleClient, node.getId(), path, dataQueue.toString().getBytes()).await();
                    if (result.getStatus().isSuccess()) {
                        Log.v("myTag", "Message: {" + message + "} sent to: " + node.getDisplayName());
                    } else {
                        // Log an error
                        Log.v("myTag", "ERROR: failed to send Message");
                    }
    
                } catch (InterruptedException e) {
                    Thread.interrupted();
                }
    
            }
            return "Executed";
        }
    

0 个答案:

没有答案