android - Using intents for methods -
edit: updated code:
the notificationsetter method:
public void notificationsetter(){ intent myintent = new intent(this, myreceiver.class); alarmmanager alarmmanager = (alarmmanager)getsystemservice(alarm_service); pendingintent pendingintent = pendingintent.getbroadcast(this, 0, myintent, 0); log.v("log","notificationsetter"); calendar calendar = calendar.getinstance(); calendar.set(calendar.hour_of_day, 21); calendar.set(calendar.minute, 55); calendar.set(calendar.second, 00); alarmmanager.setrepeating(alarmmanager.rtc_wakeup, calendar.gettimeinmillis(), 24*60*60*1000 , pendingintent); }
the myreceiver class:
public class myreceiver extends broadcastreceiver { private static final string tag = "myactivity"; @override public void onreceive(context context, intent intent) { string = "log recieved"; log.v(tag, "index=" + i); frontpage.displaynotificationactivity(context); } }
and finally, displaynotificationactivity method:
public static void displaynotificationactivity(context context){ int notificationidone = 111; notificationcompat.builder mbuilder = new notificationcompat.builder(context); mbuilder.setcontenttitle("leibniz vertretungsplan"); mbuilder.setcontenttext("schau dir den vertretungsplan an!"); mbuilder.setticker("leibniz vertretungsplan"); sharedpreferences pref= preferencemanager.getdefaultsharedpreferences(context); string text = pref.getstring("notifications_new_message_ringtone",""); mbuilder.setsound(uri.parse(text)); mbuilder.setautocancel(true); sharedpreferences pref2= preferencemanager.getdefaultsharedpreferences(context); boolean vibrate = pref2.getboolean("notifications_new_message_vibrate",false); if(vibrate){ mbuilder.setvibrate(new long[] {1000, 1000}); } mbuilder.setlights(0xffff0000,5000,5000); mbuilder.setsmallicon(r.mipmap.ic_launcher); //mbuilder.setnumber(++nummessagesone); intent resultintent = new intent(context, vertretungsplanheuteactvity.class); taskstackbuilder stackbuilder = taskstackbuilder.create(context); stackbuilder.addparentstack(frontpage.class); stackbuilder.addnextintent(resultintent); pendingintent resultpendingintent = stackbuilder.getpendingintent( 0, pendingintent.flag_one_shot //can used once ); mbuilder.setcontentintent(resultpendingintent); notificationmanager mynotificationmanager = (notificationmanager) context.getsystemservice(context.notification_service); mynotificationmanager.notify(notificationidone, mbuilder.build()); }
problem: notification not being displayed @ time set in calendar. logcat says notificationsetter used, onreceive method wasn't. start notifcationmethod button press, change in future.
create broadcastreceiver
class this:
public class myreceiver extends broadcastreceiver { @override public void onreceive(context context, intent intent) { mainactivity.displaynotification(context); } }
add manifest:
<receiver android:name=".myreceiver"/>
create pendingintent
alarm this:
intent myintent = new intent(this, myreceiver.class); pendingintent pendingintent = pendingintent.getbroadcast(this, 0, myintent, 0);
change displaynotification()
method takes context
parameter , make static
(so can called myreceiver
). in displaynotification()
replace uses of this
context
. you'll need use context
reference call methods getapplicationcontext()
, getsystemservice()
.
should work ;-)
Comments
Post a Comment