Android GPS: "NO Location Found" - android

For some unclear reason I am not getting any locations, and onLocationChanged() is not invoked.
package com.example.gpsexample;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.Menu;
public class GPS_Location extends Activity implements LocationListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("~~~","~~~ GPS_Location onCreate");
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
Location l1 = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Location l2 = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
Log.d("~~~","~~~ GPS_Location getLastKnownLocation ==> "+l1+" "+l2);
r.run();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public void onLocationChanged(Location location) {
Log.d("~~~","~~~ onLocationChanged "+location);
int latitude = (int) (location.getLatitude());
int longitude = (int) (location.getLongitude());
Log.i("~~~", "### Latitude: " + latitude + ", Longitude: " + longitude+"\n\n\n###");
}
#Override
public void onProviderDisabled(String provider) {
Log.d("~~~","~~~ onProviderDisabled"+provider);
}
#Override
public void onProviderEnabled(String provider) {
Log.d("~~~","~~~ onProviderEnabled "+provider);
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d("~~~","~~~ "+provider+" "+status+" "+extras);
}
final Handler handler = new Handler();
Runnable r = new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Log.d("~~~","~~~ GPS_Location run");
Location l = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Log.d("~~~","GPS enabled: "+locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER));
Log.d("~~~","~~~ GPS_Location getLastKnownLocation ==> "+l);
l = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
Log.d("~~~","Network enabled: "+locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER));
Log.d("~~~","~~~ Network_Location getLastKnownLocation ==> "+l);
handler.postDelayed(this, 5000);
}
};
}
the permissions are:
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
/>
<uses-permission
android:name="android.permission.ACCESS_COARSE_LOCATION"
/>
<uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION"
/>
<uses-permission
android:name="android.permission.READ_PHONE_STATE"
/>
<uses-permission
android:name="android.permission.ACCESS_NETWORK_STATE"
/>
<uses-permission
android:name="android.permission.INTERNET"
/>
and the output is:
D/~~~ (29852): ~~~ GPS_Location run
D/~~~ (29852): GPS enabled: true
D/~~~ (29852): ~~~ GPS_Location getLastKnownLocation ==> null
D/~~~ (29852): Network enabled: true
D/~~~ (29852): ~~~ Network_Location getLastKnownLocation ==> null
When I change the location settings, I do see onProviderEnabled() calls.
How do I get a location instead of null?

// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
Update Location
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Please Update In Your Code
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000 * 60 * 1, 10, this);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000 * 60 * 1, 10, this)

Related

Unable to get GPS Location with Common Code

I have code to get location, which I believe must be working but it doesn't at all:
Manifest:
<uses-permission android:name="android.permission.INTERNET" />
<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" />
Java:
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
0,
0,
new MyLocationListener()
);
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 = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
System.out.println("location: " + location);
location prints null and the listener never get called at all. Anyone can advise what's wrong? If I open Google Maps, it can return my location anyway.
Note: I am testing with my device
Create Class SingleShotLocationProvider in your utility package or wherever you are fine with below code.
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.widget.Toast;
public class SingleShotLocationProvider {
public interface LocationCallback {
void onNewLocationAvailable(Location location);
}
// calls back to calling thread, note this is for low grain: if you want higher precision, swap the
// contents of the else and if. Also be sure to check gps permission/settings are allowed.
// call usually takes <10ms
public static void requestSingleUpdate(final Context context, final LocationCallback callback) {
final LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
if (Utility.checkAndRequestPermissionsForGPS(context)) {
if (ActivityCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(context, "First enable LOCATION ACCESS in settings.", Toast.LENGTH_LONG).show();
return;
}
boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (isGPSEnabled) {
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
locationManager.requestSingleUpdate(criteria, new LocationListener() {
#Override
public void onLocationChanged(Location location) {
callback.onNewLocationAvailable(location);
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onProviderDisabled(String provider) {
}
}, null);
} else {
boolean isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (isNetworkEnabled) {
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
locationManager.requestSingleUpdate(criteria, new LocationListener() {
#Override
public void onLocationChanged(Location location) {
callback.onNewLocationAvailable(location);
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onProviderDisabled(String provider) {
}
}, null);
}
}
}
}
}
Now in you Activity import it like
import com.yourdomain.utils.SingleShotLocationProvider;
Now you can use it like below this is reference to your activity
SingleShotLocationProvider.requestSingleUpdate(this,
new SingleShotLocationProvider.LocationCallback() {
#Override
public void onNewLocationAvailable(Location location) {
if (location != null) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
}
}
}
});
You will receive location in first attempt just make sure GPS is on and set to high accuracy.And in manifest you have given necessary permission as like below -
<uses-permission android:name="android.permission.INTERNET" />
<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" />
Try this code. I am using the GPS here to get the latitude and longitude.
Criteria criteria = new Criteria();
// Getting the name of the best provider
provider = locationManager.getBestProvider(criteria, true);
// Getting Current Location
location = locationManager.getLastKnownLocation(provider);
if (location != null) {
System.out.println("Get Last LOcation" + location.getLatitude()
+ location.getLongitude());
loc.setLatitude(location.getLatitude());
loc.setLongitude(location.getLongitude());
}
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) {
// TODO Auto-generated method stub
}
#Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
if (location.getAccuracy() < 50) {
locationManager.removeUpdates(locationListener);
}
}
};
locationManager.requestLocationUpdates(provider,
INTERVAL_TIME_SECONDS, MIN_DISTANCE_METERS,
locationListener);

