android - Bind service using nested class -


i following tutorial bind service,this code

server

public class server extends service {  ibinder mbinder = new localbinder();  @override public ibinder onbind(intent intent) {     return mbinder; }  public class localbinder extends binder {     public server getserverinstance() {         return server.this;     } }  public string gettime() {     simpledateformat mdateformat = new simpledateformat(             "yyyy-mm-dd hh:mm:ss");     return mdateformat.format(new date());  } } 

client

public class client extends activity {  boolean mbounded; server mserver; textview text; button button;  @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.main);      text = (textview) findviewbyid(r.id.text);     button = (button) findviewbyid(r.id.button);     button.setonclicklistener(new onclicklistener() {          public void onclick(view v) {             text.settext(mserver.gettime());         }     }); }  @override protected void onstart() {     super.onstart();     intent mintent = new intent(this, server.class);     bindservice(mintent, mconnection, bind_auto_create); };  serviceconnection mconnection = new serviceconnection() {      public void onservicedisconnected(componentname name) {         toast.maketext(client.this, "service disconnected", 1000).show();         mbounded = false;         mserver = null;     }      public void onserviceconnected(componentname name, ibinder service) {         toast.maketext(client.this, "service connected", 1000).show();         mbounded = true;         localbinder mlocalbinder = (localbinder) service;         mserver = mlocalbinder.getserverinstance();     } };  @override protected void onstop() {     super.onstop();     if (mbounded) {         unbindservice(mconnection);         mbounded = false;     } }; } 

its working fine.

if change

localbinder mlocalbinder = (localbinder) service;         mserver = mlocalbinder.getserverinstance(); 

to

mserver = new server(); 

it work fine.

so actual need of creating nested localbinder class in server class , create getserverinstance() method getting reference of it, if can directly initialize object using traditional java method new keyword, have seen in other examples.i can't make out it, expert can me?


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -