0

i have made a background service which fetches value to database every 40 seconds but here longitude and latitude values are not updating, while in GPSTracker service, the values are updating every 30 second. This Service only fetches the first value of coordinates it get..

BackService.java:

public class BackService extends Service {

static double latitude,longitude;
GPSTracker gpsTracker;
private static final String DATA_URL = "http://"+Userstate.IP+"/spytracem/enter_loc.php";


public BackService() {
}

@Override
public IBinder onBind(Intent intent) {

    return null;
}

@Override
public void onCreate() {
    super.onCreate();
    gpsTracker = new GPSTracker(getApplicationContext());
    if (gpsTracker.canGetLocation()) {
        longitude = gpsTracker.getLongitude();
        latitude = gpsTracker.getLatitude();
    }


}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    Runnable r = new Runnable() {
            @Override
            public void run() {


                for (int a = 0; a < 10; a++) {
                    if (!getSharedPreferences(Userstate.STATESAVE,Context.MODE_PRIVATE).getBoolean(Userstate.status,false))
                    {
                    long futuretime = System.currentTimeMillis() + 40000;
                    while (System.currentTimeMillis() < futuretime) {
                        synchronized (this) {
                            try {
                                wait(futuretime - System.currentTimeMillis());
                                System.out.println(getSharedPreferences(Userstate.SHARED_PREF_NAME,Context.MODE_PRIVATE).getString(Userstate.EMAIL_SHARED_PREF,"No User"));
                                System.out.println(longitude);
                                System.out.println(latitude);

                                enter(getSharedPreferences(Userstate.SHARED_PREF_NAME,Context.MODE_PRIVATE).getString(Userstate.EMAIL_SHARED_PREF,"No User"), String.valueOf(longitude), String.valueOf(latitude));
                                if (a == 9) {
                                    a = 0;
                                }
                            } catch (Exception e) {
                            }
                                            }
                        }
                    }
                    else
                    {
                        getSharedPreferences(Userstate.STATESAVE, Context.MODE_PRIVATE).edit().putBoolean(Userstate.status,true);
                        stopSelf();
                        break;
                    }
                }


            }
        };

        Thread thread = new Thread(r);
        thread.start();
        return Service.START_STICKY;

}


@Override
public void onDestroy() {
    super.onDestroy();
    getSharedPreferences(Userstate.STATESAVE,Context.MODE_PRIVATE).edit().putBoolean(Userstate.status,true);
}

private void enter( String email, String longitude, String latitude) {
    class Enter extends AsyncTask<String, Void, String> {
        Locationhander ruc11 = new Locationhander();

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            System.out.println("Location Entered Boss!");
        }

        @Override
        protected String doInBackground(String... params) {

            HashMap<String, String> data2 = new HashMap<String,String>();
            data2.put("email",params[0]);
            data2.put("longitude",params[1]);
            data2.put("latitude",params[2]);

            String result2 = ruc11.sendPostRequest(DATA_URL,data2);
            return  result2;
        }
    }

    Enter ru2 = new Enter();
    ru2.execute(email, longitude, latitude);
}
}
  • Tip: Don't fetch data like that every 30 seconds. Use a pooled connection or a socket to a lazy loaded server. – NightSkyCode Dec 31 '15 at 20:16
  • I don't see you retrieving the latitude/longitude in your thread runnable, only in onStartCommand() – CSmith Dec 31 '15 at 21:39

1 Answers1

0

Try to make latitude,longitude volatile:

static volatile double latitude,longitude;

And also your code:

longitude = gpsTracker.getLongitude();
latitude = gpsTracker.getLatitude();

Must be called every time, you call it only in onCreate().

roma2341
  • 111
  • 2
  • 10
  • I would suggest also adding in at least a Log.E() statement inside your `catch (Exception e) {}` for testing (and possibly reporting) of issues. – mawalker Dec 31 '15 at 21:44