【问题标题】:Values are not updating in thread值未在线程中更新
【发布时间】:2015-12-31 20:15:12
【问题描述】:

我已经创建了一个后台服务,它每 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);
}
}

【问题讨论】:

  • 提示:不要每 30 秒获取一次这样的数据。对延迟加载的服务器使用池连接或套接字。
  • 我没有看到您在可运行的线程中检索纬度/经度,仅在 onStartCommand() 中

标签: android android-service synchronized


【解决方案1】:

尝试使经纬度不稳定:

static volatile double latitude,longitude;

还有你的代码:

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

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

【讨论】:

  • 我建议您在 catch (Exception e) {} 中至少添加一个 Log.E() 语句,以测试(并可能报告)问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-20
  • 2019-05-08
  • 1970-01-01
  • 2016-07-16
  • 1970-01-01
相关资源
最近更新 更多