我有一个定期为服务器发送数据的服务,经度和纬度来自要发送的数据。 问题是异步任务总是在registerLocationUpdates()结束之前开始执行,请帮我解决它,我唯一想要的是保证在寄存器位置更新结束之前不会执行PostQos任务。
public class QosTestsService extends Service {
private Handler handler;
SharedPreferences mshared;
protected LocationManager locationManager;
protected LocationListener locationListener;
double longitude = 0.0, latitude = 0.0;
String provider;
boolean waitingForLocationUpdate = true;
private boolean firstTime = true, running = true;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
Toast.makeText(this, " MyService Created ", Toast.LENGTH_LONG).show();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, " MyService Started", Toast.LENGTH_LONG).show();
int delay = 5000; //milliseconds
mshared = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
handler.postDelayed(this, MyApplication.routineSchedule.getFrequency() * 60 * 1000);
if (MyApplication.routineSchedule.getEnabled().equals("true")){
registerLocationUpdates();
new PostQosTest(getApplicationContext(), true).execute();
}
}
}, delay);
return START_STICKY;
}
private void registerLocationUpdates() {
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_LOW);
criteria.setPowerRequirement(Criteria.POWER_LOW);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
locationManager = (LocationManager) this
.getSystemService(LOCATION_SERVICE);
provider = locationManager.getBestProvider(criteria, true);
// Cant get a hold of provider
if (provider == null) {
return;
}
locationListener = new MyLocationListener();
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
0, locationListener);
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
// connect to the GPS location service
Location oldLocation = locationManager.getLastKnownLocation(provider);
if (oldLocation != null) {
waitingForLocationUpdate = false;
}
}
private class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
latitude = location.getLatitude();
longitude = location.getLongitude();
Log.d("hello im here", "the value are " + latitude + " "
+ longitude);// , msg)
// Log.v(TAG, "IN ON LOCATION CHANGE");
if (waitingForLocationUpdate) {
// getNearbyStores();
waitingForLocationUpdate = false;
}
if (ActivityCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
locationManager.removeUpdates(this);
}
public void onStatusChanged(String s, int i, Bundle bundle) {
// Log.v(TAG, "Status changed: " + s);
}
public void onProviderEnabled(String s) {
// Log.e(TAG, "PROVIDER DISABLED: " + s);
}
public void onProviderDisabled(String s) {
// Log.e(TAG, "PROVIDER DISABLED: " + s);
}
}
private class PostQosTest extends AsyncTask<Void, Void, String> {
String ipAddress, res;
Profile profile;
Context context;
String carrierName;
QosTest qosTest;
boolean isSignalStrength;
String testDate;
public PostQosTest(Context context, boolean isSignalStrength){
this.context = context;
this.isSignalStrength = isSignalStrength;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
try {
testDate = new SimpleDateFormat("HH:mm:ss dd-MMM-yyyy", Locale.ENGLISH).format(new Date());
} catch (Exception e) {
e.printStackTrace();
}
try {
InetAddress ip = Actions.GetIp();
ipAddress = ip.toString();
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
TCTDbAdapter sour = new TCTDbAdapter(context);
sour.open();
ArrayList<Profile> arr = sour.getAllProfiles();
profile = arr.get(0);
sour.close();
TelephonyManager manager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
carrierName = manager.getSimOperatorName();
qosTest = new QosTest();
qosTest.setId(0);
try {
qosTest.setUserId(profile.getId());
} catch (Exception e) {
e.printStackTrace();
qosTest.setUserId("");
}
try {
qosTest.setDeviceId(String.valueOf(mshared.getInt(getString(R.string.device_id), 0)));
} catch (Exception e) {
e.printStackTrace();
qosTest.setDeviceId("");
}
try {
qosTest.setDeviceModel(Actions.getDeviceName());
} catch (Exception e) {
qosTest.setDeviceModel("");
}
qosTest.setDeviceType("1");
try {
qosTest.setServiceProvider(carrierName);
} catch (Exception e) {
e.printStackTrace();
qosTest.setServiceProvider("");
}
try {
qosTest.setIp(ipAddress.substring(1));
} catch (Exception e) {
e.printStackTrace();
qosTest.setIp("");
}
try {
qosTest.setLocationIPAddress(MyApplication.ip);
} catch (Exception e) {
e.printStackTrace();
qosTest.setLocationIPAddress("");
}
try {
qosTest.setLocationX(Double.toString(longitude));
} catch (Exception e) {
e.printStackTrace();
qosTest.setLocationX("");
}
try {
qosTest.setLocationY(Double.toString(latitude));
} catch (Exception e) {
e.printStackTrace();
qosTest.setLocationY("");
}
try {
qosTest.setTimeStamp("");
} catch (Exception e) {
e.printStackTrace();
qosTest.setTimeStamp("");
}
qosTest.setTestType(isSignalStrength ? MyApplication.SIGNAL_STRENGTH_TEST : MyApplication.CALL_DISCONNECTION_TEST);
qosTest.setCallDisconnectionReason("");
try {
qosTest.setTestDateTime(testDate);
} catch (Exception e) {
e.printStackTrace();
qosTest.setTestDateTime(testDate);
}
try {
qosTest.setIsIncident("");
} catch (Exception e) {
e.printStackTrace();
qosTest.setIsIncident("");
}
try {
qosTest.setSignalStrength("");
} catch (Exception e) {
e.printStackTrace();
qosTest.setSignalStrength("");
}
try {
qosTest.setConnectionType(NetworkUtil.getNetworkClass(context));
} catch (Exception e) {
e.printStackTrace();
qosTest.setConnectionType(NetworkUtil.getNetworkClass(context));
}
try {
qosTest.setCallDuration("");
} catch (Exception e) {
e.printStackTrace();
qosTest.setCallDuration("");
}
try {
qosTest.setTestTriggerType("");
} catch (Exception e) {
e.printStackTrace();
qosTest.setTestTriggerType("");
}
try {
qosTest.setTriggerStartDate("");
} catch (Exception e) {
e.printStackTrace();
qosTest.setTriggerStartDate("");
}
try {
qosTest.setValidationEndDate("");
} catch (Exception e) {
e.printStackTrace();
qosTest.setValidationEndDate("");
}
try {
qosTest.setResultId("");
} catch (Exception e) {
e.printStackTrace();
qosTest.setResultId("");
}
try {
qosTest.setStatusId("");
} catch (Exception e) {
e.printStackTrace();
qosTest.setStatusId("");
}
}
@Override
protected String doInBackground(Void... a) {
// URLEncoder.encode(searchText, "utf-8");
String url = ****;
try {
url = url
+ "UserId="
+ URLEncoder.encode(profile.getId(), "utf-8")
+ "&"
+ "mobileNumber="
+ URLEncoder.encode(profile.getnum(), "utf-8")
+ "&"
+ "ServiceProvider="
+ URLEncoder.encode(qosTest.getServiceProvider(), "utf-8")
+ "&"
+ "ipAddress="
+ URLEncoder.encode(qosTest.getIp(), "utf-8")
+ "&"
+ "LocationIPAddress="
+ URLEncoder.encode(qosTest.getLocationIPAddress(), "utf-8")
+ "&"
+ "DeviceId="
+ URLEncoder.encode(String.valueOf(qosTest.getDeviceId()), "utf-8")
+ "&"
+ "DeviceModel="
+ URLEncoder.encode(qosTest.getDeviceModel(), "utf-8")
+ "&"
+ "DeviceType="
+ URLEncoder.encode(qosTest.getDeviceType(), "utf-8")
+ "&"
+ "LocationX="
+ URLEncoder.encode(qosTest.getLocationX(), "utf-8")
+ "&"
+ "LocationY="
+ URLEncoder.encode(qosTest.getLocationY(), "utf-8")
+ "&"
+ "TestTypeId="
+ URLEncoder.encode(qosTest.getTestType(), "utf-8")
+ "&"
+ "CallDisconnectionReason="
+ URLEncoder.encode(qosTest.getCallDisconnectionReason(), "utf-8")
+ "&"
+ "TestDate="
+ URLEncoder.encode(qosTest.getTestDateTime(), "utf-8")
+ "&"
+ "IsIncident="
+ URLEncoder.encode(qosTest.getIsIncident(), "utf-8")
+ "&"
+ "SignalStrength="
+ URLEncoder.encode(qosTest.getSignalStrength(), "utf-8")
+ "&"
+ "ConnectionType="
+ URLEncoder.encode(qosTest.getConnectionType(), "utf-8")
+ "&"
+ "CallDuration="
+ URLEncoder.encode(qosTest.getCallDuration(), "utf-8")
+ "&"
+ "TestTriggerTypeId="
+ URLEncoder.encode(qosTest.getTestTriggerType(), "utf-8")
+ "&"
+ "TriggerStartDate="
+ URLEncoder.encode(qosTest.getTriggerStartDate(), "utf-8")
+ "&"
+ "ValidationEndDate="
+ URLEncoder.encode(qosTest.getValidationEndDate(), "utf-8")
+ "&"
+ "ResultId="
+ URLEncoder.encode(qosTest.getResultId(), "utf-8")
+ "&"
+ "StatusId="
+ URLEncoder.encode(qosTest.getStatusId(), "utf-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
...
return "";
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
}
}
}
答案 0 :(得分:1)
解决了,我只是将执行代码移动到了on location changed函数的最后一行。