Android GPS Location- It doesn't go to onLocationChanged - android

I want to get the GPS location value.I have wrote code.But i didn't show message. I have tested in deveice not emulator.Device contain GPS & its enable. & there is no internet connection also.
Please tell me what is wrong in my code
public class AndroidGPSSampleActivity extends Activity {
private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 30000; // in Milliseconds
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,
MINIMUM_TIME_BETWEEN_UPDATES,
MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
new MyLocationListener()
);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, 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(AndroidGPSSampleActivity.this, "Sample test",
Toast.LENGTH_LONG).show();
Toast.makeText(AndroidGPSSampleActivity.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(AndroidGPSSampleActivity.this, message, Toast.LENGTH_LONG).show();
}
public void onStatusChanged(String s, int i, Bundle b) {
Toast.makeText(AndroidGPSSampleActivity.this, "Provider status changed",Toast.LENGTH_LONG).show();
}
public void onProviderDisabled(String s) {
Toast.makeText(AndroidGPSSampleActivity.this,"Provider disabled by the user. GPS turned off",Toast.LENGTH_LONG).show();
}
public void onProviderEnabled(String s) {
System.out.println("==onProviderEnabled=" + s);
Toast.makeText(AndroidGPSSampleActivity.this, "Provider enabled by the user. GPS turned on",Toast.LENGTH_LONG).show();
}
}
and permission
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
always Location give null value. It didn't go to onLocationChanged method...
Please help me out from this issue...

I adapt where am i tutorial.. hope it would help
p.s. I write it without testing .. technically,it would work.. i guess
private Location loca;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager locman;
String context = Context.LOCATION_SERVICE;
locman = (LocationManager)getSystemService(context);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
String provider = locman.getBestProvider(criteria, true);
loca = locman.getLastKnownLocation(provider);
updateWithNewLocation(loca);
locman.requestLocationUpdates(provider, 1000, 1, locationListener);
retrieveLocationButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String message = "My location : " + updateWithNewLocation(loca);
Toast.makeText(AndroidGPSSampleActivity.this, message,
Toast.LENGTH_LONG).show();
}
});
}
private final LocationListener 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
}
#Override
public void onProviderDisabled(String provider) {
updateWithNewLocation(null);
}
#Override
public void onLocationChanged(Location location) {
updateWithNewLocation(location);
}
};
private string updateWithNewLocation(Location location){
if(location!=null){
double lat = location.getLatitude();
double lng = location.getLongitude();
latLongString = "Lat:" + lat + "\nLong:" + lng;
}else{
latLongString = "No location found";
}
return latLongString;
}

I changed some of your code please check this
public class AndroidGPSSampleActivity extends Activity implements LocationListener{
private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 30000; // in Milliseconds
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);
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
locationManager.requestLocationUpdates(locationManager.NETWORK_PROVIDER, 500L, 250.0f, this);
retrieveLocationButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showCurrentLocation();
}
});
}
#Override
public void onPause() {
super.onPause();
locationManager.removeUpdates(this);
}
#Override
public void onDestroy(){
locationManager.removeUpdates(this);
super.onDestroy();
}
protected void showCurrentLocation() {
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location == null){
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
if (location != null) {
String message = String.format(
"Current Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude()
);
Toast.makeText(AndroidGPSSampleActivity.this, "Sample test",
Toast.LENGTH_LONG).show();
Toast.makeText(AndroidGPSSampleActivity.this, message,
Toast.LENGTH_LONG).show();
}
}
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
if (location != null){
String message = String.format("New Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude());
Toast.makeText(AndroidGPSSampleActivity.this, message, Toast.LENGTH_LONG).show();
}
}
public void onStatusChanged(String s, int i, Bundle b) {
Toast.makeText(AndroidGPSSampleActivity.this, "Provider status changed",Toast.LENGTH_LONG).show();
}
public void onProviderDisabled(String s) {
Toast.makeText(AndroidGPSSampleActivity.this,"Provider disabled by the user. GPS turned off",Toast.LENGTH_LONG).show();
}
public void onProviderEnabled(String s) {
System.out.println("==onProviderEnabled=" + s);
Toast.makeText(AndroidGPSSampleActivity.this, "Provider enabled by the user. GPS turned on",Toast.LENGTH_LONG).show();
}
}

