Message not showing on emulator screen - android

Here is the code to find the current location, but nothing shows on the screen after pressing the retreive button.
I have reset adb and tried to send the data from emulator control manually. But when I send data using emulator control, then the emulator restarts but never shows the home screen.
Please help. Thanks in advance.
public class LbsGeocodingActivity extends Activity {
private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000; // in Milliseconds
protected LocationManager locationManager;
protected Button retrieveLocationButton;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
retrieveLocationButton = (Button) findViewById(R.id.retrieve_location_button);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MINIMUM_TIME_BETWEEN_UPDATES,
MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
new MyLocationListener()
);
retrieveLocationButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showCurrentLocation();
}
});
}
protected void showCurrentLocation() {
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
String message = String.format(
"Current Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude()
);
Toast.makeText(LbsGeocodingActivity.this, message,
Toast.LENGTH_LONG).show();
}
}
private class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
String message = String.format(
"New Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude()
);
Toast.makeText(LbsGeocodingActivity.this, message, Toast.LENGTH_LONG).show();
}
public void onStatusChanged(String s, int i, Bundle b) {
Toast.makeText(LbsGeocodingActivity.this, "Provider status changed",
Toast.LENGTH_LONG).show();
}
public void onProviderDisabled(String s) {
Toast.makeText(LbsGeocodingActivity.this,
"Provider disabled by the user. GPS turned off",
Toast.LENGTH_LONG).show();
}
public void onProviderEnabled(String s) {
Toast.makeText(LbsGeocodingActivity.this,
"Provider enabled by the user. GPS turned on",
Toast.LENGTH_LONG).show();
}
}
}

In you code you might not be receiving the current location(this is because in you need to push your location manually through emulator control).
Just have else case also in showCurrentLocation() and check whether the location object is null or not like below.
protected void showCurrentLocation() {
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
String message = String.format(
"Current Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude()
);
Toast.makeText(LbsGeocodingActivity.this, message,
Toast.LENGTH_LONG).show();
}else{
Toast.makeText(LbsGeocodingActivity.this, "location is null",
Toast.LENGTH_LONG).show();
}
}

On our first attempts to use the location sensors we had similar problems. Two things help the most in order to get the best results from GPS.
On the emulator the Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); causes a lot force closes.
And most important. Its far more stable to input the gps data through telnet. telnet localhost 5554 (You can see the port on the emulators window title). The command for a new gps fix is: geo fix lat lng

Related

unable to get gps location in certain devices in android

In my android application i am trying to get the gps location on a button click. i have tried many examples but the examples work on certain devices. The device on which i am testing i.e Micromax A90S, i am unable to get a location on it. i am unable to find out what exactly is the issue. Is it the device or do i need to do some changes in my code??? The code i am using is given below
protected void showCurrentLocation() {
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
String message = String.format(
"Current Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude()
);
Toast.makeText(SettingsActivity.this, message,
Toast.LENGTH_LONG).show();
}
}
private class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
String message = String.format(
"New Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude()
);
Toast.makeText(SettingsActivity.this, message, Toast.LENGTH_LONG).show();
}
public void onStatusChanged(String s, int i, Bundle b) {
Toast.makeText(SettingsActivity.this, "Provider status changed",
Toast.LENGTH_LONG).show();
}
public void onProviderDisabled(String s) {
Toast.makeText(SettingsActivity.this,
"Provider disabled by the user. GPS turned off",
Toast.LENGTH_LONG).show();
}
public void onProviderEnabled(String s) {
Toast.makeText(SettingsActivity.this,
"Provider enabled by the user. GPS turned on",
Toast.LENGTH_LONG).show();
}
}
Please help!! Thanks in advance!

Unable to find the latitude and longitude

I wrote code for finding the latitude and longitude of a place and it works well in my PC using Eclipse but it does not work on my Android phone.
public class MainActivity extends Activity {
protected LocationManager locationManager;
protected Button retrieveLocationButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
retrieveLocationButton = (Button) findViewById(R.id.retrieve_location_button);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1,
1000, new MyLocationListener());
retrieveLocationButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showCurrentLocation();
}
});
}
protected void showCurrentLocation() {
Location location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
String message = String.format(
"Current Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude());
Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG)
.show();
}
}
private class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
String message = String.format(
"New Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude());
Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG)
.show();
}
public void onStatusChanged(String s, int i, Bundle b) {
Toast.makeText(MainActivity.this, "Provider status changed",
Toast.LENGTH_LONG).show();
}
public void onProviderDisabled(String s) {
Toast.makeText(MainActivity.this,
"Provider disabled by the user. GPS turned off",
Toast.LENGTH_LONG).show();
}
public void onProviderEnabled(String s) {
Toast.makeText(MainActivity.this,
"Provider enabled by the user. GPS turned on",
Toast.LENGTH_LONG).show();
}
}
}
Ensure that you have given all the required permissions in AndroidManifest file to access gps...
android.permission.ACCESS_COARSE_LOCATION
android.permission.ACCESS_FINE_LOCATION
android.permission.READ_PHONE_STATE
android.permission.INTERNET
See this link : https://sites.google.com/site/androidhowto/how-to-1/using-the-gps

