java - Trying to recieve a string through a broadcast in an activity from a service, not seeing anything -
here's code below. first bit of code activity. put click listener on button in activity starts service. right now, want see can recieve strings service here code trying that. put "hello world" broadcast in oncreate of service test. can spot issue? service code below activity code.
package homebrewchatter.calcs; import android.app.activity; import android.content.broadcastreceiver; import android.content.context; import android.content.intent; import android.content.intentfilter; import android.content.sharedpreferences; import android.os.bundle; import android.util.log; import android.view.view; import android.view.view.onclicklistener; import android.widget.textview; import android.widget.edittext; import android.widget.button; import java.util.list; public class hop_timer extends activity { private textview timerout; private broadcastreceiver onbroadcast = new broadcastreceiver() { @override public void onreceive(context ctxt, intent i) { hop_timer.this.timerout = (textview)hop_timer.this.findviewbyid(r.id.display_time); hop_timer.this.timerout.settext("recieved"); } }; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.hoptimer); button setvars = (button) findviewbyid(r.id.add_alarm_button); setvars.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { //hop_timer.this.timerout = (textview)hop_timer.this.findviewbyid(r.id.display_time); //hop_timer.this.timerout.settext("working far"); startservice(new intent(hop_timer.this, hop_timer_service.class)); } }); } public void onresume() { super.onresume(); registerreceiver(onbroadcast, new intentfilter("mymessage")); } public void onpause() { super.onpause(); unregisterreceiver(onbroadcast); } } package homebrewchatter.calcs; import android.app.service; import android.content.intent; import android.os.handler; import android.os.ibinder; import android.os.systemclock; public class hop_timer_service extends service { private handler mhandler = new handler(); private int length_minutes; private boolean timer_running; private long starttime = 0l; public string currenttime = ""; @override public void oncreate() { length_minutes = 0; timer_running = false; getapplicationcontext().sendbroadcast(new intent("hello world")); } public void settime(int mins) { length_minutes = mins; } public void starttimer() { starttime = system.currenttimemillis(); mhandler.removecallbacks(mupdatetimertask); mhandler.postdelayed(mupdatetimertask, 100); } private runnable mupdatetimertask = new runnable() { public void run() { final long start = starttime; long millis = systemclock.uptimemillis() - start; int seconds = (int) (millis/1000); int minutes = seconds /60; seconds = seconds %60; if(seconds < 10) { currenttime = "" + minutes + ":0" + seconds; } else { currenttime = "" + minutes + ":" + seconds; } getapplicationcontext().sendbroadcast(new intent(currenttime)); mhandler.postattime(this, start + (((minutes * 60) + seconds + 1) * 1000)); } }; @override public ibinder onbind(intent intent) { // todo auto-generated method stub return null; } }
with respect actual question, intent
broadcasting not match intentfilter
using in registerreceiver()
. since intentfilter
says looking "mymessage"
intents
, intent
broadcast needs have "mymessage"
action. right now, 1 of intents
has action based on current time, unlikely received anything. other has action of "hello world"
.
beyond that:
by default, broadcasts broadcast entire device. if intending broadcast entire device, please use action string namespaced app (e.g.,
homebrewchatter.calcs.mymessage
). if not intending broadcast entire device, please usesetpackage()
onintent
broadcast keep within application.since
service
context
, not need usegetapplicationcontext()
inservice
broadcasts.you not appear calling
starttimer()
.it infrequently idea, in production code, have
service
primary job sit , watch time tick by.alarmmanager
more common solution here,service
can in memory when adding actual value user.you start service, never stop it.
Comments
Post a Comment