android - Service gets stops after closing an Activity in which it is created? -
i have created service
, started using activity
on button
click, service
gets stops after closes activity
. should keep running service
without gets stopped?
mainactivity.java
public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); } public void startservice(view v){ intent = new intent(this, notstickyservice.class); startservice(i); } public void stopservice(view v){ intent = new intent(this, notstickyservice.class); stopservice(i); }
service.java
public class notstickyservice extends service{ mediaplayer mp; @override public void oncreate() { showtoast("oncreate"); super.oncreate(); } @override public int onstartcommand(intent intent, int flags, int startid) { showtoast("onstart"); mp = new mediaplayer(); assetfiledescriptor descriptor; descriptor = getassets().openfd("msg.mp3"); mp.setdatasource(descriptor.getfiledescriptor(), descriptor.getstartoffset(),descriptor.getlength()); descriptor.close(); mp.prepare(); mp.start(); return service.start_sticky; } @override public void ondestroy() { super.ondestroy(); showtoast("ondestroy"); } private void showtoast(string message){ toast toast = toast.maketext(this, message, toast.length_short); toast.show(); } }
Comments
Post a Comment