我创建了一个使用LocationListener
和location.getSpeed()
来计算Android设备速度的应用。我使用短信OSA 55
设置了速度限制。此处OSA为Over Speed Alert
,55为kmph
中的速度限制。
当设备行进超过55 kmph
超过10秒时,它将以当前速度和当前纬度和经度向移动号码发送短信。我使用以下代码完成了这项工作。
private void sendOverSpeedAlert() {
Toast.makeText(MainPage.this,"Over Speed Alert Set",Toast.LENGTH_LONG).show();
final SharedPreferences account=getSharedPreferences("admins",MODE_PRIVATE);
String overSpeed=account.getString("osa", "");
double overSpeedKm=Double.parseDouble(overSpeed);
final DecimalFormat dFormat = new DecimalFormat("#");
String overSpeedFormat=dFormat.format(overSpeedKm);
final double overSpeedDouble=Double.parseDouble(overSpeedFormat);
LocationManager locationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationListener() {
@Override
public void onLocationChanged(final Location location) {
double currentSpeed=(location.getSpeed())*3.6;
String currentSpeedString=dFormat.format(currentSpeed);
final double currentSpeedDouble=Double.parseDouble(currentSpeedString);
if (currentSpeedDouble>overSpeedDouble){
CountDownTimer countDownTimer=new CountDownTimer(10000,1000) {
@Override
public void onTick(long millisUntilFinished) {
}
@Override
public void onFinish() {
double latitude=location.getLatitude();
double longitude=location.getLongitude();
DecimalFormat decimalFormat=new DecimalFormat("#.####");
Date date=new Date();
SimpleDateFormat sdf=new SimpleDateFormat("kk:mm");
String time=sdf.format(date);
SmsManager smsManager=SmsManager.getDefault();
String sms=" 'ALERT-OVER SPEED'\n\n'!+"+decimalFormat.format(latitude)+",+"+decimalFormat.format(longitude)+","+time+"'\n\nSpeed='"+currentSpeedDouble+"'Km/Hr('"+overSpeedDouble+"')";
smsManager.sendTextMessage(logPreferences.getString("admin1",""),null,sms,null,null);
MediaPlayer media = new MediaPlayer();
media = MediaPlayer.create(MainPage.this, R.raw.buzzer);
media.setAudioStreamType(AudioManager.STREAM_MUSIC);
media.setLooping(false);
media.start();
}
}.start();
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
});
}
我的问题是,当位置以小数点变化时,它会收听速度,如果它高于极限速度则会发送短信。它今天通过发送数百个短信来干掉我的短信限制。我不想向每个纬度和经度变化发送警报。我只想在设备超出限制时发送单个警报。然后设备必须达到20 kmph
以收听下一个警报。来人帮帮我。谢谢。