i trying create simple service in android.
i checked settings->application tab , can see service running. unfortunately onstartcommand function not getting executed. can't see toast message.
here code:
service class
public class certisservices extends service { gpstracker gps; private double latitude = 0; private double longitude = 0 ; @override public ibinder onbind(intent intent) { return null; } public int onstartcommand(intent intent, int flags, int startid){ get_my_gps_location(); string lati = string.valueof(latitude); string longi = string.valueof(longitude); toast.maketext(this, "services started : ", toast.length_long).show(); send_data(lati, longi); return start_sticky; } public void ondestroy (){ super.ondestroy(); toast.maketext(this, "services stopped", toast.length_long).show(); } private void send_data(string lat, string lng){ httpclient httpclient = new defaulthttpclient(); // url httppost httppost = new httppost("http://172.16.110.3/agent_tracking/index.php/api/rest/get-schedules/"); try { list<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(2); // data namevaluepairs.add(new basicnamevaluepair("latitude", lat)); namevaluepairs.add(new basicnamevaluepair("longitude", lng)); httppost.setentity(new urlencodedformentity(namevaluepairs)); httpresponse response; response = httpclient.execute(httppost); } catch (clientprotocolexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } private void get_my_gps_location(){ gps = new gpstracker(certisservices.this); // check if gps enabled if(gps.cangetlocation()){ latitude = gps.getlatitude(); longitude = gps.getlongitude(); }else{ // can't location // gps or network not enabled // ask user enable gps/network in settings //gps.showsettingsalert(); } } } main activity
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); getactionbar().setdisplayhomeasupenabled(true); startservice(new intent(getbasecontext(), certisservices.class)); i don't error messages. can me fix ? thanks.
as @pskink have typo in service code. try rename onstartcommand method onstartcommand (and ondestroy ondestroy).
if don't fix issue, should check if declared service in manifest :
<service android:name="your.package.certisservice" />
Comments
Post a Comment