GPS continuosly localize in version 2.3.5 (not in 2.2)

I have a program that employs GPS to localize a person every 2 minutes. For that, the code is:
private boolean flagLocalizacion = false;
private LocationListener locationListener;
private Location ultimaLocalizacion;
private LocationManager locationManager;
#Override
public void onCreate(Bundle savedInstanceState) {
locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
if (location!=null) {
if(ultimaLocalizacion == null && flagLocalizacion) {
ultimaLocalizacion = location;
Toast.makeText(getApplicationContext(), "Longitude: " + location.getLongitude() + "Latitude: " + location.getLatitude(), Toast.LENGTH_SHORT).show();
} else if ((ultimaLocalizacion.getLatitude() != location.getLatitude()
|| ultimaLocalizacion.getLongitude() != location.getLongitude()) && flagLocalizacion) {
ultimaLocalizacion = location;
Toast.makeText(getApplicationContext(), "Longitude: " + location.getLongitude() + "Latitude: " + location.getLatitude(), Toast.LENGTH_SHORT).show();
}
}
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {
System.out.println("gps");
}
public void onProviderDisabled(String provider) {
System.out.println("No gps");
}
}; Button botonComenzar = (Button) findViewById(R.id.bComenzar);
botonComenzar.setOnClickListener(new OnClickListener() {
#SuppressWarnings("static-access")
public void onClick(View v) {
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER, 12000, 30, locationListener);
Toast.makeText(getApplicationContext(), "Activada Localizacion", Toast.LENGTH_SHORT).show();
flagLocalizacion = true;
}
});
My problem is:
This applicaction works correctly in version 2.2. But the software failed when I installed it in a phone that has 2.3.5., the The fail is that localice continually, not every 2 minutes. Do you have thoughts on why that is happening?
Thanks you.
Continuous GPS refresh (i.e., with an interval between GPS updates of 1 sec), regardless of the requested minTime, is a known issue with many Android phones prior to JellyBean.
Full discussion of this issue with the Android team is here:
https://android-review.googlesource.com/#/c/34230/
A new CTSVerifier test has been added in Android 4.1 JellyBean that should prevent this from happening on JellyBean and higher.
For devices pre-Jelly Bean that are affected by this, your only option for a workaround is to manually unregister and re-register the LocationListener at whatever time interval you want GPS to refresh.

Unable to find user location using GPS