LocationManager minTime won't work properly

I want to get user's location every 5 minutes, so I created a Service with the following codes:
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
public class Tracker extends Service {
private SharedPreferences prefs;
private LocationManager locationManager;
private long minTime = 300000; // miliseconds
private float minDistance = 10; // meters
private boolean gpsStatus;
private boolean networkStatus;
#Override
public void onCreate() {
Log.i("Location2", "Location: Created");
super.onCreate();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("Location2", "Location: Started");
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
gpsStatus = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
networkStatus = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
locationManager.requestLocationUpdates(getProviderName(), minTime, minDistance, locationListener);
return Service.START_STICKY;
}
private void resetLocationProvider() {
Log.e("Location2", "Provider Reset");
deleteLocationListener();
locationManager.requestLocationUpdates(getProviderName(), minTime, minDistance, locationListener);
}
private void deleteLocationListener() {
locationManager.removeUpdates(locationListener);
}
private LocationListener locationListener = new LocationListener() {
#Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
}
#Override
public void onProviderEnabled(String provider) {
resetLocationProvider();
Log.e("Location2", "Provider Enabled: " + provider);
}
#Override
public void onProviderDisabled(String provider) {
resetLocationProvider();
Log.e("Location2", "Provider Disabled: " + provider);
}
#Override
public void onLocationChanged(Location location) {
Log.i("Location2", "Location Time: " + TimeToDate.getDate(location.getTime(), "yyyy-MM-dd HH:mm:ss"));
Log.i("Location2", "Location Provider: " + location.getProvider());
Log.i("Location2", "Location Accuracy: " + location.getAccuracy());
Log.i("Location2", "Location Latitude: " + location.getLatitude());
Log.i("Location2", "Location Longitude: " + location.getLongitude());
if (gpsStatus != locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
gpsStatus = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
resetLocationProvider();
return;
}
if (networkStatus != locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
networkStatus = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
resetLocationProvider();
}
}
};
private String getProviderName() {
LocationManager locationManager = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setPowerRequirement(Criteria.POWER_MEDIUM); // Chose your desired power consumption level.
criteria.setAccuracy(Criteria.ACCURACY_FINE); // Choose your accuracy requirement.
criteria.setSpeedRequired(false); // Chose if speed for first location fix is required.
criteria.setAltitudeRequired(false); // Choose if you use altitude.
criteria.setBearingRequired(false); // Choose if you use bearing.
criteria.setCostAllowed(false); // Choose if this provider can waste money :-)
// Provide your criteria and flag enabledOnly that tells
// LocationManager only to return active providers.
return locationManager.getBestProvider(criteria, true);
}
#Override
public void onDestroy() {
deleteLocationListener();
super.onDestroy();
};
#Override
public IBinder onBind(Intent arg0) {
return null;
}
}
As is seen, I've used a minTime of 300,000 ms, But I got a location every about 20 seconds rather than every 5 minutes. As documentation says:
The elapsed time between location updates will never be less than
minTime, although it can be more depending on the Location Provider
implementation and the update interval requested by other applications
And also I know, an active provider is not a good choice for a background process. However, what is wrong with my code?
Based on this answer, I changed my codes to:
#Override
public void onLocationChanged(Location location) {
if (location != null) {
if (location.getProvider().equals(LocationManager.GPS_PROVIDER) && android.os.Build.VERSION.SDK_INT < 16) {
Log.i("TEST", "DATE: " + TimeToDate.getDate(location.getTime(), "yyyy-MM-dd HH:mm:ss"));
Log.i("TEST", "LAT: " + location.getLatitude());
Log.i("TEST", "LONG: " + location.getLongitude());
locationManager.removeUpdates(locationListener);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
locationManager.requestLocationUpdates(getProviderName(), minTime, minDistance, locationListener);
}
}, minTime);
}
...
And now it works.

