android 中的 LocationListener 是否像循环一样工作?
Is LocationListener in android works like loop?
我用LocationListener
来计算移动的移动速度。我用 location.getSpeed()
来获得速度。我的任务是,如果设备速度超过已经设置的速度限制,它会以 ALERT- OVER SPEED
的形式向其他手机号码发送短信。我使用 Flag Variable
boolean flag;
来检查条件。我的问题是,如果手机连续 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);
final LocationManager locationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationListener() {
@Override
public void onLocationChanged(final Location location) {
final double currentSpeed=(location.getSpeed())*3.6;
String currentSpeedString =dFormat.format(currentSpeed);
final double currentSpeedDouble=Double.parseDouble(currentSpeedString);
if ((currentSpeedDouble>=overSpeedDouble)&&(flag==false)){
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);
flag=true;
}
}.start();
}
if (currentSpeedDouble<20){
flag=false;
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
});
http://developer.android.com/reference/android/location/LocationListener.html
只要您的设备改变位置,它就会被调用。您设置了 falg,但在超过速度限制 10 秒后发送消息时更改了它的值。你应该在检测到它后立即更改它。
我用LocationListener
来计算移动的移动速度。我用 location.getSpeed()
来获得速度。我的任务是,如果设备速度超过已经设置的速度限制,它会以 ALERT- OVER SPEED
的形式向其他手机号码发送短信。我使用 Flag Variable
boolean flag;
来检查条件。我的问题是,如果手机连续 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);
final LocationManager locationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationListener() {
@Override
public void onLocationChanged(final Location location) {
final double currentSpeed=(location.getSpeed())*3.6;
String currentSpeedString =dFormat.format(currentSpeed);
final double currentSpeedDouble=Double.parseDouble(currentSpeedString);
if ((currentSpeedDouble>=overSpeedDouble)&&(flag==false)){
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);
flag=true;
}
}.start();
}
if (currentSpeedDouble<20){
flag=false;
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
});
http://developer.android.com/reference/android/location/LocationListener.html
只要您的设备改变位置,它就会被调用。您设置了 falg,但在超过速度限制 10 秒后发送消息时更改了它的值。你应该在检测到它后立即更改它。