0

I want a background service which provide location update after every 20 minutes and automatically stop after 2 hours. I am able to create alarm that start service after 15 minutes ,its work fine but i am not able to stop the service or cancel the alarm manager after 2 hours automatically. I can stop the service by clicking on button any time ,but i want that after 2 hours the background service automatically stop.

Only the problem is How to stop background service after 2 hours automatically.

Is there any suggestion or solution.........

THE CODE I USED TO START ALARM IS

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 is the time selected through spinner by user,for testing i mention 20 minutes =1 minute and deactivateinterval is the time at which background service stop,it is again selected by user.

user8910
  • 11
  • 3

1 Answers1

0

PendingIntent.FLAG_Cancel_CURRENT==> Set this flag to a Pending Intent ==> *It must have same ID that you used to set a alarm==>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);
}
Parth Kapoor
  • 1,494
  • 12
  • 23
  • Hi Sir, thanks for your reply,I am little confused so i update my question and put the code how i start my alarm manger ...can u please provide me any example or sample code..am new in android development...Thanks again – user8910 Jun 09 '14 at 09:26
  • Thanks sir,but again i stuck so i update full code what actually am trying to do,can you please guide what i am doing wrong. i have two spinner which works when togglebutton is pressed,the background services work perfectly and also stop when toggle button is off,but its not stop automatically according to the deactivateinterval time.and as i check the logcat its not checking that condtion selectdeactivate.can u please give me suggestion according o updated question.please – user8910 Jun 09 '14 at 10:33