如何在Android中计算Speed(Float ArrayList)的平均值?

问题描述 投票:0回答:2

我对速度平均计算有问题。我有一个FOR LOOP用于从Float ArrayList获取速度变量,但在LOOP中取整数出错。当我开始它正确,然后有时会发生一些事情(可能是错误),然后它从头开始计数,它总是停在不同的整数上。

LOG

09-02 12:55:18.422 9380-9380/com.mcarrow.mapsservicerunning D/MyLogs: int 0
09-02 12:55:18.423 9380-9380/com.mcarrow.mapsservicerunning D/MyLogs: int 1
int 2
int 3
int 4
int 5
int 6
int 7
int 8
int 9
int 10
int 11
int 12
int 13
int 14
int 15
int 16
int 17
int 18

09-02 12:55:19.118 4825-9360/? D/ConnectivityService: 
filterNetworkStateForUid() uid: 10034 networkInfo: [type: MOBILE[LTE] - 
MOBILE, state: CONNECTED/CONNECTED, reason: (unspecified), extra: 
internet.tele2.lv, failover: false, available: true, roaming: false, 
metered: true]
09-02 12:55:19.256 6156-10625/? E/ctxmgr: [BaseServerTask]Server task 
(WriteInterestRecordTask) got error statusCode=-1.
com.android.volley.VolleyError: Unable to obtain auth token - is the device 
online?
    at dut.a(:com.google.android.gms@[email protected] (040400-204998136):65)
    at dpz.run(:com.google.android.gms@[email protected] (040400- 
    204998136):2)
    at dpx.handleMessage(:com.google.android.gms@[email protected] (040400- 
    204998136):3)
    at pmn.run(:com.google.android.gms@[email protected] (040400- 
    204998136):6)
    at pmz.run(:com.google.android.gms@[email protected] (040400- 
    204998136):28)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
    at ptb.run(Unknown Source:7)
    at java.lang.Thread.run(Thread.java:764)
    09-02 12:55:19.257 6156-10625/? E/ctxmgr: [SyncServerInterestRecordsOperation]Failed WriteInterestRecord: network status=-1

09-02 12:55:19.424 9380-9380/com.mcarrow.mapsservicerunning D/MyLogs: 
int 0
int 1
int 2
int 3
int 4
int 5
int 6
int 7
09-02 12:55:19.425 9380-9380/com.mcarrow.mapsservicerunning D/MyLogs: int 8
int 9
int 10
int 11
int 12
int 13
int 14
int 15
int 16
int 17
int 18
int 19

这是服务代码

    ArrayList <Float> speedcounts = new ArrayList();
    float averSpeed = 0;
    float averSpeedcount = 0;

    public int onStartCommand(Intent intent, int flags, int startId) {

    Intent notificationIntent=new Intent(this,MapsActivity.class);
    PendingIntent pendingIntent=PendingIntent.getActivity(this,
            0,notificationIntent,0);
    listener=new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            Intent intent = new Intent("location_update");

            float speed = location.getSpeed() * 3600 / 1000;
            intent.putExtra("speed", speed);

            speedcounts.add(speed);
            for(int i = 0; i < speedcounts.size(); i++) {
                averSpeedcount += speedcounts.get(i);

                Log.d(TAG,"int "+i);
                averSpeed = averSpeedcount / speedcounts.size();
            }
            intent.putExtra("avg_speed",averSpeed);
            sendBroadcast(intent);

        }
android for-loop service average android-gps
2个回答
1
投票

试试这段代码可能有用:

float speedSum = 0;
int speedCounts = 0;

public int onStartCommand(Intent intent, int flags, int startId) {

    Intent notificationIntent=new Intent(this,MapsActivity.class);
    PendingIntent pendingIntent=PendingIntent.getActivity(this,
            0,notificationIntent,0);
    listener=new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            Intent intent = new Intent("location_update");

            float speed = location.getSpeed() * 3600 / 1000;
            intent.putExtra("speed", speed);
            speedSum += speed;
            speedCounts++;

            float averSpeed = speedSum / speedCounts;

            intent.putExtra("avg_speed",averSpeed);
            sendBroadcast(intent);

        }

0
投票

检查一下:

public void onLocationChanged(Location location) {
                Intent intent = new Intent("location_update");

                float speed = location.getSpeed() * 3600 / 1000;
                intent.putExtra("speed", speed);

                speedcounts.add(speed); //size of speedcounts is only 1
                for(int i = 0; i < speedcounts.size(); i++) { //just loop once
                    averSpeedcount += speedcounts.get(i);

                    Log.d(TAG,"int "+i);
                    averSpeed = averSpeedcount / speedcounts.size();
                }
                intent.putExtra("avg_speed",averSpeed);
                sendBroadcast(intent);

            }
© www.soinside.com 2019 - 2024. All rights reserved.