android - method call from activity to service -


i developing app fires notifications in background. when device on sleep. have used alarm manager , service class. here code- these in activity class.

public void start(){  schedulenotification(getnotification(w.getmean()),time); } 

private void schedulenotification(notification notification, int delay) {

    alarmmanager alarmmanager = (alarmmanager)getsystemservice(getbasecontext().alarm_service);     int d=delay*1000;     intent notificationintent = new intent(getapplicationcontext(), notificationpublisher.class);     notificationintent.putextra(notificationpublisher.notification_id, 1);     notificationintent.putextra(notificationpublisher.notification, notification);      pendingintent pendingintent = pendingintent.getbroadcast(getapplicationcontext(), 0, notificationintent, pendingintent.flag_update_current);             long futureinmillis = systemclock.elapsedrealtime() + d;     alarmmanager.set(alarmmanager.elapsed_realtime_wakeup, futureinmillis, pendingintent);   }  private notification getnotification(string content) {     intent intent=new intent(getapplicationcontext(),meaningactivity.class);      id++;}     pendingintent pi=pendingintent.getactivity(this, 10, intent,pendingintent.flag_cancel_current);      notification.builder builder = new notification.builder(this);     if(id<15){      builder.setcontentintent(pi);     builder.setautocancel(true);     notiid++;     }     return builder.build();  } 

the service called intent on button click

case r.id.button1: {     string t=ed.gettext().tostring();     int time = integer.parseint(t);      savepreferences("switch", s.ischecked());     if (s.ischecked())     {         savepreferences("time", ed.gettext().tostring());         intent intent = new intent(this,notifyservice.class);         //intent.putextra("time",time);         startservice(intent);      }     else     {         stopservice(new intent(this,notifyservice.class));      } 

this notification publisher.java-

public class notificationpublisher extends broadcastreceiver {  public static string notification_id = "notification-id"; public static string notification = "notification"; list<word> queslist; int id=0,count =0; word w; public void onreceive(context context, intent intent) {      notificationmanager notificationmanager = (notificationmanager)context.getsystemservice(context.notification_service);      notification notification = intent.getparcelableextra(notification);   notification.defaults=notification.default_lights|notification.default_sound|notification.default_vibrate;     int id = intent.getintextra(notification_id, 0);     notificationmanager.notify(id, notification);  } 

the service class is-

@override public ibinder onbind(intent arg0) {     //time = arg0.getintextra("time",0);     return null; }  @override public void oncreate() {     super.oncreate();     toast.maketext(this,"service created", toast.length_long).show(); } @override public int onstartcommand(intent intent, int flags, int startid) {     //start();     // restart service if got killed     sa.start();     toast.maketext(this,"service started", toast.length_long).show();      return start_not_sticky; }  @override public void ondestroy() {     super.ondestroy();     toast.maketext(this,"service destroyed", toast.length_long).show(); } 

the above code shows error stating service cannot called intent!! please me how can fire notifications in background @ specified time.. in advance..

we have similar cases mine works. i'll post here code , adapt yours. doing wrong doing service "job" in broadcastreceiver , broadreceiver "job" inside service

mainactivity inside oncreate

intent myintent = new intent(mainactivity.this, broadreceiver.class);     pendingintent pendingintent = pendingintent.getbroadcast(mainactivity.this, 0, myintent, 0);      alarmmanager alarmmanager = (alarmmanager) getsystemservice(alarm_service);     /*alarmmanager.set(alarmmanager.rtc, calendar.gettimeinmillis(), pendingintent);*/     alarmmanager.setrepeating(alarmmanager.elapsed_realtime_wakeup,             calendar.gettimeinmillis(),             alarmmanager.interval_half_day, pendingintent); 

broadcastreceiver

public void onreceive(context context, intent intent) {     intent service1 = new intent(context, notifyservice.class);     context.startservice(service1); 

service

public int onstartcommand(intent intent, int flags, int startid) {     notificationmanager notificationmanager = (notificationmanager) this.getapplicationcontext().getsystemservice(context.notification_service);     intent intent1 = new intent(this.getapplicationcontext(),mainactivity.class);     notification notification = new notificationcompat.builder(this)             .setticker(gettext(r.string.notification_ticker))             .setsmallicon(r.drawable.green)             .setcontenttitle(frase())             .setcontenttext(gettext(r.string.notification_text))             .setautocancel(true)             .build();     intent1.addflags(intent.flag_activity_single_top| intent.flag_activity_clear_top);     notificationmanager.notify(0, notification);     return start_sticky; } 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -