android - stop START_STICKY service -


i want bulid app run in background when press "start service" , continue in background if app closed or killed. use start_sticky , works, problem when want kill service-the app closed(like want) , error(unfortunately process service has stopped). , service try run again.

how stop service?

p.s-i searched solution on web without success.

my code

mainactivty

public class mainactivity extends appcompatactivity {     private textview textview;     private  intent i;     private static button killserbut;     private static final string tag="com.example.elicahi.service";      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);         textview=(textview) findviewbyid(r.id.textview1);         killserbut=(button)findviewbyid(r.id.kill);         killserbut.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view v) {                 log.d(tag,"onclick");                 textview.settext("onclick");                 killserv();             }         });         //start service        i= new intent(this, myservice.class);         startservice(i);      }        public void changetext(string msg)     {          textview.settext(msg);      }     public void killserv()     {         finish();         log.d(tag,"onstop");          myservice myservice=new myservice();         myservice.stopservice(i);     } } 

myservice

    public class myservice extends service {          private static final string tag="com.example.elicahi.service";         private intent intent;          public myservice( ) {           }           @override         public int onstartcommand(intent intent, int flags, int startid) {             log.d(tag, "onstartcommand method called");             runnable r= new runnable() {                 @override                 public void run() {                     (int i=0; i<10;i++){                         long waitfor= system.currenttimemillis()+1000;                         while (system.currenttimemillis()<waitfor){                             synchronized (this){                                 try{                                     wait(waitfor-system.currenttimemillis());                                     log.d(tag, "the service doing something");                                  }catch (exception e){}                             }                         }                      }                 }             };             //start thread inside run nethod             thread elichaithread = new thread(r);             elichaithread.start();             //if service destroy- restart             return start_sticky;           }             @override         public void ondestroy() {             log.d(tag,"ondestroy method called");             stopself();         }             /* public void killserv(intent intget)         {             log.d(tag,"killed");         intent=intget;         }*/          @override         public ibinder onbind(intent intent) {             // todo: return communication channel service.             return null;         }  } 

and thats manifest

  <service                 android:name=".myservice"                 android:enabled="true"                 android:exported="true" >             </service> 

logcat

07-06 13:07:11.197 1230-1299/com.example.elicahi.service d/com.example.elicahi.service: service doing 07-06 13:07:12.197 1230-1299/com.example.elicahi.service d/com.example.elicahi.service: service doing 07-06 13:07:13.198 1230-1299/com.example.elicahi.service d/com.example.elicahi.service: service doing 07-06 13:07:13.720 1230-1230/com.example.elicahi.service d/com.example.elicahi.service: onclick 07-06 13:07:13.737 1230-1230/com.example.elicahi.service d/com.example.elicahi.service: onstop 07-06 13:07:14.198 1230-1299/com.example.elicahi.service d/com.example.elicahi.service: service doing 07-06 13:07:15.198 1230-1299/com.example.elicahi.service d/com.example.elicahi.service: service doing 07-06 13:07:16.946 1557-1557/com.example.elicahi.service d/com.example.elicahi.service: onstartcommand method called 07-06 13:07:17.954 1557-1589/com.example.elicahi.service d/com.example.elicahi.service: service doing 07-06 13:07:18.990 1557-1589/com.example.elicahi.service d/com.example.elicahi.service: service doing 07-06 13:07:20.031 1557-1589/com.example.elicahi.service d/com.example.elicahi.service: service doing 07-06 13:07:21.071 1557-1589/com.example.elicahi.service d/com.example.elicahi.service: service doing 07-06 13:07:22.111 1557-1589/com.example.elicahi.service d/com.example.elicahi.service: service doing 07-06 13:07:23.149 1557-1589/com.example.elicahi.service d/com.example.elicahi.service: service doing 07-06 13:07:24.189 1557-1589/com.example.elicahi.service d/com.example.elicahi.service: service doing 07-06 13:07:25.207 1557-1589/com.example.elicahi.service d/com.example.elicahi.service: service doing 07-06 13:07:26.208 1557-1589/com.example.elicahi.service d/com.example.elicahi.service: service doing 07-06 13:07:27.223 1557-1589/com.example.elicahi.service d/com.example.elicahi.service: service doing 

use attribute in manifest file under service tag

android:stopwithtask="true" 

Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -