hi , help.
please trying use alarmmanager start , stop activities during week.
now confronted problem not understand.
for each day of week create 2 pendingintents:
- one starting activity
- one killing activity
you can see code below.
now problem that:
- second pendingintent fires off
(and starts activity, thereticaally should stop since first pendingintent never goes off second pendingintent starts activity instead of closing it).
- apparently first pendingintent never goes off !!!!
/ intent should start activity intent sthu = new intent(ctxt, videoactivty.class); pendingintent psthu = pendingintent.getactivity(ctxt, 0, sthu, 0); calendar calset7 = calendar.getinstance(); calset7.set(calendar.month, c.get(calendar.month)); calset7.set(calendar.year, c.get(calendar.year)); calset7.set(calendar.day_of_week, 5); calset7.set(calendar.hour_of_day, hsthu); calset7.set(calendar.minute, msthu); calset7.set(calendar.second, 0); calset7.set(calendar.millisecond, 0); mgr.setrepeating(alarmmanager.rtc_wakeup, calset7.gettimeinmillis(), 7 * 24 * 60 * 60 * 1000, psthu); // intent should kill activity intent fthu = new intent(ctxt, videoactivty.class); fthu.putextra("finish", true); pendingintent pfthu = pendingintent.getactivity(ctxt, 0, fthu, 0); calendar calset8 = calendar.getinstance(); calset8.set(calendar.month, c.get(calendar.month)); calset8.set(calendar.year, c.get(calendar.year)); calset8.set(calendar.day_of_week, 5); calset8.set(calendar.hour_of_day, hfthu); calset8.set(calendar.minute, mfthu); calset8.set(calendar.second, 0); calset8.set(calendar.millisecond, 0); mgr.setrepeating(alarmmanager.rtc_wakeup, calset8.gettimeinmillis(), 7 * 24 * 60 * 60 * 1000, pfthu);
after suggested change:
apparently 1st pendingintent goes off:
intent sthu = new intent(ctxt, videoactivty.class); sthu.setflags(intent.flag_activity_single_top); pendingintent psthu = pendingintent.getactivity(ctxt, 1, sthu, 0); calendar calset7 = calendar.getinstance(); calset7.set(calendar.month, c.get(calendar.month)); calset7.set(calendar.year, c.get(calendar.year)); calset7.set(calendar.day_of_week, 5); calset7.set(calendar.hour_of_day, hsthu); calset7.set(calendar.minute, msthu); calset7.set(calendar.second, 0); calset7.set(calendar.millisecond, 0); //calset.settimezone(timezone.gettimezone("utc")); mgr.setrepeating(alarmmanager.rtc_wakeup, calset7.gettimeinmillis(), 7 * 24 * 60 * 60 * 1000, psthu); log.e("","setto alarm per giovedì"); // intent intent fthu = new intent(ctxt, videoactivty.class); fthu.putextra("finish", true); pendingintent pfthu = pendingintent.getactivity(ctxt, 2, fthu, 0); calendar calset8 = calendar.getinstance(); calset8.set(calendar.month, c.get(calendar.month)); calset8.set(calendar.year, c.get(calendar.year)); calset8.set(calendar.day_of_week, 5); calset8.set(calendar.hour_of_day, hfthu); calset8.set(calendar.minute, mfthu); calset8.set(calendar.second, 0); calset8.set(calendar.millisecond, 0); mgr.setrepeating(alarmmanager.rtc_wakeup, calset8.gettimeinmillis(), 7 * 24 * 60 * 60 * 1000, pfthu);
the purpose of 2nd pendingintent is
to tell activity (started 1st pendingintent)
to finish itself.
(
to sum up:
1st pendingintent start activity,
2nd pendingintent thell activity finish
)
here code of activity:
public class videoactivty extends activity { protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); videoview videoholder = new videoview(this); uri video = uri.parse("android.resource://" + getpackagename() + "/" + r.raw.ingress); videoholder.setvideouri(video); setcontentview(videoholder); videoholder.start(); @override protected void onnewintent (intent i){ //here try tell activity (via second intent) shut down if( i.getbooleanextra("finish",false) ){ finish(); } }
}
change request code of pendingintent (of one). should not same.
like
pendingintent psthu = pendingintent.getactivity(ctxt, 1 /*changed here*/, sthu, 0); pendingintent pfthu = pendingintent.getactivity(ctxt, 2 /*changed here*/, fthu, 0);
add intent.flag_activity_new_task
both intents passing each pendingintents.
note activity started outside of context of existing activity, must use intent.flag_activity_new_task launch flag in intent.
read more getactivity
what working me is..
register alarm launch activity
alarmmanager mgr = (alarmmanager)(getsystemservice( context.alarm_service )); intent sthu = new intent(getapplicationcontext(), videoactivty.class); sthu.setflags(intent.flag_activity_single_top); sthu.addflags(intent.flag_activity_new_task); pendingintent psthu = pendingintent.getactivity(getapplicationcontext(), 1, sthu, pendingintent.flag_update_current); calendar startwhen = new gregoriancalendar(); startwhen.add(calendar.millisecond, 1000); //calset.settimezone(timezone.gettimezone("utc")); mgr.setrepeating(alarmmanager.rtc_wakeup, startwhen.gettimeinmillis(), 60 * 1000, psthu); log.e("","setto alarm per giovedì");
and call below method @ oncreate
of videoactivty
private void registeractivityclosealarm() { alarmmanager mgr = (alarmmanager)(getsystemservice(context.alarm_service )); intent fthu = new intent(getapplicationcontext(), videoactivty.class); fthu.putextra("finish", true); fthu.setflags(intent.flag_activity_single_top); fthu.addflags(intent.flag_activity_new_task); pendingintent pfthu = pendingintent.getactivity(getapplicationcontext(), 2, fthu, pendingintent.flag_update_current); calendar endwhen = new gregoriancalendar(); endwhen.add(calendar.millisecond, 2000); mgr.set(alarmmanager.rtc_wakeup, endwhen.gettimeinmillis(), pfthu); }
this work
Comments
Post a Comment