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 static k not become instance dependent , can read value calling

    exampleintentservice.getint();

  • send broadcast, can received broadcast receiver,every time change k think work moment send 10 broadcast in second 100 seconds


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -