I have a problem with my simple android aplication that use GPS cords.
My class MyLocationListener implements LocationListener, and there is a static method call:
String strText ="My current location is: " + "Latitude = " + location.getLatitude() + " Longitude= " + location.getLongitude();
Toast.makeText(GpsModule.cont, strText, Toast.LENGTH_SHORT).show();
Problem is with displaying this string. It's showing up, and never ends. When I press back button for main menu and even when I close application it's showing up constantly. Any clue? How can I resolve this problem?
GpsModule Class:
public class GpsModule extends Activity {
public static Context cont;
//public static WebView position;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.gps);
cont = getApplicationContext();
//position = new WebView(cont);
//position = (WebView) findViewById(R.layout.gps);
//position.getSettings().setJavaScriptEnabled(true);
LocationManager locManager = (LocationManager) getSystemService(LOCATION_SERVICE);
LocationListener locListener = new MyLocationListener();
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);
}
MyLocationListener class:
#SuppressLint("ShowToast")
public class MyLocationListener implements LocationListener{
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
location.getLatitude();
location.getLongitude();
String strText ="My current location is: " + "Latitude = " + location.getLatitude() + " Longitude= " + location.getLongitude();
Toast.makeText(GpsModule.cont, strText, Toast.LENGTH_SHORT).show();
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
Toast.makeText(GpsModule.cont, "GPS disabled", Toast.LENGTH_SHORT).show();
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
Toast.makeText(GpsModule.cont, "GPS enabled", Toast.LENGTH_SHORT).show();
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
Please read the docs of the method LocationManager.requestLocationUpdates().
Especially the parameter:
minTime minimum time interval between location updates, in milliseconds.
Try value 10'000 for every 10 sec. In production you should consider value more than mins.
I think, you add your code in your location detection function and LocationListener is active. That means your Toast is called when GPS detect new location.
Looks like your location listener is active and repeatedly gets called. are you moving your device? LocationListener is called when the location changes.
Try this,
1 .
Toast.makeText(getBaseContext(), strText, Toast.LENGTH_SHORT).show();
2 .
#Override
public void onPause() {
super.onPause();
locationManager.removeUpdates(locationListener);
}
3 .
#Override
public void onResume() {
super.onResume();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
Length, long, locationListener);
}
Length and long should be more than 0.
Related
I want to know a way to search for current location limited by a timer.
During that timer, the app should search various possibilities for current location which they will be filtered to get the best estimation.
Basically, I'm trying to do this:
Enable GPS providers;
Inside a runnable code, request updates to location from them, and then execute some way to filtered and obtain the best estimation;
The runnable should execute during a predefined timer or if the user cancel the search, it should stop.
Anyone have any idea to do this? Thanks.
this is globle variable
Location loc;
double latitude;
double longitude;
LocationManager mLocationManager;
this is bind location manager
mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
100, 3, mLocationListener);
this class
private final LocationListener mLocationListener = new LocationListener() {
#Override
public void onLocationChanged(final Location location) {
// your code here
latitude = location.getLatitude();//set here your current location latitude & longitude.
longitude = location.getLongitude();
Log.e(KEY_TAG, "Lat " + latitude + "Long " + longitude);
}
#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
}
};
when user location is change that a time to you will get the new lat long.
I have an android device that is mounted inside a car.
I need to receive gps location updates every second.
my problem is that I get all the necessary data (number of satellites , coordinates , time etc .. )
but for some reason "GpsStatus.GPS_EVENT_FIRST_FIX" is not called or if is called it takes a long time (like 30 minutes .. )
why is that ? here is my code :
public class ExtProtocolService extends Service implements GpsStatus.Listener {
private GpsStatus gpsStatus;
private GpsData gpsData;
private LocationManager locationManager;
private LocationListener locationListener;
#Override
public void onCreate() {
locationManager = (LocationManager)
getSystemService(this.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
String provider = locationManager.getBestProvider(criteria, true);
if(provider.equals("gps"))
{
locationListener = 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
// not used
}
#Override
// not used
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
// update location parameters when location is changed
#Override
public void onLocationChanged(Location location) {
gpsData.setLatitude(location.getLatitude());
L.m("LATLONG", "latitude " + location.getLatitude());
gpsData.setLongitude(location.getLongitude());
L.m("LATLONG", "longitude " + location.getLongitude());
gpsData.setSpeed(location.getSpeed());
gpsData.setHeading(location.getBearing());
gpsData.setAltitude(location.getAltitude());
gpsData.setHdop(location.getAccuracy());
}
};
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
1000, 0, locationListener); // update location every 1000 ms
locationManager.addGpsStatusListener(this);
}
}
#Override
public void onGpsStatusChanged(int event) {
switch (event) {
case GpsStatus.GPS_EVENT_FIRST_FIX:
gpsData.setLocked(true); // gps is locked
break;
case GpsStatus.GPS_EVENT_STOPPED:
gpsData.setLocked(false); // gps unreachable
break;
}
gpsStatus = locationManager.getGpsStatus(null);
// calc number of satelites fixed
Iterable<GpsSatellite> sats = gpsStatus.getSatellites();
int satellites = 0;
int satellitesInFix = 0;
int timetofix = gpsStatus.getTimeToFirstFix();
for (GpsSatellite sat : sats) {
if (sat.usedInFix()) {
satellitesInFix++;
}
satellites++;
}
gpsData.setNumSatellites(satellitesInFix);
L.m("GPS", gpsData.toString());
}
}
edit : also I can not use the google location API's ..
i am developing location based project where i am using the following code
i am using google api 8 for the project
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
currloc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
TextView t = (TextView)findViewById(R.id.textView1);
try{
t.setText("Your current location is - "+currloc.getLatitude()+","+currloc.getLongitude());
}catch (Exception e) {
// TODO: handle exception
t.setText("cant find current location ");
}
this code works fine on my galaxy tab even on htc
but when i use nexus it returns null value for location.
do i need to change my api level or is there any specific requirement for galaxy nexus
thank you in advance :)
Please follow the line of code..
Step1: into your oncreate
LocationListener locationListener = new LocalLocationListener();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
Step2: into class body
/**Listener on location change*/
private class LocalLocationListener implements LocationListener
{
public void onLocationChanged(Location location)
{
String text = "My current Location is: "+location.getLatitude()+", "+location.getLongitude();
GeoPoint geoPoint = new GeoPoint((int)(location.getLatitude()* 1E6), (int)(location.getLatitude() * 1E6));
mapController_.animateTo(geoPoint);
Toast.makeText(LocalMap.this, text, Toast.LENGTH_SHORT).show();
Log.i("onLocationChanged", text);
}
public void onProviderDisabled(String provider)
{
// TODO Auto-generated method stub
Toast.makeText(LocalMap.this, "GPS Disable", Toast.LENGTH_SHORT).show();
Log.i("onProviderDisabled", "GPS Disable");
}
public void onProviderEnabled(String provider)
{
// TODO Auto-generated method stub
Toast.makeText(LocalMap.this, "GPS Enable", Toast.LENGTH_SHORT).show();
Log.i("onProviderEnabled", "GPS Enable");
}
public void onStatusChanged(String provider, int status, Bundle extras)
{
// TODO Auto-generated method stub
}
I use below code to get current location of user. This code work in emulator but when I use this application in mobile it unable to give current latitude and longitude.
I uses <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> this code in android manifest file.
Please give me solution... my code is below
public class MenuPage extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.menupage);
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener locListener = new MyLocationListener();
locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, locListener);
}
/* Start of Class MyLocationListener for get current location of user */
public class MyLocationListener implements LocationListener
{
#Override
public void onLocationChanged(Location crurrentLocation)
{
Log.v("WHERE ","onLocationChanged()");
crurrentLocation.getLatitude(); // get current latitude
crurrentLocation.getLongitude(); // get current longitude
longitude=crurrentLocation.getLongitude();
latitude= crurrentLocation.getLatitude();
Log.v("WHERE ","onLocationChanged() Latitude="+latitude);
Log.v("WHERE ","onLocationChanged() Longitude="+longitude);
Toast.makeText(getApplicationContext(),"Latitud="+crurrentLocation.getLatitude(),Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(),"Longitud="+crurrentLocation.getLongitude(),Toast.LENGTH_SHORT).show();
send_current_location = new Send_Current_location_ToServer();
// Send current location to server
String server_message = send_current_location.sendData(userid,latitude,longitude);
// String text ="My current location is: " + "Latitud = " + crurrentLocation.getLatitude() + "Longitud = " + crurrentLocation.getLongitude();
// Toast.makeText(getApplicationContext(),text,Toast.LENGTH_SHORT).show();
}
#Override
public void onProviderDisabled(String provider)
{
Log.v("WHERE ","onProviderDisabled()");
Toast.makeText( getApplicationContext(),"Gps Disabled",Toast.LENGTH_SHORT ).show();
}
#Override
public void onProviderEnabled(String provider)
{
Log.v("WHERE ","onProviderEnabled()");
Toast.makeText( getApplicationContext(),"Gps Enabled",Toast.LENGTH_SHORT).show();
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
Log.v("WHERE ","onStatusChanged()");
}
}/* End of Class MyLocationListener */
}
There is a bug in the 2.3 emulator concerning the location services. Try changing to 2.1 to test your code.
For more info about location services, use this.
See here for the status of the bug.
Am working on an app, which toasts the latitude and longitude using LocationManager and LocationListener. On running the app, an error shows up saying "Sorry, Process system is not responding.". This happens when I supply the lat and long either manually from emulator control under DDMS or from command prompt using telnet.
Java Code:
public class LocationFinder extends Activity {
private LocationManager locManager;
private LocationListener locListener;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
locListener = new MyLocationListener();
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);
}
private class MyLocationListener implements LocationListener{
#Override
public void onLocationChanged(Location loc) {
// TODO Auto-generated method stub
if(loc != null){
Toast.makeText(getBaseContext(), "Latitude: " + loc.getLatitude() + "Longitude: " + loc.getLongitude(), Toast.LENGTH_SHORT).show();
}
}
#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
}
}
}
And I have set the following permissions in manifest.xml
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.ACCESS_ASSISTED_GPS" />
The emulator is also hw.gps enabled.
I would like to know if there is anything wrong with my code.
Thanks
Check by using Log that you are getting Values for Latitude and longitude..
Then in Toast put this
Toast.makeText(LocationFinder.this, "Latitude: " + loc.getLatitude() + "Longitude: " + loc.getLongitude(), Toast.LENGTH_SHORT).show();
instead of
Toast.makeText(getBaseContext(), "Latitude: " + loc.getLatitude() + "Longitude: " + loc.getLongitude(), Toast.LENGTH_SHORT).show();
// Des: Start Device's GPS and get current latitude and longitude
public void GPS() throws IOException {
// Des: This is a background service and called after every 10 minutes and fetch latitude-longitude values
background = new Thread(new Runnable() {
#Override
public void run() {
for (int i = 0; i < j; i++) {
if (ProjectStaticVariable.GPSExit == true ) {
try {
Thread.sleep(600000); //10 minutes
mainhandler.sendMessage(mainhandler.obtainMessage());
j++;
}
catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
}
});
background.start();
mainhandler = new Handler() {
public void handleMessage(Message msg) {
// Check Internet status
isInternetPresent = cd.isConnectingToInternet();
if (isInternetPresent) {
lat_long_Service_flag = true;
mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
mlocListener = new MyLocationListener(getApplicationContext());
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 0, mlocListener);
mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
}
}
};
}
// Des: Location Listener through which we get current latitude and longitude
public class MyLocationListener implements LocationListener {
public MyLocationListener(Context mContext) {}
public MyLocationListener(Runnable runnable) {}
#Override
public void onLocationChanged(Location loc) {
longitude = loc.getLongitude();
latitude = loc.getLatitude();
final_latitude = Double.toString(latitude);
final_longitude = Double.toString(longitude);
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}