Note that there are some explanatory texts on larger screens.

plurals
  1. POStoring a Map Location - android
    text
    copied!<p>I'm trying to get a GPS location and parse it to a String to in turn parse that to a clickable URL. I've done the code to get the location, which works, but because it can take a while to get a location, the String was executing before the location was parsed. I've tried messing around with Process Dialogs etc, but I can't get it to work.</p> <p>See Code</p> <p>public class ConfirmScreen extends Activity{</p> <pre><code>String mapCoord = "http://maps.google.com"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_confirm_screen); LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); LocationListener mLocListener = new MyLocationListener(); mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0, mLocListener); sendEmail(); playSound(); } public void backHome(View view) { Intent intent = new Intent (this, MainScreen.class); startActivity(intent); } // Method to start playing and looping a sound. public void playSound() { MediaPlayer clickSound = MediaPlayer.create(this, R.raw.warning); SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); Boolean soundCheck = sp.getBoolean("SOUND", false); if (soundCheck) { clickSound.start(); } }// method end public void sendEmail() { SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); String nameValue = sp.getString("NAME", "failed to get name"); String emailValue = sp.getString("EMAIL", "failed to get email"); Intent i = new Intent(Intent.ACTION_SEND); i.setType("message/rfc822"); i.putExtra(Intent.EXTRA_EMAIL, new String[]{emailValue}); i.putExtra(Intent.EXTRA_SUBJECT, "Email sent from DON'T PANIC - A Chris O'Brien Project"); i.putExtra(Intent.EXTRA_TEXT, "Hi there\n" + nameValue + " is in mortal danger. Please see the co-ords attached and run to their rescue!" + " If you don't see any co-ords, they didn't check the box and assume you know where they are.\nKind Regards\nDon't Panic! \n\n\n" + mapCoord); try { startActivity(Intent.createChooser(i, "Send mail....")); } catch (android.content.ActivityNotFoundException ex){ Toast.makeText(ConfirmScreen.this, "There are no email clients installed or set up", Toast.LENGTH_SHORT).show(); } } public class MyAsyncTask extends AsyncTask&lt;Void, Void, Result&gt;{ private Activity activity; private ProgressDialog progressDialog; private double longitude; private double latitude; private String countryCode; public MyAsyncTask(Activity activity, double longitude, double latitude) { super(); this.activity = activity; this.longitude = longitude; this.latitude = latitude; } @Override protected void onPreExecute() { super.onPreExecute(); ProgressDialog.show(activity, "", "Looking for GPS satellites..."); } @Override protected Result doInBackground(Void... v) { Geocoder geocoder = new Geocoder(activity, Locale.getDefault()); List&lt;Address&gt; addresses; try { addresses = geocoder.getFromLocation(latitude, longitude, 1); countryCode = addresses.get(0).getAddressLine(2); }catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); Toast.makeText(activity, "Location is not available, please try again later.", Toast.LENGTH_LONG).show(); } if(countryCode==null){ Toast.makeText(activity, "Location is not available, please try again later.", Toast.LENGTH_LONG).show(); } Toast.makeText(activity, countryCode, Toast.LENGTH_LONG).show(); return null; } @Override protected void onPostExecute(Result result) { progressDialog.dismiss(); Toast.makeText(activity.getApplicationContext(), "Finished.", Toast.LENGTH_LONG).show(); } } //Location Listener public class MyLocationListener implements LocationListener { @Override public void onLocationChanged(Location location) { double lng = location.getLatitude(); double lat = location.getLongitude(); MyAsyncTask task = new MyAsyncTask(ConfirmScreen.this, lng, lat); task.execute(); Toast.makeText(getApplicationContext(), "Done :D", Toast.LENGTH_LONG).show(); String text = "Current Location is \nLat: " + lng + " \nLng: " + lat; mapCoord = Double.toString(lng) + " " + Double.toString(lat); Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show(); } @Override public void onProviderDisabled(String provider) { Toast.makeText(getApplicationContext(), "GPS Disabled", Toast.LENGTH_SHORT).show(); } @Override public void onProviderEnabled(String provider) { Toast.makeText(getApplicationContext(), "GPS Enabled", Toast.LENGTH_SHORT).show(); } @Override public void onStatusChanged(String provider, int status, Bundle extras) { // TODO Auto-generated method stub } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_confirm_screen, menu); return true; } } </code></pre> <p>Compiles with no errors but it doesn't do what I intend. I've been read others code and trying to implement onto my own but I'm probably messing up something.</p> <p>1) onCreate gets location 2) A dialog box should pop up until GPS co-ords are received 3) Those co-ords should be passed to a String 4) An email is sent with the co-ords (this works fine) 5) Sound plays if box checked (again this works)</p> <p>I was getting the co-ords but the difficulty was that the email method was being called before the app has the chance to get the co-ords. I don't want to use lastKnownCo-ords.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload