show Progress Dialog in OnLocationChanged - android

I know there are thousands of questions like this. But I can't find an explanation to mine.
I use the onLocationChanged method to update the user's location on a mapView. Everything's fine; I display a ProgressDialog in the onCreate method and dismiss it at the end of OnlocationChanged and it works.
The problem is when I create and show a Progress Dialog inside the onLocationChanged method. Somehow it doesn't work. I think it's because the method runs on a different thread.
But my question is, if the onLocationChanged method runs on a different thread, why does it let me to dismiss the dialog but not create a new one?
Here's part of my class:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tiendas);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapView.setSatellite(false);
ProgressDialog dialog = ProgressDialog.show(StoresActivity.this, "",
"Getting your location", true, true);
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
/////if i show or initialize the dialog here, doesn't work!
updateMap(location) // this function calls asynctask
// for an HTTPrequest
dialog.dismiss();
}
}
That code works and dismisses the dialog correctly, but if I declare or show the dialog inside the onLocationChanged method, it never displays. Anybody?
Why does it dismiss it but can't show it?

Pass the Context of LocationListener class in ProgressDialog.

Check this code its working fine for me
public class LocationFinderActivity extends Activity implements LocationListener{
LocationManager locationManager = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location_finder);
Log.i("inside","oncreate");
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0,this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_location_finder, menu);
return true;
}
#Override
public void onLocationChanged(Location location) {
Log.i("inside","onlocationchange");
ProgressDialog dialog = ProgressDialog.show(LocationFinderActivity.this, "",
"Getting your location", true, true);
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}

Related

Remove Location Listener

I want after open dialog and click on location button, detect user's location and show user's location and then user dismiss dialog, location listener removes and not get location from onLocationChanged, I try used locationManager.removeUpdate(locListener)on override dismiss but not any chance and location after dismiss again shows! why? How can i detect user's location after show dialog and after detect location, location listener remove? Thanks
EDIT:
public class DialogTest extends Dialog implements
android.view.View.OnClickListener {
Context context;
LocationManager locationManager;
LocationListener locationListener;
Location location;
public DialogTest(Context context) {
super(context);
// TODO Auto-generated constructor stub
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.context = context;
setContentView(R.layout.profile_dialog);
getWindow().setBackgroundDrawableResource(R.drawable.background_dialog);
firstAccessLocation=true;
setCancelable(true);
initialize();
}
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
findAddress();
}
private void findAddress() {
// if (gpsCheckBox.isChecked()) {
locationManager = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
locationListener = new GPSLocationListener();
locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER, 0,
0, locationListener);
locationManager.requestLocationUpdates(
locationManager.NETWORK_PROVIDER, 0, 0, locationListener);
// }
}
private void stopTracking(){
if(locationManager!=null){
locationManager.removeUpdates(locationListener);
}
}
#Override
public void dismiss() {
// TODO Auto-generated method stub
stopTracking();
gpsTurnOff();
super.dismiss();
}
public class GPSLocationListener implements LocationListener {
#Override
public void onLocationChanged(Location loc) {
// TODO Auto-generated method stub
if(location!=null)
Log.e("location", location.getLatitude()+"");
location = loc;
if (location != null&& firstAccessLocation) {
setAddress(location.getLatitude(), location.getLongitude());
firstAccessLocation=false;
}
Toast.makeText(context, "changed", Toast.LENGTH_LONG).show();
Log.e("location", "changed");
}
#Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
}
}
In new code you should use LocationClient rather then LocationManager. It's more efficient in getting the location with minimal energy(battery drain) with greater accuracy. Look here LocationClient vs LocationManager. With LocationClient established you can call getLastLocation() which provide you last location and doesn't start cyclic location updates.
If you want to stick with LocationManager use requestSingleUpdate (String provider, LocationListener listener, Looper looper) method to get only one newest location from it.

Android show progress dialog while waiting for location

