onLocationChanged is not working on real device - android

Now I am working with location based app and my problem is that the location is not updated on the real device. That is the onLocationChanged not working on real device. But fine on emulator.
locmngr=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
loclis =new MyLocationListner();
our_location=locmngr.getLastKnownLocation(LocationManager.GPS_PROVIDER);
locmngr.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0,loclis);
public class MyLocationListner implements LocationListener{
#Override
public void onLocationChanged(Location location)
{
// TODO Auto-generated method stub
L1=location.getLatitude();
L2=location.getLongitude();
String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
// Toast.makeText(getApplicationContext(), currentDateTimeString, Toast.LENGTH_SHORT).show();
DatabaseHandlerActivity db = new DatabaseHandlerActivity(getApplicationContext());
db.addLocation(new Locations_viewer(L1.toString(), L2.toString(), currentDateTimeString));
Toast.makeText(getApplicationContext(), "New Location "+"Latitude ="+L1+
" Longitude ="+L2, Toast.LENGTH_LONG).show();
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedDate = df.format(c.getTime());
ParseUser.getCurrentUser().put("Latitude", L1.toString());
ParseUser.getCurrentUser().put("Longitude", L2.toString());
ParseUser.getCurrentUser().put("Time", formattedDate);
ParseUser.getCurrentUser().saveInBackground();
// Toast.makeText(getApplicationContext(), "Latitude ="+L1, Toast.LENGTH_LONG).show();
// Toast.makeText(getApplicationContext(), "Longitude ="+L2, Toast.LENGTH_LONG).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
}
}
Please help me.

I just used NETWORK_PROVIDER instead of GPS_PROVIDER. Because GPS is not available in all locations. In my case it solved my problem perfectly. We just need an active internet connection in our mobile.

Related

How to find user location based on EditText value

I am trying to get user's city from EditText and then check if it is valid or not. If there is a little typo, then I want to correct it on the EditText and then show it back to user.
I have been searching for this for quite some time but what I am getting is to get user current location and then get its location according to lat and long values. But I don't want to do this.
Here's what I am doing right now, but I don't want to do it this way
userDataFrag.loc.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 0, 0, 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
userLat = location.getLatitude();
userLong = location.getLongitude();
Geocoder geocoder = new Geocoder(getActivity(),
Locale.getDefault());
List<Address> addresses;
try {
addresses = geocoder.getFromLocation(userLat,
userLong, 1);
CityName = addresses.get(0).getAddressLine(0);
StateName = addresses.get(0).getAddressLine(1);
CountryName = addresses.get(0).getAddressLine(2);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
Can any one suggests me a better way to do it?

Toast doesn't disappear from screen

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.

getLastknownLocation() returns null value on nexus

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
}

Issue with LocationListener Network_provider in android

I have an app that implements LocationListener using Network_Provider and onLocationChanged(), I am logging the new location in a log file with timestamp. This app is run as a service.
I am seeing unusual behavior with this :
it seems to be capturing location from a wi-fi location only ( no cellular network provider )
once the service is started, it waits till it finds the first wi-fi location. Until then it does not log anything.
once it finds the location thru wi-fi, it starts logging the location latitude/longitude
this logging continues thereafter, every 45sec, eventhough the location is not changed ..
this logging continues thereafter, every 45 sec, eventhough the device is moved out of range of that wi-fi, in fact, from my home to work # 20 miles (no wi-fi at work) and it still continues to log the same location every 45 sec .
wierd ?
Here is my code :
public class TestLocationService extends Service implements LocationListener {
LocationManager lm;
public TestLocationService() {
}
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
String loc= "Time : "+new Date()+"\n";
loc += "New Location "+location.getLatitude()+" and "+location.getLongitude()+"\n*******************\n";
System.out.println(loc);
try {
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File (sdCard.getAbsolutePath() + "/loc");
dir.mkdirs();
File file = new File(dir, "loc.txt");
FileOutputStream fOut = new FileOutputStream(file,true);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
osw.write(loc);
osw.flush();
osw.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
#Override
public void onCreate() {
this.lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
this.lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
}
}
-abhay
Change this:
this.lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
by:
this.lm.requestLocationUpdates(this.lm.getBestProvider(new Criteria(),true),0,0,this);
In this case your phone will use the best enabled provider

location listner do not calling onlocation change

I am using location listener in my app.
I have the code like:
public void startlistning()
{
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
Toast.makeText( getApplicationContext(),"Gps Enabled", Toast.LENGTH_SHORT).show();
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
Toast.makeText( getApplicationContext(),"Gps Disabled", Toast.LENGTH_SHORT ).show();
}
#Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
location.getLatitude();
location.getLongitude();
}
};
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
But don't know why the method onLocationChanged did not call when I restart device 1st time (without cellular data (no simcard) available). however, I am using best provider in my app.
Please look into matter. Thanks
Location listener method call when you change the location of device . Its not start when you restart device . If you want to invoke onLocationChanged method you have to change the location of device .
Please try this to get last known location.
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
Criteria locationCritera = new Criteria();
locationCritera.setAccuracy(Criteria.ACCURACY_FINE);
locationCritera.setAltitudeRequired(false);
locationCritera.setBearingRequired(false);
locationCritera.setCostAllowed(true);
locationCritera.setPowerRequirement(Criteria.NO_REQUIREMENT);
String providerName = locationManager.getBestProvider(locationCritera, true);
Location location = locationManager.getLastKnownLocation(providerName);
Log.i("--- Latitude",""+location.getLatitude());
Log.i("--- Latitude",""+location.getLongitude());

Categories

Resources