我使用alarmManager来控制Locationmanager以始终检查距离。 我使用setRepeating闹钟总是10秒(用于检查)
我想在用户外出区域时第一次提醒应用程序(距离> 100米) 但它每隔10秒就会发出警报(用户熄灭)。
第一次提醒时如何取消?
非常感谢你。
活动
trackchk.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(trackchk.isChecked())
{
Intent intrack = new Intent(hatebug.this,trackservice.class);
intrack.putExtra("LT", lattointent);
intrack.putExtra("LN", lontointent);
Calendar calendartrack = Calendar.getInstance();
calendartrack.setTimeInMillis(System.currentTimeMillis());
calendartrack.add(Calendar.SECOND, 3);
getPreferences(MODE_PRIVATE).edit().putBoolean("CHECKTRACK", true).commit();
tracklocal = PendingIntent.getService(hatebug.this, 0, intrack, PendingIntent.FLAG_UPDATE_CURRENT);
alarmtrack = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmtrack.setRepeating(AlarmManager.RTC_WAKEUP, calendartrack.getTimeInMillis(), 6000, tracklocal);
}
else {
getPreferences(MODE_PRIVATE).edit().putBoolean("CHECKTRACK", false).commit();
alarmtrack = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmtrack.cancel(tracklocal);
}
}
});
服务(onStart)
public void onStart(Intent intent,int startId){
// TODO Auto-generated method stub
float latserv = intent.getFloatExtra("LT", 0);
float lonserv =intent.getFloatExtra("LN", 0);
String slat = String.valueOf(latserv);
String slon = String.valueOf(lonserv);
LocationManager locate = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
Location lm = locate.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
float lat = (float) lm.getLatitude();
float lon = (float) lm.getLongitude();
float dlat = (float) Math.toRadians(lat - latserv);
float dlon = (float) Math.toRadians(lon - lonserv);
float a1 = (float) ((Math.sin(dlat/2))*(Math.sin(dlat/2)));
float a2 = (float) ((Math.cos(Math.toRadians(lon)))*(Math.cos(Math.toRadians(100.546652)))*(Math.sin(dlon/2))*(Math.sin(dlon/2)));
float a = a1+a2;
float c = (float) (2*Math.atan2(Math.sqrt(a), Math.sqrt(1-a)));
float d = 6371000*c;
String lool = String.format("Latitude\t\t: %1$s\nLongitude\t: %2$s\nDistance\t\t: %3$f",slat,slon,d);
Toast.makeText(getApplicationContext(), lool, Toast.LENGTH_SHORT).show();
if(d>100)
{
Vibrator vibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(1000);
}
}