service - Android How to stop AlarmManager in other Activity -
i using service called in activity 'a' repeatedly created alarmmanager. service checking repeatedly response server, when response true new activity b started. when activity b started want stop service alarmmanager. how can this??
my 'a' activity code calling service following
calendar cal = calendar.getinstance(); cal.add(calendar.second, 40); intent intent = new intent(this, response.class); // add extras bundle intent.putextra("foo", "bar"); // start service // startservice(intent); pintent = pendingintent.getservice(this, 0, intent, 0); alarm = (alarmmanager) getsystemservice(context.alarm_service); int i; = 15; alarm.setrepeating(alarmmanager.rtc_wakeup, cal.gettimeinmillis(), * 1000, pintent); startservice(intent);
i have tried code snippet in activity 'b' stop alarmmanager fail
intent sintent = new intent(this, response.class); pintent = pendingintent.getservice(this, 0, sintent, 0); alarm = (alarmmanager) getsystemservice(context.alarm_service); stopservice(sintent); alarm.cancel(pintent);
in activity b should create sintent same context created in activity a.
so in activity add this
private static context context; public static context getappcontext(){ return activitya.context; }
and inside oncreate() method of activity a, initialize context
context=getapplicationcontext();
and in activity b create sintent this:
intent intent = new intent(activitya.getappcontext(), serviceclass.class); intent.putextra("fsasf", "bar"); pendingintent pintent = pendingintent.getservice(this, 0, intent, 0); alarmmanager alarm = (alarmmanager) getsystemservice(context.alarm_service); stopservice(intent); alarm.cancel(pintent);
Comments
Post a Comment