I'm developing location based app using this example: http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/
But when I turn on phone, the location isn't available right on that moment. So I would like to show progress dialog while waiting for the location. Wanna do this in the background using AsyncTask.
Can you give any ideas how and where to do that?
There is no need of AsyncTask because Location service already running in different process, Just implement the LocationListener and register it on resume method, and in onCreateActivity check if location is null, the show the ProgressDialog, and in onLocationChanged() set the location and close the ProgressDialog
Implement locationListner interface and start your wait dialog override onlocation change method and there just cancel the dialog, all the best.
public class MainActivity extends Activity implements LocationListener{
ProgressDialog dialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//-------------------- Start your GPS Reading ------------------ //
dialog = new ProgressDialog(this);
dialog.setMessage("Please wait!");
dialog.show();
}
#Override
public void onLocationChanged(Location arg0) {
dialog.dismiss();
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
Place your ProgressDialog in onPreExecute, sample code below:
private ProgressDialog progressdialog;
#Override
protected void onPreExecute(){
super.onPreExecute();
progressdialog = new ProgressDialog(yourContext);
progressdialog.setMessage("Loading...");
progressdialog.show();
}
#Override
protected void onPostExecute(){
super.onPostExecute();
progressdialog.dismiss();
}

set Latitude and Longitude in map

i write a map viewer and i want to get Latitude and Longitude from user, then show him the location.
i use menu for this work. but i don't know how show him something to enter the values. this is my code.
public class main extends MapActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MapView view = (MapView) findViewById(R.id.themap);
view.setBuiltInZoomControls(true);
final MapController control = view.getController();
LocationManager manager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
LocationListener listener = new LocationListener() {
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onLocationChanged(Location location) {
control.setCenter(new GeoPoint((int)location.getLatitude(), (int)location.getLongitude()));
}
};
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
so i have 2 questions. first how can i create a menu for my locations. i must use context menu or something else.
second, if i get locations, i can enter them directly to my Latitude and Longitude?
in my code i use the default location of user.
Well if I am understanding you correctly you can do this 2 ways.
1) In your xml layout add an input box and button then on click set your location.
2) Override your onTouch and have and alert box pop up and ask for the long and lat. Then set it accordingly.
Both these will be setting your overLay: http://developer.android.com/resources/tutorials/views/hello-mapview.html
one of many overlay tutorials: http://eagle.phys.utk.edu/guidry/android/mapOverlayDemo.html
If u only want to show your current position yo can use MyLoicationOverlay class:
miPunto = new MyLocationOverlay(this, eMapa);
eMapa.getOverlays().add(miPunto);
miPunto.enableCompass();
miPunto.enableMyLocation();
If you wont to use this, yo must to get your location and then use an itemizedoverlay
This is my code, It works fine with me
Drawable marker=getResources().getDrawable(R.drawable.mapmarker);
marker.setBounds( (int) (-marker.getIntrinsicWidth()/2),
-marker.getIntrinsicHeight(),
(int) (marker.getIntrinsicWidth()/2),
0);
InterestingLocations funPlaces =
new InterestingLocations(marker);
Add your overlay Item in here.
mapView.getOverlays().add(funPlaces);
class InterestingLocations extends ItemizedOverlay {
private ArrayList<OverlayItem> locations =
new ArrayList<OverlayItem>();
private GeoPoint center = null;
public InterestingLocations(Drawable marker)
{
super(marker);
// Put your custom Lat, Long in here ,
GeoPoint disneySevenLagoon =
new GeoPoint((int)(28.410067*1000000),
(int)(-81.583699*1000000));
locations.add(new OverlayItem(disneySevenLagoon ,
"Seven Seas Lagoon", "Seven Seas Lagoon"));
populate();
}

Sending location to emulator

I am testing the option to send coordinates to the Android Emulator using the Built in Eclipse tool.But it doesn't seem to receive them.I have this simple code:
public class Main extends MapActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MapView view = (MapView) findViewById(R.id.themap);
view.setBuiltInZoomControls(true);
final MapController mp = view.getController();
LocationManager manager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
LocationListener listener = new LocationListener() {
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
public void onLocationChanged(final Location location) {
// TODO Auto-generated method stub
mp.setCenter(new GeoPoint((int)location.getLatitude(), (int)location.getLongitude()));
}
};
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
I have the permissions for ACCESS_FINE_LOCATION and INTERNET in the Manifest.
If someone has any explanation I would be grateful.
Did you add
<uses-library android:name="com.google.android.maps" />
to the application tag in the manifest?
You also need a MD5 Fingerprint of the SDK Debug Certificate from Google to receive Google Maps data. You can get it here.
Try to put a breakpoint inside the onLocationChanged method and see if it stops when sending the location command to the emulator. this should also work when you don't have a certificate yet.
This works for me.
Listener implementation...
public class MyLocationListener implements LocationListener {
public static final String TAG = "MyLocationListener";
private Context context;
public MyLocationListener(Context c) {
this.context = c;
}
public void onLocationChanged(android.location.Location location) {
Log.d(TAG,"LocationChanged: Lat: "+location.getLatitude()+" Lng: "+location.getLongitude());
}
}
Usage...
MyLocationListener locationListener = new MyLocationListener(this);
// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,locationListener);

whats wrong with this code?

I am trying to get the current GPS location of the mobile. I am using emulator I have given the position to the emulator using DDMS but I am getting nothing in the screen also I don't get any errors.
public class getitright extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager lm=(LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
LocationListener ll=new LocationListener(){
public void onLocationChanged(Location l)
{
go(l);
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
// TODO Auto-generated method stub
}
};
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
}
public void go(Location l){
TextView tv=(TextView)findViewById(R.id.tv1);
tv.setText(""+l.getLatitude());
}
}
Put some log messages in for a start.
But secondly. You shouldn't declare your TextView or your LocationListener to be local variables like that. Those should both be member variables. By declaring your locationlistener like that, you lose your reference to it. That could be the problem, but if it's not, you're still unable to remove the locationlistener from the manager because you don't have a reference.
Are you sure your textview is even showing at all? Try setting it to something static in onCreate just to make sure
Also add an #Override statement to
public void onLocationChanged(Location l)
{
go(l);
}
Just to be 100% sure you're actually overriding the method.

Categories

Resources