如何在Android中取消2小时后的重复报警?

如何在Android中取消2小时后的重复报警?,android,location,alarmmanager,background-service,Android,Location,Alarmmanager,Background Service,我想要一个后台服务,每20分钟提供一次位置更新,2小时后自动停止。 我能够创建15分钟后启动服务的警报,其工作正常,但我无法在2小时后自动停止服务或取消警报管理器。 我可以随时点击按钮停止服务,但我希望在2小时后后台服务自动停止 唯一的问题是如何在2小时后自动停止后台服务 有什么建议或解决办法吗 我用来启动警报的代码是 ToggleButton toggle = (ToggleButton) findViewById(R.id.togglebtn); toggle.setOnChecke

我想要一个后台服务,每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是后台服务停止的时间,用户再次选择它。

PendingEvent.FLAG\u Cancel\u CURRENT==>将此标志设置为挂起的意图==>*它必须具有与设置报警==>alarmManager.Cancel(pendingUpdateIntent)相同的ID


您好,先生,谢谢您的回复,我有点困惑,所以我更新了我的问题,并把代码放在如何启动我的报警管理器上…您能给我提供一些示例或示例代码吗…我是android开发的新手…再次感谢Hanks先生,但我再次卡住了,所以我更新了完整的代码。我到底想做什么,请您指导我做错了什么。我有两个旋转器,当按下togglebutton时可以工作,后台服务工作正常,当togglebutton关闭时也会停止,但是它不会根据停用间隔时间自动停止。当我检查logcat时,它不会检查条件选择Deactivate。你能根据更新的问题给我建议吗
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);
}