android - Alarm not going off when it should -
i'm working on alarmmanager
; i've created alarmmanager (based on tutorial) can set go off @ specific time, problem here set time , click ok button in timepickerdialog
alarm goes off no matter when should. however, toast shown when it's supposed to.
what missing?
here code:
androidtimeactivity.java
public class androidtimeactivity extends activity { notificationcompat.builder mbuilder; timepicker mytimepicker; button buttonstartsetdialog; textview textalarmprompt; timepickerdialog timepickerdialog; final static int rqs_1 = 1; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); textalarmprompt = (textview)findviewbyid(r.id.alarmprompt); buttonstartsetdialog = (button)findviewbyid(r.id.startsetdialog); buttonstartsetdialog.setonclicklistener(new onclicklistener(){ @override public void onclick(view v) { textalarmprompt.settext(""); opentimepickerdialog(false); } }); } private void opentimepickerdialog(boolean is24r){ calendar calendar = calendar.getinstance(); timepickerdialog = new timepickerdialog( androidtimeactivity.this, ontimesetlistener, calendar.get(calendar.hour_of_day), calendar.get(calendar.minute), is24r); timepickerdialog.settitle("set alarm time"); timepickerdialog.show(); } ontimesetlistener ontimesetlistener = new ontimesetlistener(){ @override public void ontimeset(timepicker view, int hourofday, int minute) { calendar calnow = calendar.getinstance(); calendar calset = (calendar) calnow.clone(); calset.set(calendar.hour_of_day, hourofday); calset.set(calendar.minute, minute); calset.set(calendar.second, 0); calset.set(calendar.millisecond, 0); if(calset.compareto(calnow) <= 0){ calset.add(calendar.date, 1); } setalarm(calset); } }; private void setalarm(calendar targetcal){ intent intent = new intent(getbasecontext(), alarmreceiver.class); pendingintent pendingintent = pendingintent.getbroadcast(getbasecontext(), rqs_1, intent, 0); alarmmanager alarmmanager = (alarmmanager)getsystemservice(context.alarm_service); alarmmanager.set(alarmmanager.rtc_wakeup, targetcal.gettimeinmillis(), pendingintent); mbuilder = new notificationcompat.builder(this); mbuilder.setsmallicon(r.drawable.icon1); mbuilder.setcontenttitle("my notification"); mbuilder.setcontenttext("hello world!"); mbuilder.setcontentintent(pendingintent); mbuilder.setsound(uri.parse("android.resource://com.example.applenett.myapplication/raw/ringtone1")); notificationmanager notificationmanager = (notificationmanager) getsystemservice(notification_service); notificationmanager.notify(0, mbuilder.build()); } }
alarmreceiver.java
public class alarmreceiver extends broadcastreceiver { @override public void onreceive(context context, intent arg1) { toast.maketext(context, "alarm received!", toast.length_long).show(); } }
androidmanifest.xml
<uses-permission android:name="com.android.alarm.permission.set_alarm" /> <application android:allowbackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsrtl="true" android:theme="@style/apptheme" > <activity android:name=".androidtimeactivity" android:label="@string/app_name" android:theme="@style/apptheme.noactionbar" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <receiver android:name=".alarmreceiver" android:label="@string/title_activity_alarm_receiver" android:theme="@style/apptheme.noactionbar" android:process=":remote"> </receiver> </application>
you calling notificationmanager.notify(0, mbuilder.build());
right notification (and sound accompanying it) fire b/c you've asked to. should move whole notification builder (and notify) section alarm receiver class triggered along toast @ time.
Comments
Post a Comment