如何在Android中计算平均速度(Float ArrayList)?

时间:2018-09-02 10:10:48

标签: android for-loop service average android-gps

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

日志

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@12874023@12.8.74 (040400-204998136):65)
    at dpz.run(:com.google.android.gms@12874023@12.8.74 (040400- 
    204998136):2)
    at dpx.handleMessage(:com.google.android.gms@12874023@12.8.74 (040400- 
    204998136):3)
    at pmn.run(:com.google.android.gms@12874023@12.8.74 (040400- 
    204998136):6)
    at pmz.run(:com.google.android.gms@12874023@12.8.74 (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);

        }

2 个答案:

答案 0 :(得分: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);

        }

答案 1 :(得分:0)

检查:

dependencies {
    classpath 'com.android.tools.build:gradle:3.3.0-alpha08'
    classpath 'com.google.gms:google-services:4.2.0'
    /*classpath 'com.google.gms:google-services:4.1.0' <-- this was the problem */
}