Hello, I have never encountered AsyncTask before, and now it's time. I need, in AsyncTask shove location determination. I do this:
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Geolocation(); } private void Geolocation() { final ProgressDialog progressDialog = new ProgressDialog(this); new AsyncTask<Void, Integer, Void>() { private Exception m_error = null;@Override protected void onPreExecute() { progressDialog.setMessage("Search ..."); progressDialog.setCancelable(false); progressDialog.setMax(100); progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressDialog.show(); } protected void doInBackground() { //-------------------------ΠΠ΅ΡΡΠΎΠΏΠΎΠ»ΠΎΠΆΠ΅Π½ΠΈΠ΅---------------------------// LocationManager locationManager; String context = Context.LOCATION_SERVICE; locationManager = (LocationManager) getSystemService(context); Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_FINE); criteria.setAltitudeRequired(false); criteria.setBearingRequired(false); criteria.setCostAllowed(true); criteria.setPowerRequirement(Criteria.POWER_LOW); String provider = locationManager.getBestProvider(criteria, true); Location location = locationManager.getLastKnownLocation(provider); updateWithNewLocation(location); locationManager.requestLocationUpdates(provider, 2000, 10, locationListener); } // ΠΎΠ±Π½ΠΎΠ²Π»ΡΠ΅ΠΌ progressDialog protected void onProgressUpdate(Integer... values) { progressDialog.setProgress((int)((values[0] / (float) values[1]) * 100)); }; protected void onPostExecute() { // ΠΎΡΠΎΠ±ΡΠ°ΠΆΠ°Π΅ΠΌ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΠ΅, Π΅ΡΠ»ΠΈ Π²ΠΎΠ·Π½ΠΈΠΊΠ»Π° ΠΎΡΠΈΠ±ΠΊΠ° if (m_error != null) { m_error.printStackTrace(); return; } // Π·Π°ΠΊΡΡΠ²Π°Π΅ΠΌ ΠΏΡΠΎΠ³ΡΠ΅ΡΡ ΠΈ ΡΠ΄Π°Π»ΡΠ΅ΠΌ Π²ΡΠ΅ΠΌΠ΅Π½Π½ΡΠΉ ΡΠ°ΠΉΠ» progressDialog.hide(); } @Override protected Void doInBackground(Void... params) { // TODO Auto-generated method stub return null; } }; }
But for some reason it does not work. Tell me please. Although if I remove AsyncTask and insert the code between the "location" comments in OnCreate, then everything will work, but I need it to work in AsyncTask.
UPDATED:
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); LocationManager locationManager; String context = Context.LOCATION_SERVICE; locationManager = (LocationManager)getSystemService(context); Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_FINE); criteria.setAltitudeRequired(false); criteria.setBearingRequired(false); criteria.setCostAllowed(true); criteria.setPowerRequirement(Criteria.POWER_LOW); String provider = locationManager.getBestProvider(criteria, true); Location location = locationManager.getLastKnownLocation(provider); updateWithNewLocation(location); locationManager.requestLocationUpdates(provider, 2000, 10, locationListener); } private final LocationListener locationListener = new LocationListener() { public void onLocationChanged(Location location) {//ΠΏΠΎΡΡΠΎΡΠ½Π½ΠΎΠ΅ ΠΎΠ±Π½ΠΎΠ²Π»Π΅Π½ΠΈΠ΅. updateWithNewLocation(location); } public void onProviderDisabled(String provider){ updateWithNewLocation(null); } public void onProviderEnabled(String provider){ } public void onStatusChanged(String provider, int status, Bundle extras){ } }; @SuppressLint("NewApi") private void updateWithNewLocation(Location location) { String latLongString; TextView myLocationText; myLocationText = (TextView)findViewById(R.id.location); String addressString = "No address found"; if (location != null) { double latitude = location.getLatitude(); double longitude = location.getLongitude(); Geocoder gc = new Geocoder(this, Locale.getDefault()); try { List<Address> addresses = gc.getFromLocation(latitude, longitude, 1); if (addresses.size() > 0) { Address address = addresses.get(0); if (address.getCountryName() != null) { country_location = address.getCountryName(); } if (address.getLocality() != null) { city_location = address.getLocality(); } if (address.getThoroughfare() != null) { street_location = address.getThoroughfare(); } if (address.getFeatureName() != null) { house_location = address.getFeatureName(); } geolocation.append(country_location).append(","); geolocation.append(city_location).append(","); geolocation.append(street_location).append(","); geolocation.append(house_location).append("."); } addressString = geolocation.toString(); } catch (IOException e) {} } else { latLongString = "No location found"; updateWithNewLocation(location); } myLocationText.setText(addressString); }
UPDATED 2:
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.create_consumption); //-------------------------ΠΠ΅ΡΡΠΎΠΏΠΎΠ»ΠΎΠΆΠ΅Π½ΠΈΠ΅---------------------------// LocationManager locationManager; String context = Context.LOCATION_SERVICE; locationManager = (LocationManager)getSystemService(context); Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_FINE); criteria.setAltitudeRequired(false); criteria.setBearingRequired(false); criteria.setCostAllowed(true); criteria.setPowerRequirement(Criteria.POWER_LOW); String provider = locationManager.getBestProvider(criteria, true); // Location location = locationManager.getLastKnownLocation(provider); Location location; // updateWithNewLocation(location); locationManager.requestLocationUpdates(provider, 2000, 10, locationListener); //-------------------------ΠΠ΅ΡΡΠΎΠΏΠΎΠ»ΠΎΠΆΠ΅Π½ΠΈΠ΅---------------------------// mt = new MyTask(); mt.execute(); } private final LocationListener locationListener = new LocationListener() { public void onLocationChanged(Location location) { // updateWithNewLocation(location); } public void onProviderDisabled(String provider){ updateWithNewLocation(null); } public void onProviderEnabled(String provider){ } public void onStatusChanged(String provider, int status, Bundle extras){ } }; @SuppressLint("NewApi") private void updateWithNewLocation(Location location) { String latLongString; TextView myLocationText; myLocationText = (TextView)findViewById(R.id.location); String addressString = "No address found"; if (location != null) { double latitude = location.getLatitude(); double longitude = location.getLongitude(); Geocoder gc = new Geocoder(this, Locale.getDefault()); try { List<Address> addresses = gc.getFromLocation(latitude, longitude, 1); if (addresses.size() > 0) { Address address = addresses.get(0); if (address.getCountryName() != null) { country_location = address.getCountryName(); } if (address.getLocality() != null) { city_location = address.getLocality(); } if (address.getThoroughfare() != null) { street_location = address.getThoroughfare(); } if (address.getFeatureName() != null) { house_location = address.getFeatureName(); } geolocation.append(country_location).append(","); geolocation.append(city_location).append(","); geolocation.append(street_location).append(","); geolocation.append(house_location).append("."); } addressString = geolocation.toString(); } catch (IOException e) {} } else { latLongString = "No location found"; updateWithNewLocation(location); } myLocationText.setText(addressString); } class MyTask extends AsyncTask<Void, Void, Void> { protected void onPreExecute() { super.onPreExecute(); } protected Void doInBackground(Void... params) { updateWithNewLocation(location); return null; } @Override protected void onPostExecute(Void result) { super.onPostExecute(result); } }
UPDATED:
private void updateWithNewLocation(Location location) { String latLongString; myLocationText = (TextView)findViewById(R.id.location); String addressString = "No address found"; if (location != null) { double latitude = location.getLatitude(); double longitude = location.getLongitude(); mt = new MyTask(); mt.execute(latitude,longitude); } } class MyTask extends AsyncTask<Double, Void, String> { @Override protected void onPreExecute() { super.onPreExecute(); } protected String doInBackground(Double... params) { String addressString = "No address found"; Geocoder gc = new Geocoder(this, Locale.getDefault()); try { List<Address> addresses = gc.getFromLocation(latitude, longitude, 1); StringBuilder sb = new StringBuilder(); if (addresses.size() > 0) { Address address = addresses.get(0); if (address.getCountryName() != null) { country_location = address.getCountryName(); geolocation.append(country_location).append(","); } if (address.getLocality() != null) { city_location = address.getLocality(); geolocation.append(city_location).append(","); } if (address.getThoroughfare() != null) { street_location = address.getThoroughfare(); geolocation.append(street_location).append(","); } if (address.getFeatureName() != null) { house_location = address.getFeatureName(); geolocation.append(house_location).append("."); } addressString = geolocation.toString(); } return addressString; } } protected void onPostExecute(String addressString) { myLocationText.setText(addressString); } }