android - How to stop an intent activity from a service -


i have started intent activity service. now, how can stop intent activity service itself?

public class mainactivity extends activity {      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);     }      public void startservice (view view) {         intent intent = new intent(this,mainservice.class);         startservice(intent);     }      public void stopservice (view view) {         intent intent = new intent(this,mainservice.class);         stopservice(intent);      } } 

startservice, stopservice have buttons associated.

public class mainservice extends service {      @override     public void oncreate() {         super.oncreate();      }      @override     public int onstartcommand(intent intent, int flags, int startid) {         toast.maketext(this, "service started",toast.length_long).show();         intent dialogintent = new intent(this, calledactivity.class);         dialogintent.addflags(intent.flag_activity_new_task);         startactivity(dialogintent);          return start_sticky;         //return super.onstartcommand(intent,flags,startid);     }      @override     public void ondestroy() {         //super.ondestroy();         toast.maketext(this, "service stopped",toast.length_long).show();      }      @nullable     @override     public ibinder onbind(intent intent) {         //intent dialogintent = new intent(this, mainactivity.class);         //dialogintent.addflags(intent.flag_activity_new_task);         //startactivity(dialogintent);         return null;     }  }   public class calledactivity extends activity {     @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);         toast.maketext(this, "i here", toast.length_long).show();         displaynumber();     }      @override     protected void onstop() {         super.onstop();         toast.maketext(this, "i stopping", toast.length_long).show();         finish();     }      @override     protected void ondestroy () {         super.ondestroy();         toast.maketext(this, "i being destroyed", toast.length_long).show();         finish();     }      public void displaynumber () {         textview tv = (textview)findviewbyid(r.id.textview);         integer counter = 10;         while (counter > 0) {             //tv.settext("here go" + string.valueof(counter));             tv.settext("here go" + counter);             counter = counter - 1 ;             toast.maketext(this, "i there "+counter, toast.length_long).show();          }     }   } 

when service invokes intent activity, think intent activity on foreground. now, whenever press stop service button, application crashes. believe stopservice associated service class not in intent activity. there way stop intent activity service?

service works on ui thread. try using intentservice. runs on background thread.


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 -