Related

Android Gps Disable Updates

How do I disable location updates in onLocationChanged() after clicking on stop button also it is continuously updating the location.
I have used the code mentioned here.
Please help me out how to solve this.
public class AndroidGPSSampleActivity extends Activity {
private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 30000; // in Milliseconds
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,
MINIMUM_TIME_BETWEEN_UPDATES,
MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
new MyLocationListener()
);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, 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(AndroidGPSSampleActivity.this, "Sample test",
Toast.LENGTH_LONG).show();
Toast.makeText(AndroidGPSSampleActivity.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(AndroidGPSSampleActivity.this, message, Toast.LENGTH_LONG).show();
}
public void onStatusChanged(String s, int i, Bundle b) {
Toast.makeText(AndroidGPSSampleActivity.this, "Provider status changed",Toast.LENGTH_LONG).show();
}
public void onProviderDisabled(String s) {
Toast.makeText(AndroidGPSSampleActivity.this,"Provider disabled by the user. GPS turned off",Toast.LENGTH_LONG).show();
}
public void onProviderEnabled(String s) {
System.out.println("==onProviderEnabled=" + s);
Toast.makeText(AndroidGPSSampleActivity.this, "Provider enabled by the user. GPS turned on",Toast.LENGTH_LONG).show();
}
}
I'll suggest you to make MyLocationListener as class field and later you can access it from any inner class. You can disable updates for a specific listener by calling removeUpdates method of LocationManager class.
public class AndroidGPSSampleActivity extends Activity {
private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1;
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 30000;
private MyLocationListener listener = new MyLocationListener();
...
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, listener);
Updates can be stopped in any place of code by using the following:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.removeUpdates(listener);
}
});

Issue in finding the lat and long in samsung S3

I am not getting the lat and long in samsung S3.In other devices my code is working fine and getting the proper lat and long.Below is my code to get the lat and long.
public class MainActivity extends Activity {
TextView textView1;
Location currentLocation;
double currentLatitude,currentLongitude;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView1 = (TextView) findViewById(R.id.textView1);
findLocation();
}
public void findLocation() {
LocationManager locationManager = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
updateLocation(location,currentLatitude,currentLongitude);
Toast.makeText(
MainActivity.this,
String.valueOf(currentLatitude) + "\n"
+ String.valueOf(currentLongitude), 5000)
.show();
}
public void onStatusChanged(String provider, int status,
Bundle extras) {
}
public void onProviderEnabled(String provider) {
}
public void onProviderDisabled(String provider) {
}
};
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
}
void updateLocation(Location location,double currentLatitude,double currentLongitude) {
currentLocation = location;
this.currentLatitude = currentLocation.getLatitude();
this.currentLongitude = currentLocation.getLongitude();
textView1.setText(String.valueOf(this.currentLatitude) + "\n"
+ String.valueOf(this.currentLongitude));
}
}
In above code the onLocationChanged() method is not executing in S3 and the same code is not working in Samsung S3 device.In other devices it working fine.I am getting the correct lat and long in other devices.Please help me to sort out this problem..

GPS not working when activity resumed

