Ich verwende eine CountDownTimer
, aber es scheint nicht richtig zu funktionieren. Ich muss Geschwindigkeit erfassen; wenn es über eine bestimmte Geschwindigkeit geht, tue nichts. Ich protokolliere nur die Geschwindigkeit. Aber wenn es unter eine bestimmte Geschwindigkeit geht, muss ich eine Benachrichtigung nach einem dreiminütigen Countdown-Timer auslösen.Verwenden von CountDown Timer
Kann jemand meinen Code ansehen und sehen, was ich falsch mache? Wenn die Geschwindigkeit höher wird, ist es in Ordnung .. Es läuft nicht der Timer, aber das Problem ist, wenn es unter einer bestimmten Geschwindigkeit geht. Manchmal funktioniert der Timer, andere Zeiten, nein. Manchmal protokolliert es nur 0 und zeigt den Timer nicht an.
Ich weiß, es ist in den Bedingungen, ich habe es gerade wieder und wieder angesehen, und ich kann nicht sehen, was falsch ist. Vier Augen sind besser als zwei?
Jede Hilfe wird geschätzt. Vielen Dank.
public class SpeedManagerService extends Service implements IBaseGpsListener {
private static final String TAG = "SpeedCheckerService";
public boolean vehicleStopped = false;
public boolean timer_started = true;
float nCurrentSpeed = 0;
CountDownTimer timer = new CountDownTimer(180000, 1000) {
// If speed increases again, cancel timer.
public void onTick(long millisUntilFinished) {
Log.i("Current Ride", "Timer countdown: " + millisUntilFinished/1000 +
" seconds.");
if (vehicleStopped) {
// Vehicle should have stopped, but it has started moving again
if (nCurrentSpeed > 0) {
timer.cancel();
timer_started = false;
}
} else if (nCurrentSpeed == 0) {
// If vehicle has just slowed down,
// once speed drops to zero we have stopped
vehicleStopped = true;
}
}
public void onFinish() {
Intent resultIntent = new Intent(SpeedManagerService.this, CurrentRide.class);
NotificationCompat.Builder builder = new NotificationCompat
.Builder(SpeedManagerService.this);
builder.setContentText("Click to save Current Ride info");
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentTitle("Did you just pay for a Ride?");
builder.setContentIntent(PendingIntent.getActivity(SpeedManagerService.this, 0,
resultIntent, 0));
NotificationManagerCompat.from(SpeedManagerService.this).notify(0,
builder.build());
timer.start();
}
}
.start();
@Override
public void onCreate() {
Log.i(TAG, "in onCreate()");
LocationManager locationManager = (LocationManager) this.
getSystemService(Context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission
.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this,
android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager
.PERMISSION_GRANTED) {
return;
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
SpeedManagerService.this.updateSpeed(null);
}
// On start, run speed service, and return sticky so if error, service will restart
// on its own
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
Log.i(TAG, "Service onStartCommand");
if (nCurrentSpeed == 0) {
timer_started = false;
}
updateSpeed(null);
return Service.START_STICKY;
}
public void updateSpeed(SpeedLocation location) {
Log.i("Current Ride ", "User is in a Vehicle. Speed is: " +
Math.round(nCurrentSpeed) + " mph. ");
// If a location exists, get speed
if (location != null) {
nCurrentSpeed = location.getSpeed();
}
// In meters/second, if speed goes above 8 mph, then it will just log the
// speed as miles/hour.
if (nCurrentSpeed >= 8) {
}
// However, if speed falls below 5 mph, then countdown timer
// of 3 minutes will begin.
if (nCurrentSpeed <= 5 && !timer_started) {
// Flag to indicate if vehicle has come to a complete stop
vehicleStopped = (nCurrentSpeed == 0);
// Indicate the timer is running
timer_started = true;
timer.start();
}
}
@Override
public void onDestroy() {
timer.cancel();
Log.i(TAG, "Timer cancelled");
super.onDestroy();
}
// Binder returns null because it's not used
@Override
public IBinder onBind(Intent intent) {
return null;
}
// If location changes, update location and speed.
@Override
public void onLocationChanged(Location location) {
if (location != null) {
SpeedLocation myLocation = new SpeedLocation(location, false);
this.updateSpeed(myLocation);
}
}
// If provider is disabled, timer won't run either
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onGpsStatusChanged(int event) {
}
}
Hier ist ein Teil des logcat ich:
07-05 17:42:17.742 17023-17023/ I/Current Ride:: User is in a Vehicle. Speed is: 3 mph.
07-05 17:42:17.752 17023-17023/ I/Current Ride: Timer countdown: 179 seconds.
07-05 17:42:17.752 17023-17023/ I/Current Ride: Timer countdown: 179 seconds.
07-05 17:42:17.762 17023-17023/ I/Current Ride: Timer countdown: 179 seconds.
07-05 17:42:17.772 17023-17023/ I/Current Ride: Timer countdown: 179 seconds.
07-05 17:42:17.772 17023-17023/ I/Current Ride: Timer countdown: 179 seconds.
07-05 17:42:17.772 17023-17023/ I/Current Ride: Timer countdown: 179 seconds.
07-05 17:42:17.772 17023-17023/ I/Current Ride: Timer countdown: 179 seconds.
07-05 17:42:17.772 17023-17023/ I/Current Ride: Timer countdown: 179 seconds.
07-05 17:42:17.772 17023-17023/ I/Current Ride: Timer countdown: 179 seconds.
07-05 17:42:17.772 17023-17023/ I/Current Ride: Timer countdown: 179 seconds.
07-05 17:42:17.772 17023-17023/ I/Current Ride: Timer countdown: 179 seconds.
07-05 17:42:17.772 17023-17023/ I/Current Ride: Timer countdown: 179 seconds.
07-05 17:42:17.772 17023-17023/ I/Current Ride: Timer countdown: 179 seconds.
und es wird dies wie 25-mal tun, bevor die nächsten Meilen angezeigt werden.
Sie müssen 'setSpeed ()' selbst, und Sie sollten 'hasSpeed ()' auch verwenden, um zu überprüfen, ob eine Geschwindigkeit verfügbar ist. Wenn 'location == NULL',' nCurrentSpeed' nicht aktualisiert wird, verwenden Sie möglicherweise einen ungültigen Wert. –
Also habe ich es auf die Geschwindigkeit eingestellt, die ich brauche, um zu fangen? location.setSpeed ((float) 8); ? – Angel
Bitte lesen Sie [diese Frage] (https://stackoverflow.com/questions/11843801/find-out-the-speed-of-the-user-in-android-using-gps) für weitere Details zum Umgang mit ' getSpeed () '. –