android - Pending Intent Fires Immediately -
i trying make app allows user enter 2 times using 2 timepickers , phone set silent between these times. using pendingintent , alarmmanager trigger broadcastreceiver @ times user chooses. @ moment, setting phone on , off silent when user clicks save button. "alarm scheduled" toast appears , "broadcast received" appears twice doesn't after that.
here main activity code:
public class mainactivity extends appcompatactivity { alarmmanager alarm; timepicker timepickerstart; timepicker timepickerend; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); timepickerstart = (timepicker)findviewbyid(r.id.timepickerstart); timepickerstart.setis24hourview(true); timepickerend = (timepicker)findviewbyid(r.id.timepickerend); timepickerend.setis24hourview(true); } //method called when save button clicked public void setalarm(view view) { calendar calendarstart; calendar calendarend; calendarstart = calendar.getinstance(); calendarend = calendar.getinstance(); //set calendars times in both timepickers calendarstart.set(calendar.year, calendar.month, calendar.day_of_month, timepickerstart.getcurrenthour(), timepickerstart.getcurrentminute()); long starttimemillis = calendarstart.gettimeinmillis(); calendarend.set(calendar.year, calendar.month, calendar.day_of_month, timepickerend.getcurrenthour(), timepickerend.getcurrentminute()); long endtimemillis = calendarend.gettimeinmillis(); //create intent , set class triggered intent intent intent = new intent(mainactivity.this, receiver.class); pendingintent pintent = null; pintent = pendingintent.getbroadcast(this, 123, intent, pendingintent.flag_update_current); //create alarm manager alarm = (alarmmanager) getsystemservice(context.alarm_service); //set alarms go off @ time specified timepickers alarm.set(alarmmanager.rtc_wakeup, calendarstart.gettimeinmillis(), pintent); alarm.set(alarmmanager.rtc_wakeup, calendarend.gettimeinmillis(), pintent); //toast feedback toast.maketext(this, "alarm scheduled", toast.length_short).show(); }
here broadcastreceiver:
public class receiver extends broadcastreceiver { audiomanager audiomanager; int modenum; @override public void onreceive(context context, intent intent) { //toast feedback toast.maketext(context, "broadcast received", toast.length_short).show(); audiomanager = (audiomanager)context.getsystemservice(context.audio_service); modenum = audiomanager.getringermode(); if (modenum == 0) { audiomanager.setringermode(2); } else { audiomanager.setringermode(0); } }
}
this manifest:
<application android:allowbackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name=".mainactivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <receiver android:name=".receiver"></receiver> </application>
you have multiple problems.
first, trying create 2 alarms same pendingintent
. not work. second alarm request cancel first one. need distinct pendingintent
objects, means either substantially different intent
objects wrapped pendingintent
objects, or use 2 different values have 123
.
second, set()
calls on calendar
incorrect. example, in both places, first parameter supposed year. passing calendar.year
, 1
, not 2015
.
Comments
Post a Comment