Android 线程中的值未更新

Android 线程中的值未更新,android,android-service,synchronized,Android,Android Service,Synchronized,我做了一个后台服务,它每40秒向数据库获取一次值,但这里的经度和纬度值不更新,而在GPSTracker服务中,这些值每30秒更新一次。此服务仅获取其获取的坐标的第一个值 BackService.java: public class BackService extends Service { static double latitude,longitude; GPSTracker gpsTracker; private static final String DATA_URL = "http:/

我做了一个后台服务,它每40秒向数据库获取一次值,但这里的经度和纬度值不更新,而在GPSTracker服务中,这些值每30秒更新一次。此服务仅获取其获取的坐标的第一个值

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);
}
}
公共类后台服务扩展服务{
静态双纬度、经度;
GPSTracker-GPSTracker;
私有静态最终字符串数据\u URL=“http://“+Userstate.IP+”/spytracem/enter_loc.php”;
公共后台服务(){
}
@凌驾
公共IBinder onBind(意向){
返回null;
}
@凌驾
public void onCreate(){
super.onCreate();
gpsTracker=新的gpsTracker(getApplicationContext());
if(gpsTracker.canGetLocation()){
longitude=gpsTracker.getLongitude();
纬度=gpsTracker.getLatitude();
}
}
@凌驾
公共int onStartCommand(Intent Intent、int标志、int startId){
Runnable r=新的Runnable(){
@凌驾
公开募捐{
对于(int a=0;a<10;a++){
if(!getSharedReferences(Userstate.STATESAVE,Context.MODE\u PRIVATE).getBoolean(Userstate.status,false))
{
long futuretime=System.currentTimeMillis()+40000;
while(System.currentTimeMillis()
尝试使纬度、经度不稳定:

static volatile double latitude,longitude;
还有你的代码:

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

每次都必须调用,您只能在onCreate()中调用它。

提示:不要每30秒提取一次这样的数据。使用池连接或套接字连接到延迟加载的服务器。我没有看到您在线程runnable中检索纬度/经度,只有在onStartCommand()中,我建议您在
catch(Exception E){}
中至少添加一个Log.E()语句,以测试(并可能报告)问题。