Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<pre><code>public class FastMainActivity extends Activity { Button searchBtn = null; Intent locatorService = null; AlertDialog alertDialog = null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); searchBtn = (Button) findViewById(R.id.searchBtn); searchBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub if (!startService()) { CreateAlert("Error!", "Service Cannot be started"); } else { Toast.makeText(FastMainActivity.this, "Service Started", Toast.LENGTH_LONG).show(); } } }); } public boolean stopService() { if (this.locatorService != null) { this.locatorService = null; } return true; } public boolean startService() { try { // this.locatorService= new // Intent(FastMainActivity.this,LocatorService.class); // startService(this.locatorService); FetchCordinates fetchCordinates = new FetchCordinates(); fetchCordinates.execute(); return true; } catch (Exception error) { return false; } } public AlertDialog CreateAlert(String title, String message) { AlertDialog alert = new AlertDialog.Builder(this).create(); alert.setTitle(title); alert.setMessage(message); return alert; } public class FetchCordinates extends AsyncTask&lt;String, Integer, String&gt; { ProgressDialog progDailog = null; public double lati = 0.0; public double longi = 0.0; public LocationManager mLocationManager; public VeggsterLocationListener mVeggsterLocationListener; @Override protected void onPreExecute() { mVeggsterLocationListener = new VeggsterLocationListener(); mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); mLocationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, 0, 0, mVeggsterLocationListener); progDailog = new ProgressDialog(FastMainActivity.this); progDailog.setOnCancelListener(new OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { FetchCordinates.this.cancel(true); } }); progDailog.setMessage("Loading..."); progDailog.setIndeterminate(true); progDailog.setCancelable(true); progDailog.show(); } @Override protected void onCancelled(){ System.out.println("Cancelled by user!"); progDialog.dismiss(); mLocationManager.removeUpdates(mVeggsterLocationListener); } @Override protected void onPostExecute(String result) { progDailog.dismiss(); Toast.makeText(FastMainActivity.this, "LATITUDE :" + lati + " LONGITUDE :" + longi, Toast.LENGTH_LONG).show(); } @Override protected String doInBackground(String... params) { // TODO Auto-generated method stub while (this.lati == 0.0) { } return null; } public class VeggsterLocationListener implements LocationListener { @Override public void onLocationChanged(Location location) { int lat = (int) location.getLatitude(); // * 1E6); int log = (int) location.getLongitude(); // * 1E6); int acc = (int) (location.getAccuracy()); String info = location.getProvider(); try { // LocatorService.myLatitude=location.getLatitude(); // LocatorService.myLongitude=location.getLongitude(); lati = location.getLatitude(); longi = location.getLongitude(); } catch (Exception e) { // progDailog.dismiss(); // Toast.makeText(getApplicationContext(),"Unable to get Location" // , Toast.LENGTH_LONG).show(); } } @Override public void onProviderDisabled(String provider) { Log.i("OnProviderDisabled", "OnProviderDisabled"); } @Override public void onProviderEnabled(String provider) { Log.i("onProviderEnabled", "onProviderEnabled"); } @Override public void onStatusChanged(String provider, int status, Bundle extras) { Log.i("onStatusChanged", "onStatusChanged"); } } } } </code></pre>
 

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