常に距離をチェックするために、alarmManager を使用して Locationmanager を制御します。setRepeating alarm を常に 10 秒 (確認用) に使用します
ユーザーがエリアに出たとき(距離> 100 m)に初めてアラートをアプリに送信したいのですが、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);
}
}