прошлой ночью я трачу много времени на решение этой проблемы.И я решил проблему, проблема в том, когда я пытаюсь взять местоположение GPS из потока:
private Executor executor = newFixedThreadPool(1);
, более точно в метоне:
private boolean sendCoordinates(String terminalId, String lattitude, String longitude)
, который вызывается в потоке:
executor.execute(new Runnable() {
public void run() {
Message msg = msgHandler.obtainMessage();
msg.arg1 = sendCoordinates("123456", "23.25", "45.02") ? 1 : 0;
msgHandler.sendMessage(msg);
}
});
и для решения этой проблемы я делаю другой метод, чтобы получить местоположение и сохранить эту дату в локальной переменной, в моем случае два просмотра текста:
private void getLocation() {
if (ActivityCompat.checkSelfPermission(SendActivity.this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission
(SendActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(SendActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);
} else {
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
Location location1 = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Location location2 = locationManager.getLastKnownLocation(LocationManager. PASSIVE_PROVIDER);
if (location != null) {
double latti = location.getLatitude();
double longi = location.getLongitude();
lattitude = String.valueOf(latti);
longitude = String.valueOf(longi);
textView.setText(lattitude);
textView1.setText(longitude);
} else if (location1 != null) {
double latti = location1.getLatitude();
double longi = location1.getLongitude();
lattitude = String.valueOf(latti);
longitude = String.valueOf(longi);
textView.setText(lattitude);
textView1.setText(longitude);
} else if (location2 != null) {
double latti = location2.getLatitude();
double longi = location2.getLongitude();
lattitude = String.valueOf(latti);
longitude = String.valueOf(longi);
textView.setText(lattitude);
textView1.setText(longitude);
}else{
Toast.makeText(this,"Unble to Trace your location",Toast.LENGTH_SHORT).show();
}
}
}
и после этого измененияметод sendCoordinates:
private boolean sendCoordinates(String terminalId, String lat, String lng) {
HttpURLConnection con = null;
try {
URL obj = new URL("http://192.168.0.102:8085/position/send");
con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", "Mozilla/5.0");
con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
con.setDoOutput(true);
OutputStream os = con.getOutputStream();
os.write(String.format(STATIC_LOCATION, terminalId, lat, lng).getBytes());
os.flush();
os.close();
int responseCode = con.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) { //success
BufferedReader in = new BufferedReader(new InputStreamReader(
con.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
return true;
} else {
return false;
}
} catch (Exception e) {
// e.printStackTrace();
return false;
} finally {
if (con != null) {
con.disconnect();
}
}
}
и, наконец, метод onClick:
@Override
public void onClick(View v) {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
buildAlertMessageNoGps();
} else if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
getLocation();
}
executor.execute(new Runnable() {
public void run() {
Message msg = msgHandler.obtainMessage();
// use MAC addr or IMEI as terminal id
// read true position
// replace static coordinates with the ones from the true position
lat1=textView.getText().toString();
long1=textView1.getText().toString();
msg.arg1 = sendCoordinates("999999", lat1, long1) ? 1 : 0;
msgHandler.sendMessage(msg);
}
});
}