如何在Android 2小时后取消重复闹钟?

时间:2014-06-09 08:55:59

标签: android location alarmmanager background-service

我想要一个后台服务,每隔20分钟提供一次位置更新,并在2小时后自动停止。 我能够创建15分钟后开始服务的闹钟,它的工作正常,但我无法在2小时后自动停止服务或取消闹钟管理器。 我可以随时点击按钮停止服务,但我希望在2小时后后台服务自动停止。

唯一的问题是如何在2小时后自动停止后台服务。

是否有任何建议或解决方案.........

我用来启动警报的代码

ToggleButton toggle = (ToggleButton) findViewById(R.id.togglebtn);
    toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            if (isChecked) {
                if (selectedupdate != null) {
                    System.out.println(selectedupdate);
                    if (selectedupdate.equals("20 Minutes")) {
                        updateInterval = (1 * (60000));
                    } else if (selectedupdate.equals("30 Minutes")) {
                        updateInterval = (2 * (60000));
                    } else if (selectedupdate.equals("60 Minutes")) {
                        updateInterval = (60 * (60000));
                    } else if (selectedupdate.equals("90 Minutes")) {
                        updateInterval = (90 * (60000));
                    } else if (selectedupdate.equals("120 Minutes")) {
                        updateInterval = (120 * (60000));
                    }

                    viewgps.setBackgroundColor(Color.parseColor("#008000"));
                    Toast.makeText(getApplicationContext(),
                            "Location Updates Activate", Toast.LENGTH_SHORT)
                            .show();
                    Calendar calendar = Calendar.getInstance();
                    Intent myIntent = new Intent(PersonalGpsScreen.this,
                            MyBroadcastReceiver.class);
                    pendingIntent = PendingIntent.getBroadcast(
                            PersonalGpsScreen.this, 0, myIntent, 0);


                    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
                    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
                            calendar.getTimeInMillis(), updateInterval,
                            pendingIntent);
                } else if (selectdeactivate != null) {
                    System.out.println(selectdeactivate);
                    if (selectdeactivate.equals("6 Hours")) {
                        deactivateinterval = (3 * (60000));
                    } else if (selectdeactivate.equals("12 Hours")) {
                        deactivateinterval = (12 * (60000) * (60));
                    } else if (selectdeactivate.equals("24 Hours")) {
                        deactivateinterval = (24 * (60000) * (60));
                    } else if (selectdeactivate.equals("48 Hours")) {
                        deactivateinterval = (48 * (60000) * (60));
                    }

                    cancelAlarm();



                }

            } else {
                viewgps.setBackgroundColor(Color.parseColor("#ff0000"));
                Toast.makeText(getApplicationContext(),
                        "Location update deactivated", Toast.LENGTH_SHORT)
                        .show();
                Intent myIntent = new Intent(PersonalGpsScreen.this,
                        MyBroadcastReceiver.class);
                PendingIntent sender = PendingIntent.getBroadcast(
                        PersonalGpsScreen.this, 0, myIntent, 0);
                AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

                alarmManager.cancel(sender);
                // stopService(new Intent(getApplicationContext(),
                // MyAlarmService.class));

                Intent stopServiceIntent = new Intent(getBaseContext(),
                        LocationService.class);
                getBaseContext().stopService(stopServiceIntent);

            }
        }
    });

UpdateInterval是用户通过微调器选择的时间,用于测试我提到20分钟= 1分钟,而deactivateinterval是后台服务停止的时间,它再次由用户选择。

1 个答案:

答案 0 :(得分:0)

PendingIntent.FLAG_Cancel_CURRENT ==>将此标志设置为待定意图==> *它必须具有用于设置闹钟的相同ID ==> alarmManager.cancel(pendingUpdateIntent);

void cancelAlarm()
{
Intent myIntent = new Intent(PersonalGpsScreen.this, MyBroadcastReceiver.class);
//The params for below (Context,ID,Intent,Flag)-- This ID is what needs to be same for //seting and removing the alarm. You can keep it static 0 as you have used, if you do not //have multiple alarms in your app.
pendingIntent = PendingIntent.getBroadcast(PersonalGpsScreen.this, 0, myIntent, PendingIntent.FLAG_Cancel_CURRENT);
AlarmManager  alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
}