java - Android service is not starting -
i need start background service on click of android app icon. below activity oncreate() method.
protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_executable_runner); startservice(new intent(this, exerunnerservice.class)); }
and overridden service class rest default.
public class exerunnerservice extends service{ public int onstartcommand(intent intent, int flags, int startid) { return start_sticky; } @override public void oncreate() { thread th = new thread(new runnable() { public void run() { log.d(tag, "service running"); } }); th.start(); } }
i don't have initialization code same. when start application not getting service logs.
i put , override on onstartcommand, , call super.oncreate() in overridden oncreate method;
like this:
public class myservice extends service { private static boolean isrunning = false; private context context; @override public void oncreate() { super.oncreate(); context = this; } public static boolean isservicerunning() { return isrunning; } @override public int onstartcommand(intent intent, int flags, int startid) { if(!isrunning) { isrunning = true; } return start_sticky; }
iv added isrunning flag because have experienced service running , initializing objects again not idea.
Comments
Post a Comment