0

I am trying to create a simple service in android.

I checked the Settings->application tab and I can see that the service is running. Unfortunately OnStartCommand function is not getting executed. I can't even see the toast message.

Here is my 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();
        // Your 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);
            // Your 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 get location
            // GPS or Network is not enabled
            // Ask user to 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 get any error messages. Can anyone help me to fix this ? Thanks.

Hans1984
  • 796
  • 11
  • 24
Sathya Baman
  • 3,424
  • 7
  • 44
  • 77

2 Answers2

3

As @pskink say you have a typo in your service code. Try to rename OnStartCommand method to onStartCommand (and also OnDestroy to onDestroy).

If it don't fix your issue, you should check if you declared your service in the manifest :

<service android:name="your.package.CertisService" />
sonic
  • 1,894
  • 1
  • 18
  • 22
0

Try using a log instead of toast. The OnStartCommand() might be executing but sometimes(In my case never) the toast messages never appear. Try to debug the service.

Bhushan
  • 205
  • 2
  • 14