I need to calculate the current latiude and longitude of the user before he logs in. I have tested my code in my mobile device but it does not seem to work. Here is my code :
LocationManager mlocManager=null;
LocationListener mlocListener=null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
mlocListener = new MyLocationListener();
}
#Override protected void onResume() {
super.onResume();
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES, mlocListener);
}
#Override protected void onPause() {
super.onPause();
mlocManager.removeUpdates(mlocListener); //<8>
}
/* Class My Location Listener */
public class MyLocationListener implements LocationListener
{
#Override
public void onLocationChanged(Location loc)
{
Toast.makeText(getApplicationContext(),"In onchange", Toast.LENGTH_SHORT).show();
if(loc!=null){
latitude=loc.getLatitude();
longitude=loc.getLongitude();
if(loc.getLatitude()!=0.0 || loc.getLongitude()!=0.0){
Toast.makeText(getApplicationContext(),"Location not null", Toast.LENGTH_SHORT).show();
SharedPreferences prefsSaveLatLong = context.getSharedPreferences("prefsSaveLatLong",Context.MODE_PRIVATE);
SharedPreferences.Editor e = prefsSaveLatLong.edit();
e.remove("LAT");
e.remove("LONG");
e.putString("LAT",Double.toString(loc.getLatitude()));
e.putString("LONG",Double.toString(loc.getLongitude()));
e.commit();
String Text = "My current location is: " + "Latitude = " + loc.getLatitude() + "Longitude = " + loc.getLongitude();
Toast.makeText(getApplicationContext(),Text+" "+latitude+" "+longitude, Toast.LENGTH_SHORT).show();
}else{
SharedPreferences prefsSaveLatLong = context.getSharedPreferences("prefsSaveLatLong",Context.MODE_PRIVATE);
if(prefsSaveLatLong.contains("LAT") && prefsSaveLatLong.contains("LONG")){
SharedPreferences.Editor e1 = prefsSaveLatLong.edit();
e1.remove("LAT");
e1.remove("LONG");
e1.commit();
}
}
// set latitude longitude to label
setLatLongLabel();
}else{
latLongLabel.setTextColor(Color.parseColor("#FF0000"));
latLongLabel.setText("Latitude-Longitude not available");
SharedPreferences prefsSaveLatLong = context.getSharedPreferences("prefsSaveLatLong",Context.MODE_PRIVATE);
if(prefsSaveLatLong.contains("LAT") && prefsSaveLatLong.contains("LONG")){
SharedPreferences.Editor e1 = prefsSaveLatLong.edit();
e1.remove("LAT");
e1.remove("LONG");
e1.commit();
}
}
}
#Override
public void onProviderDisabled(String provider)
{
gpsEnabled=false;
if(!gpsEnabled){
Toast.makeText( getApplicationContext(),"Gps Disabled", Toast.LENGTH_SHORT ).show();
showSettingsAlert();
}
}
#Override
public void onProviderEnabled(String provider)
{
gpsEnabled=true;
Toast.makeText(getApplicationContext(),"Gps Enabled",Toast.LENGTH_SHORT).show();
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
if(status==0){
Toast.makeText(getApplicationContext(),"OUT_OF_SERVICE",Toast.LENGTH_SHORT).show();
SharedPreferences prefsSaveLatLong = context.getSharedPreferences("prefsSaveLatLong",Context.MODE_PRIVATE);
if(prefsSaveLatLong.contains("LAT") && prefsSaveLatLong.contains("LONG")){
SharedPreferences.Editor e1 = prefsSaveLatLong.edit();
e1.remove("LAT");
e1.remove("LONG");
e1.commit();
}
}
else if(status==1){
Toast.makeText(getApplicationContext(),"TEMPORARILY_UNAVAILABLE",Toast.LENGTH_SHORT).show();
}else if(status==2){
Toast.makeText(getApplicationContext(),"AVAILABLE",Toast.LENGTH_SHORT).show();
}
}
}/* End of Class MyLocationListener */enter code here
`
Try some thing like this
public class ShowLocationActivity extends Activity implements LocationListener {
private TextView latituteField;
private TextView longitudeField;
private LocationManager locationManager;
private String provider;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
latituteField = (TextView) findViewById(R.id.TextView02);
longitudeField = (TextView) findViewById(R.id.TextView04);
// Get the location manager
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Define the criteria how to select the locatioin provider -> use
// default
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
// Initialize the location fields
if (location != null) {
System.out.println("Provider " + provider + " has been selected.");
onLocationChanged(location);
} else {
latituteField.setText("Location not available");
longitudeField.setText("Location not available");
}
}
/* Request updates at startup */
#Override
protected void onResume() {
super.onResume();
locationManager.requestLocationUpdates(provider, 400, 1, this);
}
/* Remove the locationlistener updates when Activity is paused */
#Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);
}
#Override
public void onLocationChanged(Location location) {
int lat = (int) (location.getLatitude());
int lng = (int) (location.getLongitude());
latituteField.setText(String.valueOf(lat));
longitudeField.setText(String.valueOf(lng));
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
Toast.makeText(this, "Enabled new provider " + provider,
Toast.LENGTH_SHORT).show();
}
#Override
public void onProviderDisabled(String provider) {
Toast.makeText(this, "Disabled provider " + provider,
Toast.LENGTH_SHORT).show();
}
}
See this tutorial

Network location provider not giving location android

I am developing a small android application in which I want to find out the user's current location by using the network provider. I tried this in following ways but it's not giving me any output :
networklocationManager = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener networklocationListener = new LocationListener() {
public void onLocationChanged(Location location) {
Log.i("********************************",
"this is my network location " + location);
String Location_text = "NETWORK LOCATION latitude:"
+ location.getLatitude() + " longitude:"
+ location.getLatitude();
network_location.setText(Location_text);
}
public void onStatusChanged(String provider, int status,
Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
// Register the listener with the Location Manager to receive location
// updates
networklocationManager
.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
networklocationListener);
I gave permissions in my manifest file like this
<uses-permission
android:name="android.permission.INTERNET" />
<uses-permission
android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION" />
Is there any thing which I am missing ? Is this the correct way? Need help. Thank you...
public class MainActivity extends Activity implements LocationListener {
private TextView latituteField;
private TextView longitudeField;
private LocationManager gpslocationManager;
private LocationManager networklocationManager;
private LocationManager networklocationManager1;
private String provider;
private TextView gps_location;
private TextView network_location;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gps_location = (TextView) findViewById(R.id.gps_location);
network_location = (TextView) findViewById(R.id.network_location);
networkLocation();
}
public void networkLocation() {
networklocationManager = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
LocationListener networklocationListener = new LocationListener() {
public void onLocationChanged(Location location) {
Log.i("********************************",
"this is my network location " + location);
String Location_text = "NETWORK LOCATION latitude:"
+ location.getLatitude() + " longitude:"
+ location.getLatitude();
network_location.setText(Location_text);
}
public void onStatusChanged(String provider, int status,
Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
networklocationManager
.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
networklocationListener);
}
}
Make sure you enable Location Services in Settings! That should be the problem. It might be disabled (and this setting will usually be found in Location and Security in Settings)
Let me know if it works!
Is your network provider enabled?
boolean network_enabled;
try {
network_enabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch(Exception ex) {
ex.printStackTrace();
}
I hope this part of the code will help you extracted from vogella.
public class ShowLocationActivity extends Activity implements LocationListener {
private TextView latituteField;
private TextView longitudeField;
private LocationManager locationManager;
private String provider;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
latituteField = (TextView) findViewById(R.id.TextView02);
longitudeField = (TextView) findViewById(R.id.TextView04);
// Get the location manager
locationManager = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
// Define the criteria how to select the locatioin provider -> use
// default
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
// Initialize the location fields
if (location != null) {
System.out.println("Provider " + provider + " has been selected.");
onLocationChanged(location);
} else {
latituteField.setText("Location not available");
longitudeField.setText("Location not available");
}
}
/* Request updates at startup */
#Override
protected void onResume() {
super.onResume();
locationManager.requestLocationUpdates(provider, 400, 1, this);
}
/* Remove the locationlistener updates when Activity is paused */
#Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);
}
#Override
public void onLocationChanged(Location location) {
int lat = (int) (location.getLatitude());
int lng = (int) (location.getLongitude());
latituteField.setText(String.valueOf(lat));
longitudeField.setText(String.valueOf(lng));
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
#Override
public void onProviderEnabled(String provider) {
Toast.makeText(this, "Enabled new provider " + provider,
Toast.LENGTH_SHORT).show();
}
#Override
public void onProviderDisabled(String provider) {
Toast.makeText(this, "Disabled provider " + provider,
Toast.LENGTH_SHORT).show();
}
}

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

Categories

Resources