如何计算 Android 中的平均速度(Float ArrayList)?
How to calculate average of Speed (Float ArrayList) in Android?
我在计算平均速度时遇到问题。我有一个 FOR 循环,用于从 Float ArrayList 中获取速度变量,但在循环中获取整数会出错。当我开始时它运行正常,然后有时会发生某些事情(可能是错误)然后它从头开始计数并且它总是停在不同的整数上。
日志
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);
}
检查这个:
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);
}
试试这个代码可能会有帮助:
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);
}
我在计算平均速度时遇到问题。我有一个 FOR 循环,用于从 Float ArrayList 中获取速度变量,但在循环中获取整数会出错。当我开始时它运行正常,然后有时会发生某些事情(可能是错误)然后它从头开始计数并且它总是停在不同的整数上。
日志
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);
}
检查这个:
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);
}
试试这个代码可能会有帮助:
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);
}