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.
Related
I've an app that gets the user's location. I've registered the locationManager and listener in oncreate(). I've requested and removed updates in onResume() and onPause() respectively. The app finds the location and calls the getTime() on the loc object of onLocationChanged, this is to get an external time and then later compare to system time. This time is set in an applicationObject so that it's available app-wide. Everything works fine and i toast the time to the user from the applicationObject time setter method.
What i'm finding is that when i have found the time by getting a fix on the user's location, and i move to the next activity, the app continues to get the time for another 20 secs and sometimes more.
How can this be when i have unregistered the locaction listener in the first activity's onPause() method?
#Override
protected void onPause() {
mlocManager.removeUpdates(mlocListener);
super.onPause();
}
#Override
protected void onResume() {
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
super.onResume();
}
private class MyLocationListener implements LocationListener {
#Override
public void onLocationChanged(Location loc) {
Log.e(TAG, "external time = " + loc.getTime());
DateTime dt = new DateTime(loc.getTime());
DateTimeFormatter df3 = DateTimeFormat.forPattern("yyyy-MM-dd H:mm:ss.SSS");
String formattedNowTime3 = df3.print(dt);
Log.e(TAG, "formatted ext time = " + formattedNowTime3);
nfcscannerapplication.setExternalTime(dt);
}
#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
}
}//end of MyLocationListener
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.entryscreen);
mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
mlocListener = new MyLocationListener();
.
The onPause() may be called only after the new activity has started.
If you only want a single onLocationChange(), you can use requestSingleUpdate() and your problem is gone.
Regards.
Set mlocListener to null just after removeUpdates();
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
}
}
The application doesnt point to where I am at..here is what i have done:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mv=(MapView) findViewById (R.id.mapView);
mv.setBuiltInZoomControls(true);
// mv.setSatellite(true);
mv.setStreetView(true);
lmanager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
llistener=new MyLocationListener();
lmanager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, llistener);
}
private class MyLocationListener implements LocationListener
{
#Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
if(location!=null)
{
Toast.makeText(getBaseContext(),"Location changed: "+location.getLatitude()+" lang: "+ location.getLongitude() , Toast.LENGTH_SHORT).show(); // no toast is shown
}
p=new GeoPoint((int)(location.getLatitude()*1E6), (int)(location.getLongitude()*1E6));
mapC.animateTo(p);
mapC.setZoom(18);//no zooming happens
}
#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
}
To my manifest I added both of these:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
first of all sorry for poor english. when the location was changed and you set the value into the map you need to invalidate the map using the invalidate() method of MapView class so you can get the updated value on the map
mapC.animateTo(p);
mapC.setZoom(18);
mv.invalidate();
as well as add after you set the value at initial time set into the map like
mv.setBuiltInZoomControls(true);
mv.setStreetView(true);
mv.invalidate();
Although I set minTime to 10 seconds in the requestLocatioupdate method I still get new location in less than one second.
public class GpsActivity extends Activity {
LocationManager mLocationManager;
TextView mTextView;
int count;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTextView = (TextView) findViewById(R.id.text);
count = 0;
mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 0,mGpsLocationListener);
}
Handler mHandler = new Handler(){
#Override
public void handleMessage(Message msg){
switch(msg.what){
case 1:
mTextView.setText("new location count:"+count);
}
}
};
LocationListener mGpsLocationListener = new LocationListener(){
#Override
public void onLocationChanged(Location location) {
count++;
mHandler.sendEmptyMessage(1);
}
#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
}
};
}
My program is written for android 2.2
Any idea?
Thanks in advance.
you can set a distance .if not solve,youcan see :minTime the minimum time interval for notifications, in milliseconds. This field is only used as a hint to conserve power, and actual time between location updates may be greater or lesser than this value.
minDistance the minimum distance interval for notifications, in meters
so i think the time not correct,if you hope update 10s,i think you should use Time andTimeTask,or handler. or you can look http://androidforums.com/developer-101/107085-proper-provider-parameter-requestlocationupdates.html
I wrote some code which basically spits out NMEA sentences. The code worked on froyo and did exactly what I wanted. Now However, when I got the gingerbread upgrade, it is no longer working. Has anyone else had this issue?
I'm using a Nexus One and will post some code later if you guys need it.
Thanks
Sorry, i was very busy lately. Here is the code finally:
public class GPSTest extends Activity {
TextView mTextView;
Button mStartButton, mStopButton;
LocationManager mLocationManager;
boolean isRegistered;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTextView = (TextView) findViewById(R.id.mTextView);
mStartButton = (Button) findViewById(R.id.Button01);
mStopButton = (Button) findViewById(R.id.Button02);
mStartButton.setOnClickListener(mButtonListener);
mStopButton.setOnClickListener(mButtonListener);
mLocationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);
}
void registerListeners(){
if(!isRegistered){
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0 , 0, mLocationListener);
mLocationManager.addNmeaListener(mListener);
isRegistered=true;
}
}
void deregisterListeners(){
if (isRegistered){
mLocationManager.removeUpdates(mLocationListener);
mLocationManager.removeNmeaListener(mListener);
isRegistered=false;
}
}
OnClickListener mButtonListener = new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v==mStartButton){
registerListeners();
}else{
deregisterListeners();
}
}
};
#Override
protected void onPause(){
super.onPause();
deregisterListeners();
}
#Override
protected void onResume(){
super.onResume();
registerListeners();
}
NmeaListener mListener = new NmeaListener(){
#Override
public void onNmeaReceived(long timestamp, String nmea) {
// TODO Auto-generated method stub
mTextView.append("\n"+nmea);
}
};
LocationListener mLocationListener = new LocationListener(){
#Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
;
}
#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
}
};
}
Documented bug in the post-Froyo implementations on some handsets (Nexus One I can confirm, Xoom anecdotally seems to be fine). Haven't heard from the folks using the Nexus S, but it sounds like they are fine.
See http://code.google.com/p/android/issues/detail?id=15500 for more detail.
Edit: trying to poke the folks at Google to see if some clarification can be found.
Edit 2: Oh yeah, verified that 2.2 works (HTC Incredible, Nexus One worked BEFORE the 2.3 update), but the same Nexus One no longer works after updating to 2.3.3.