-2

我使用 alarmManager 来控制 Locationmanager 以始终检查距离。我总是使用 setRepeating 警报 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);                     
        }
}
4

1 回答 1

0

通过调用AlarmManager.cancel(PendingIntent 操作)

于 2013-01-21T11:19:19.603 回答