android - Weekly repeating alarm -
i'm working on small android radio application. app fetching radio stream internet , plays on media player
. works fine, next problem adding alarm in same app. alarm start radio on specific date (day,hour,minute). alarm looks same android official alarm clock. layout is: alarm.xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="#ff4379df"> <timepicker android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/timepicker" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" /> <linearlayout android:weightsum="7" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/timepicker" android:layout_alignparentleft="true" android:layout_alignparentstart="true"> <togglebutton android:texton="p" android:textoff="p" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/chk_monday" /> <togglebutton android:texton="u" android:textoff="u" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/chk_tuesday" /> <togglebutton android:texton="s" android:textoff="s" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/chk_wednesday" /> <togglebutton android:texton="c" android:textoff="c" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/chk_thursday" /> <togglebutton android:texton="p" android:textoff="p" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/chk_friday" /> <togglebutton android:texton="s" android:textoff="s" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/chk_sat" /> <togglebutton android:texton="n" android:textoff="n" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/chk_sunday" /> </linearlayout> </relativelayout>
and alarm.java fragment (tab) updated 2#
public class alarm extends fragment { timepicker timepicker; togglebutton chk_monday; togglebutton chk_tuesday; togglebutton chk_wednesday; togglebutton chk_thursday; togglebutton chk_friday; togglebutton chk_sat; togglebutton chk_sunday; textview textview; final static int rqs_1 = 1; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.alarm, container, false); final togglebutton chk_monday = (togglebutton) rootview.findviewbyid(r.id.chk_monday); togglebutton chk_tuesday = (togglebutton) rootview.findviewbyid(r.id.chk_tuesday); togglebutton chk_wednesday = (togglebutton) rootview.findviewbyid(r.id.chk_wednesday); togglebutton chk_thursday = (togglebutton) rootview.findviewbyid(r.id.chk_thursday); togglebutton chk_friday = (togglebutton) rootview.findviewbyid(r.id.chk_friday); togglebutton chk_sat = (togglebutton) rootview.findviewbyid(r.id.chk_sat); togglebutton chk_sunday = (togglebutton) rootview.findviewbyid(r.id.chk_sunday); timepicker timepicker = (timepicker) rootview.findviewbyid(r.id.timepicker); timepicker.setis24hourview(true); /**/ calendar calset = calendar.getinstance(); int hour = calset.get(calendar.hour_of_day); int min = calset.get(calendar.minute); timepicker.setcurrenthour(hour); timepicker.setcurrentminute(min); /**/ if (chk_monday.ischecked()) { forday(2); } if (chk_tuesday.ischecked()) { forday(3); } if (chk_wednesday.ischecked()) { forday(4); } if (chk_thursday.ischecked()) { forday(5); }if (chk_friday.ischecked()) { forday(6); } if (chk_sat.ischecked()) { forday(7); }if (chk_sunday.ischecked()) { forday(1); } return rootview; } public void forday (int week) { calendar calset = calendar.getinstance(); calset.set(calendar.day_of_week, week); calset.set(calendar.hour_of_day, timepicker.getcurrenthour()); calset.set(calendar.minute, timepicker.getcurrentminute()); calset.set(calendar.second, 0); calset.set(calendar.millisecond, 0); intent intent = new intent(getactivity().getbasecontext(), alarmreceiver.class); pendingintent pendingintent = pendingintent.getbroadcast(getactivity().getapplicationcontext(), rqs_1, intent, 0); alarmmanager alarmmanager = (alarmmanager) getactivity().getsystemservice(context.alarm_service); alarmmanager.setrepeating(alarmmanager.rtc_wakeup, calset.gettimeinmillis(), 1 * 60 * 60 * 1000, pendingintent); } }
now goal make repeat every day,hour,minute when user declares it. plus should stay in textview
day , time when user set it. + using this link guide myself little bit.
update 2# created alarmreceiver.java
public class alarmreceiver extends broadcastreceiver { @override public void onreceive(context context, intent intent) { // todo auto-generated method stub log.e("onreceive", "recived!"); toast.maketext(context, "onrecive!", toast.length_short).show(); } }
added receiver androidmanifest.xml
<receiver android:name=".alarmreceiver"></receiver>
i programmed similar function. solved searching next alarm-time of current-started-alarm , start new intent on alarmmanager
.
so each alarm restart himself in onstartcommand
. alarm-model has method getnextalarmtime()
next timestamp execute.
maybe gives fresh ideas.
Comments
Post a Comment