73197

LocationManager not working on Android phone

Question:

I have a problem,

The locationmanager is working perfectly fine when i test it in the emulator and edit the location in the DDMS screen, but when i test it on the <strong>Samsung Galaxy SII</strong>, its nog working..

Please help me.

import android.app.Activity; import android.content.Context; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.telephony.TelephonyManager; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class LbsGeocodingActivity extends Activity { private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 0; // in Meters private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000; // 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()); retrieveLocationButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { showCurrentLocation(); } }); } public String getMyPhoneNumber(){ TelephonyManager mTelephonyMgr; mTelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); return mTelephonyMgr.getLine1Number(); } 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 \n %3$s ", location.getLongitude(), location.getLatitude(), getMyPhoneNumber()); 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 \n %3$s ", location.getLongitude(), location.getLatitude(), getMyPhoneNumber() ); 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 the permissions i have:

ACCES_COURSE_LOCATION ACCES_FINE_LOCATION

Answer1:

It could be a problem with your incorrect usage of String.format(). Sometimes this can cause weird, device-specific issues. Try,

String lon = "" + location.getLongitude(); String lat = "" + location.getLatitude(); String num = getMyPhoneNumber(); String message = String.format( "New Location \n Longitude: %1$s \n Latitude: %2$s \n %3$s", lon, lat, num ); <hr />

Have you tried using this?

LocationManager lm = (LocationManager)act.getSystemService(Context.LOCATION_SERVICE); Criteria crit = new Criteria(); crit.setAccuracy(Criteria.ACCURACY_FINE); String provider = lm.getBestProvider(crit, true); Location loc = lm.getLastKnownLocation(provider);

Answer2:

Maybe you are testing indoors, and can't get any GPS information. Try to change LocationManager.GPS_PROVIDER-->LocationManager.NETWORK_PROVIDER.

Answer3:

i had similar issues using the locationmanager on my HTC incredible S running 4.0.4. it would simply stop firing location changes on the real device after some time. on some other devices it would work. very intermittent.

my solution was to piss off the LocationManager and use LocationClient pardiagm. <a href="http://developer.android.com/reference/com/google/android/gms/location/LocationClient.html" rel="nofollow">http://developer.android.com/reference/com/google/android/gms/location/LocationClient.html</a>

ever since google play services have been introduced the Locationmanager has been intermittent on my device. I cant give any other explanation as to why it occurs.

But the LocationClient works everytime plus it removes the dumb 'provider' notion from location management.

Recommend

  • Change background color on button click?
  • Remembering user login on parse.com android?
  • PhoneStateListener doesn't call
  • Select Address from MapActivity and return back to MainActivity
  • Listview setOnItemClickListener - Not working for custom listview but working into simple listview
  • Android List Fragment with fixed Header
  • media player does not work in my code
  • is it bad to use public static fields/variables?
  • How to set (Text)View attributes programmatically?
  • Problem in Creating a View Dynamically ( in Android ) [Error : couldn't save which view …]
  • RxJava,Retrofit Error :Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
  • onActivityResult() not called after startActivityForResult() with Intent.ACTION_GET_CONTENT
  • Store additional information during registration with Firebase in Android
  • Shuffling an Array in Android
  • How to add an item to a ListView with each Button click
  • Where are files stored on an Android emulator?
  • Is Android's ARGB_8888 Bitmap internal format always RGBA?
  • android-How to enable cache or buffering when videoView have downloaded a video
  • OnItemSelectedListener for Spinner that was added programmatically does not trigger
  • Android: How to open the webview in a new screen
  • Listview - list item selection and initial setting (custom made items)
  • I have two activity.I want to Send the data from second activity to previous activity.First activity
  • Custom WebViewPage inject code when razor template is rendering
  • Unable to resolve Static method?
  • Replace Fragment with another on back button
  • Activity overlaying. WindowManager.LayoutParams
  • Avoid registering duplicate broadcast receivers in Android
  • Swift Initialization Rule Confusion
  • How to upload specific List image using click on Upload button
  • Blackberry 6: how to detect a long click on track pad?
  • How to get listview position?
  • Syntax error on tokens, AnnotationName expected instead - error on query
  • WPF version of .ScaleControl?
  • Android Activity.onWindowFocusChanged doesn't get called from within TabHost
  • Creating Java object from class name with constructor, which contains parameters [duplicate]
  • how to avoid repetitive constructor in children
  • Deleting and Updating values from a cusrsor adapter
  • Possible to stop flickering java tooltip in heavyweight mode?
  • align graphs with different xlab
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize