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.