android - Service Stop from mainActivity -
i have service calls alarm manager in period time , want stop service. cancel alarm manager service still running in background.
this code in main activity
start.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { if (!cd.isconnectingtointernet()) { showdialog(nointernetdialog); } else if (start.gettext().tostring().equalsignorecase(getresources().getstring(r.string.start_interval))) { countlimit.setenabled(false); timelimit.setenabled(false); start.settext(r.string.stop_interval); start.refreshdrawablestate(); // save send configuration savesendconfiguration(); // show sms send xmlparser read = new xmlparser(); string numberofunsend = read.getxmlfromurl(baseurl + smscount); status.settext(numberofunsend); intent downloader = new intent(context, alarmreceiver.class); downloader.putextra("time", timelimit.getselecteditem() .tostring()); downloader.putextra("count", countlimit.getselecteditem() .tostring()); downloader.addflags(intent.flag_activity_new_task); pendingintent = pendingintent.getbroadcast(context, 0, downloader, pendingintent.flag_cancel_current); int delay = integer.parseint(countlimit.getselecteditem() .tostring()); alarmmanager.setrepeating(alarmmanager.rtc_wakeup, 10 * 1000, delay * 60000, pendingintent); log.i(tag, "call main activity"); } else { alarmmanager.cancel(pendingintent); start.settext(r.string.start_interval); start.refreshdrawablestate(); countlimit.setenabled(true); timelimit.setenabled(true); log.i(tag, "alarm cancel"); } } });
this alarm receiver code
public class alarmreceiver extends broadcastreceiver { private static string tag = "saeed"; @override public void onreceive(context context, intent intent) { intent update = new intent(context, intervalservice.class); string time = intent.getstringextra("time"); string count = intent.getstringextra("count"); update.putextra("time", time); update.putextra("count", count); log.i(tag, "call service alaram receiver," + time + "," + count); context.startservice(update); } }
and service code
public class intervalservice extends intentservice { private static final string tag = "saeed"; private string baseurl; private databasehandler db; private int time; private int count; private sharedpreferences pref; connectiondetector cd; public intervalservice() { super("auto interval"); } @override protected void onhandleintent(intent intent) { log.i(tag, "interval service implementation run"); preferencemanager.setdefaultvalues(this, r.xml.settings, false); pref = preferencemanager.getdefaultsharedpreferences(this); baseurl = pref.getstring("baseurl",""); db = new databasehandler(this); cd = new connectiondetector(getapplicationcontext()); time = integer.parseint(intent.getstringextra("time")); count = integer.parseint(intent.getstringextra("count")); new interval().execute(); //this async task class }
how can stop service main activity?
you can use :
intent update = new intent(context, intervalservice.class); context.stopservice(update);
Comments
Post a Comment