Android: Service value to a BroacastReceiver -
i beginner , read posts service , broadcastreceiver components. still not now, here. have service (class: exampleservice) changes variable k:
@override protected void onhandleintent(intent intent) {
while (k < 1000) { if(forward==true){ k++; }else{ k--; } synchronized (this) { try { wait(100); } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } } } k = 0; this.stopself(); } public long getint(){ return k; }
and broadcastreceiver:
broadcastreceiver broadi=new broadcastreceiver(){ @override public void onreceive(context arg0, intent arg1) { // todo auto-generated method stub exampleintentservice se=new exampleintentservice(); text.settext("count:"+string.valueof(se.getint())); } };
so create object of service , try value 'getint()' method. returns 0 , dont know why. tips?
when call
exampleintentservice se=new exampleintentservice();
you creating new istance of class, not referring running instance of service. think have 2 chances:
make
k
, getter method static. making statick
not become instance dependent , can read value callingexampleintentservice.getint();
send broadcast, can received broadcast receiver,every time change
k
think work moment send 10 broadcast in second 100 seconds
Comments
Post a Comment