public class LocationtesterActivity extends Activity {
/** Called when the activity is first created. */
LocationManager locManager;
// LocationListener locListener;
Button b;
String provider;
TextView lat, alt, longi;
// Here i am first checking if both GPS and network options are enabled in Lovation and Security Settings or not.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b = (Button) findViewById(R.id.button1);
lat = (TextView) findViewById(R.id.lattitude);
alt = (TextView) findViewById(R.id.altitude);
longi = (TextView) findViewById(R.id.longitude);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
showCurrentLocation();
}
});
locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
provider = locManager.getBestProvider(criteria, true);
System.out.println("best provider is :" + provider);
if (!locManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
|| !locManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
System.out.println("in provider enablement");
createGpsDisabledAlert();
}
else {
System.out.println("in location update request");
locManager.requestLocationUpdates(provider, 0, 0,
new MyLocationListener());
}
}
// for displaying the Dialogue Box
// if GPS or network not enabled
// and also taking to location and security screen
private void createGpsDisabledAlert() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(
"Your GPS or network provider is disabled! Would you like to enable it?")
.setCancelable(false)
.setPositiveButton("Enable provider",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
showGpsOptions();
}
});
builder.setNegativeButton("Do nothing",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
private void showGpsOptions() {
Intent gpsOptionsIntent = new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(gpsOptionsIntent, 5);
}
// //////ends/////
// Code to check whether user enabled GPS and Network provider in settings
// if not then show the dialogue box again
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 5 && resultCode == 0) {
if (!locManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
createGpsDisabledAlert();
// Toast.makeText(this,
// "provider not enabled. Click the button for settings",
// 2000).show();
} else {
Intent i=new Intent(this, LocationtesterActivity.class);
startActivity(i);
Toast.makeText(this, "User has enabled the provider", 1000)
.show();
}
}
}
// Method to display the current location
protected void showCurrentLocation() {
Location location = locManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
String message = String.format(
"Current Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude()
);
lat.setText("" + location.getLatitude());
longi.setText("" + location.getLongitude());
Toast.makeText(LocationtesterActivity.this, message,
Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(LocationtesterActivity.this, "no last known location", 1000).show();
}
}
// Inner class for LocationListener
private class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
System.out.println("in location changed");
String message = String.format(
"New Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude()
);
lat.setText("" + location.getLatitude());
longi.setText("" + location.getLongitude());
// alt.setText("" + location.getAltitude());
Toast.makeText(LocationtesterActivity.this, message,
Toast.LENGTH_LONG).show();
}
public void onStatusChanged(String s, int i, Bundle b) {
Toast.makeText(LocationtesterActivity.this,
"Provider status changed",
Toast.LENGTH_LONG).show();
}
public void onProviderDisabled(String s) {
Toast.makeText(LocationtesterActivity.this,
"Provider disabled by the user. GPS turned off",
Toast.LENGTH_LONG).show();
}
public void onProviderEnabled(String s) {
Toast.makeText(LocationtesterActivity.this,
"Provider enabled by the user. GPS turned on",
Toast.LENGTH_LONG).show();
}
}
}
I am using the above code to find the User Location.
1.I am getting always GPS as the best provider according to my criteria but that is fine to me.
2.Thing is that why am i not able to get any location values ?
Even with Location location = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); i am not getting any thing.
Please help me in getting the location values.
GPS take some time due to slow and not available inside the building .You just check it outside the building.
And
Location location = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); only works if once GPS get any location previously.
Two things,
Have u set "android.permission.ACCESS_FINE_LOCATION" permission in manifest file ??
clear all data of Google map application and then try to load map for get current location on map, If Google map give your current location then your application can fetch current location.
Its good habit to use best criteria for fetching location.
sometimes devices can not read current location. I also face this problem but at that time i check Google Map / Direction application and reset my GPS.

How to determine my current location (longitude,latitude and altitude)?

I want to determine my curent position (latitude, longitude and altitude).
In my database, I have a table contents by table location (id,name,address,longitude,latitude,altitude)
How can I determine my curent position and the most near location?
I Added this in manifest.xml:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
How can i use the methods (.getlongitude(); .getLatitude(); .getaltitude()
In documentation this is not clear.
Thank you in advance.
You can choose to either get the last known location, or listen for updates. The easiest way is to get the latest:
Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
If you want an accurate location, you need to listen forupdates using requestLocationUpdates().
Also, remember to check whether the GPS device is enabled, see How do I find out if the GPS of an Android device is enabled
To find the nearest addresses, see http://developer.android.com/reference/android/location/Geocoder.html
First Enable Ur GPS.
private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000; // in Milliseconds
public static double lontitube,latitude;
protected LocationManager locationManager;
protected Location currentLocation;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MINIMUM_TIME_BETWEEN_UPDATES,
MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
new MyLocationListener()
);
}
protected void performReverseGeocodingInBackground() {
showCurrentLocation();
}
protected void showCurrentLocation() {
currentLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (currentLocation != null) {
String message = String.format(
"Current Location \n Longitude: %1$s \n Latitude: %2$s",
currentLocation.getLongitude(), currentLocation.getLatitude()
);
lontitube=currentLocation.getLongitude();
latitude=currentLocation.getLatitude();
System.out.println("----lati----longi----"+latitude+"\n"+lontitube);
Toast.makeText(Demo.this, message,
Toast.LENGTH_LONG).show();
}
}
private class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
String message = String.format(
"New Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude()
);
Toast.makeText(Demo.this, message, Toast.LENGTH_LONG).show();
}
public void onStatusChanged(String s, int i, Bundle b) {
Toast.makeText(Demo.this, "Provider status changed",
Toast.LENGTH_LONG).show();
}
public void onProviderDisabled(String s) {
Toast.makeText(Demo.this,
"Provider disabled by the user. GPS turned off",
Toast.LENGTH_LONG).show();
}
public void onProviderEnabled(String s) {
Toast.makeText(Demo.this,
"Provider enabled by the user. GPS turned on",
Toast.LENGTH_LONG).show();
}
}
In AndroidManifest add
ACCESS_FINE_LOCATION
permission for GPS use.
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Categories

Resources