我已经制作了一个后台服务,每隔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);
}
}
答案 0 :(得分:0)
尝试使纬度,经度不稳定:
static volatile double latitude,longitude;
还有你的代码:
longitude = gpsTracker.getLongitude();
latitude = gpsTracker.getLatitude();
每次都必须调用,只能在onCreate()中调用它。