I have an Intent service running that performs a do while loop.
The service is started from an activity when a button is pressed.
Problem is when I press the button again a new service is started along side the previous service from the first press.
I want the previous service to be stopped and the new service to replace it.
Essentially i want to know how to either kill services or pass data to the running thread in order to change the do while conditions. Im not sure how to handle this.
Thanks for your help.
startAlarm.setOnClickListener(new View.OnClickListener() {
int x=0;
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
getActivity().startService(alarmService);
System.out.println("Service has started.");
}
});
public class AlarmService extends IntentService{
public AlarmService() {
super("Alarm run service");
// TODO Auto-generated constructor stu
}
@Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub
int hour = intent.getExtras().getInt("hourval");
int minute = intent.getExtras().getInt("minval");
System.out.println("Hour is : " + hour + " minute is : " + minute);
int currentHour;
int currentMinute;
do{
System.out.println("in the loop");
Calendar cal = Calendar.getInstance();
currentHour = cal.get(Calendar.HOUR_OF_DAY);
currentMinute = cal.get(Calendar.MINUTE);
System.out.println(currentHour + ":" + currentMinute + "and " + hour +":" +minute);
}while(currentHour != hour || currentMinute != minute);
System.out.println("Left loop");
Intent alarmRingStart = new Intent(getApplicationContext(),AlarmRing.class);
alarmRingStart.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(alarmRingStart);
System.out.println("Alarm has gone off");
}
@Override
public void onDestroy(){
super.onDestroy();
}
}