get current location in android

I have tried following code but not getting current location.
When I manual set location at emulator control then I get such location but not getting current location .I get null location.
How to get current location?
Is there any other way to get current location?
This is my code:
package com.p;
import android.app.Activity;
import android.os.Bundle;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class GooglemapActivity extends Activity implements LocationListener {
private TextView latituteField;
private TextView longitudeField;
private LocationManager locationManager;
private String provider;
EditText t;
#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();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);//true if required
criteria.setBearingRequired(false);//true if required
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
provider = locationManager.getBestProvider(criteria, true);
//provider=LocationManager.GPS_PROVIDER;
Location location = locationManager.getLastKnownLocation(provider);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
/*check provder*/boolean statusOfGPS =locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if ( statusOfGPS==true) {
// Initialize the location fields
if (location != null) {
System.out.println("Provider " + provider + " has been selected.");
double lat = (double) (location.getLatitude());
double lng = (double) (location.getLongitude());
latituteField.setText(Double.toString(lat));
longitudeField.setText(Double.toString(lng));
} else {
latituteField.setText("Provider is not available");
longitudeField.setText("Provider not available");
}
}
else
{
latituteField.setText("eeeeeeeee");
longitudeField.setText("rrrrrrrrr");
}
}
/* 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);
}
public void onLocationChanged(Location location) {
double lat = (double) (location.getLatitude());
double lng = (double) (location.getLongitude());
latituteField.setText(Double.toString(lat));
longitudeField.setText(Double.toString(lng));
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
Toast.makeText(this, "Enabled new provider " + provider,
Toast.LENGTH_SHORT).show();
}
public void onProviderDisabled(String provider) {
Toast.makeText(this, "Disabled provider " + provider,
Toast.LENGTH_SHORT).show();
}
}
I have added permissions in manifest.xml as below:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
You won't get current location in emulator. You can set them through DDMS, and they will be your current location in case of a emulator.
Please, don't try to get last-known-location, most of the time it's stale and useless. Set up locationListener instead and wait for the location updates. This will get you your current location.
You may use this code to start listing for GPS updates and do something with the data you receive. It also prints messages in the log file for easy debugging. Please, note, this function does not return any results, it only starts listening to GPS. Results will be provided some time later in // do something here with the new location data part, when you may save them somewhere.
private void getGpsData() {
locationManager = (LocationManager) owner.getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
Log.i(TAG, "location change:" + location.toString());
String longitude = "Londitude: " + location.getLongitude();
String latitude = "Latitude: " + location.getLatitude();
if( location.hasAccuracy() ) { // good enough?
// do something here with the new location data
....
//
Log.i(TAG, "GPS listener done");
locationManager.removeUpdates(this); // don't forget this to save battery
}
}
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.i(TAG, provider + " status:" + status);
}
public void onProviderEnabled(String provider) {
Log.i(TAG, provider + " enabled");
}
public void onProviderDisabled(String provider) {
Log.i(TAG, provider + " disabled");
}
};
Log.i(TAG,"GPS listener started");
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener );
}

Get Gps Location Using Android

I want to get latitude and Longitude from the following Android code.
location = locationManager.getLastKnownLocation(provider);
Here i am getting error location is null.
Here is the code:
package com.p;
import android.app.Activity;
import android.os.Bundle;
import android.os.Bundle;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
public class GooglemapActivity 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.");
int lat = (int) (location.getLatitude());
int lng = (int) (location.getLongitude());
latituteField.setText(String.valueOf(lat));
longitudeField.setText(String.valueOf(lng));
} else {
latituteField.setText("Provider not available");
longitudeField.setText("Provider 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);
}
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));
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
Toast.makeText(this, "Enabled new provider " + provider,
Toast.LENGTH_SHORT).show();
}
public void onProviderDisabled(String provider) {
Toast.makeText(this, "Disabled provider " + provider,
Toast.LENGTH_SHORT).show();
}
}
I have added the following permisions:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
But I am getting the following error:
Provider not available
If the last known location is null, it's because the phone has no last known location. If there is no location cached, you need to request an updated location, either via network, or GPS. Note that this process can take time, so must be done asynchronously.
You need to read this document:
http://developer.android.com/guide/topics/location/obtaining-user-location.html
I also recommend reading this:
http://android-developers.blogspot.co.uk/2011/06/deep-dive-into-location.html
You haven't define a valid Criteria. Define that as follows
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);//true if required
criteria.setBearingRequired(false);//true if required
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
provider = locationManager.getBestProvider(criteria, true);//search for enabled provider
have you switched on GPS in Settings -> Location ?
why do you use (int) (location.getLatitude()) -- latitude/longitude is a float
Think you need to specify from which provider you want to receive updates.
You have two options:
GPS_PROVIDER
NETWORK_PROVIDER
You're probably missing something like this:
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
I think you may want to add the following permission as well, since I am pretty sure that the FINE relies on the COARSE...
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

How can I get location without internet in android, using only GPS

I want to get location using GPS only. I don't want to use internet and GPRS in this application. My code is below; tell me where I'm wrong in this.
code:
package com.getlocation;
import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
public class UseGps extends Activity {
/** Called when the activity is first created. */
private String provider;
LocationManager locationManager;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); /*
* Use the LocationManager class to
* obtain GPS locations
*/
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
criteria.setAccuracy(Criteria.ACCURACY_FINE);
provider = locationManager.getBestProvider(criteria, true);
locationManager.requestLocationUpdates(provider, 61000, 250,
mlocListener);
} /* Class My Location Listener */
public class MyLocationListener implements LocationListener {
#Override
public void onLocationChanged(Location loc) {
loc.getLatitude();
loc.getLongitude();
String Text = "My current location is: " + "Latitude = "
+ loc.getLatitude() + "Longitude = " + loc.getLongitude();
Toast.makeText(getApplicationContext(), Text, Toast.LENGTH_SHORT)
.show();
Log.d("TAG", "Starting..");
}
#Override
public void onProviderDisabled(String provider) {
Toast.makeText(getApplicationContext(), "Gps Disabled",
Toast.LENGTH_SHORT).show();
}
#Override
public void onProviderEnabled(String provider) {
Toast.makeText(getApplicationContext(), "Gps Enabled",
Toast.LENGTH_SHORT).show();
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}/* End of Class MyLocationListener */
}/* End of UseGps Activity */
Use this for only GPS Provider, it does not need GPRS.
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
You need to put the permission in manifest file.
You don't need an internet connection to run GPS system in your mobile. GPS time synchronization does not require an Internet connection. But if you want to show the current location on google map, you may require internet connection.
Coming to you code everything looks fine for me.
Try this code in your activity.
LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new YourLocationListener(getApplicationContext(), mobileNo, deviceId);
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,mlocListener);
and include this in androidmanifest.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" />

Categories

Resources