i learn writing android apps , want make simple application, give user daily notification.
i create app, works when set time of notification few minutes after installing app on phone or don't disconnect phone computer.
for example - yesterday @ 22:30 set alarm on 7:30 today , didn't work, (at 7:40) connect phone computer, install app again, set alarm on 8 , works, set alarm on 21:30 , didn't work again.
any tips or something? think tried know :/
here's code:
public class mainactivity extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); imagebutton notificationbutton = (imagebutton) findviewbyid(r.id.nofication_button); notificationbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { calendar calendar = calendar.getinstance(); calendar.settimeinmillis(system.currenttimemillis()); int curhr = calendar.get(calendar.hour_of_day); if (curhr > 21) { // since current hour on 21, setting date next day calendar.add(calendar.date, 1); } calendar.set(calendar.hour_of_day, 21); calendar.set(calendar.minute, 30); calendar.set(calendar.second, 12); intent intent = new intent(getapplicationcontext(), notificationreceiver.class); pendingintent pendingintent = pendingintent.getbroadcast(getapplicationcontext(), 0, intent, pendingintent.flag_update_current); alarmmanager alarmmanager = (alarmmanager) getsystemservice(alarm_service); alarmmanager.setrepeating(alarmmanager.rtc_wakeup, calendar.gettimeinmillis(), alarmmanager.interval_day, pendingintent); } }); } }
and notificationreceiver class:
public class notificationreceiver extends broadcastreceiver { @override public void onreceive(context context, intent intent) { notificationmanager notificationmanager = (notificationmanager) context.getsystemservice(context.notification_service); intent intent1 = new intent(context, mainactivity.class); intent1.setflags(intent.flag_activity_clear_top); uri alarmsound = ringtonemanager.getdefaulturi(ringtonemanager.type_notification); pendingintent pendingintent = pendingintent.getactivity(context, 0, intent1, pendingintent.flag_update_current); notificationcompat.builder builder = new notificationcompat.builder(context) .setsmallicon(r.drawable.index) .setcontentintent(pendingintent) .setcontenttext("notification text") .setcontenttitle("notification title") .setsound(alarmsound) .setvibrate(new long[]{1000, 1000, 1000, 1000}) .setautocancel(true); notificationmanager.notify(0, builder.build()); } }
Comments
Post a Comment