java - How to stop service in background with toggle button in Android? -
i'm implementing running service in background , service stop , start click on toggle button.service start in 1st activity , stop service in activity when click on toggle button.when run application service automatically start in 1st activity start in oncreate() method in 1st activity , activity toggle button status on status when toggle button going off service stop when 1st activity service again start.please can 1 me.here code
public class myservice extends service { private static final string tag = "myservice"; mediaplayer player; private final string strmyservice="myservice"; @override public ibinder onbind(intent arg0) { // todo auto-generated method stub return null; } @override public void oncreate() { toast.maketext(this, "my service created", toast.length_long).show(); log.d(tag, "oncreate"); player = mediaplayer.create(this, r.raw.braincandy); player.setlooping(false); // set looping } @override public void ondestroy() { toast.maketext(this, "my service stopped", toast.length_long).show(); log.d(tag, "ondestroy"); player.stop(); } @override public void onstart(intent intent, int startid) { toast.maketext(this, "my service started", toast.length_long).show(); log.d(tag, "onstart"); player.start(); } } public class service_demo extends activity implements onclicklistener { private static final string tag = "servicesdemo"; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); system.out.println("in oncreate("); startservice(new intent(this, myservice.class)); } } public class toggle_activity extends activity { togglebutton tgbutton; private boolean isservice=false; private string strservice; public final string service_prefs="serviceprefs"; private static final string strmyservice = "zdf"; public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.toggle); final sharedpreferences serviceprefs=this.getsharedpreferences("service_prefs",mode_world_readable); strservice=serviceprefs.getstring(strmyservice , "myservice"); log.e("",""+strservice); final boolean mbool = serviceprefs.getboolean("myservice", true); log.e("boolean value mbool","="+mbool); boolean b = mbool; log.e("update pref", b.tostring()); tgbutton = (togglebutton)findviewbyid(r.id.togglebutton); tgbutton.setchecked(mbool); final boolean mbool1 = serviceprefs.getboolean("myservice", false); final boolean c = mbool1; log.e("update pref", c.tostring()); tgbutton=(togglebutton)findviewbyid(r.id.togglebutton); tgbutton.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub if(tgbutton.ischecked()) { startservice(new intent(toggle_activity.this , myservice.class)); system.out.println("service started in togglr button"); } else { stopservice(new intent(toggle_activity.this,myservice.class)); system.out.println("service stopped in togglr button"); } } }); } }
in first activity, you'd put startservice(new intent(this, myservice.class)) in onresume oncreate
Comments
Post a Comment