Android gps provider work when emulated but not on mobile - java

I think I know the issue for why when running my code the gps tracking only works on emulated devices. It has something to do with the location listener being inside the onCreate() function. I just don't know how I would be able to edit my existing code so Locationlistener still works when placed outside of the onCreate() function.
I want to test the code on a mobile device because the getSpeed() function does not work when emulating.
Here is the code that fetches the location:
private LocationListener listener;
private LocationManager location;
private Float speedStr;
private Boolean boolSpeed;
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#SuppressLint("MissingPermission")
#Override
public void onCreate() { //Get coords and send to main activity. Also get speed.
listener = new LocationListener() {
#Override
public void onLocationChanged(Location location) {
Intent i = new Intent("location update");
i.putExtra("coords",location.getLatitude());
i.putExtra("coords2",location.getLongitude());
sendBroadcast(i);
Toast.makeText(GPS_Service.this,"reading",Toast.LENGTH_SHORT).show();
speedStr = location.getSpeed();
boolSpeed = location.hasSpeed();
Log.i("speed",speedStr.toString());
Log.i("speed",boolSpeed.toString());
}
#Override
public void onProviderDisabled(String s) {
Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
}
};
location = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
location.requestLocationUpdates(LocationManager.GPS_PROVIDER,3000,200,listener); //set min distance proper units
}
#SuppressLint("MissingPermission")
#Override
public void onDestroy() {
super.onDestroy();
if(location != null) {
location.removeUpdates(listener);
}
}
}

Related

(Android) Get latitude and longitude, always zero

I have an issue, I need to get the latitude and longitude of one specific moment like taking a picture. I get the Double values, the problem is that they're always 0. I receive no exceptions, and the coordinates are uploaded successfully to Firebase Database, but when I check them in the JSON, they are always zero (I took like 20 pictures).
This is my sections code of "location" stuffs and the uploading to Firebase Database.
My Main Activity is like this.
public class MainActivity extends AppCompatActivity implements LocationListener
Global variables related to Location
double latitude;
double longitude;
This goes inside onCreate():
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
String networkProvider = LocationManager.NETWORK_PROVIDER;
String gpsProvider = LocationManager.GPS_PROVIDER;
final Location location= new Location(networkProvider);
onLocationChanged(location);{
latitude=location.getLatitude();
longitude=location.getLongitude();
}
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
locationManager.requestLocationUpdates(gpsProvider, 5000, 10, this);
This goes in a button onClickListener(), also inside onCreate() method.
//Insert coordinates to JSON Firebase database...
Coordinates coordinates = new Coordinates();
coordinates.setLatitude(location.getLatitude());
coordinates.setLongitude(location.getLongitude());
testReference.push().setValue(coordinates);
Toast.makeText(MainActivity.this, "Coordinates uploaded!", Toast.LENGTH_LONG).show();
Also I have these methods inside MainActivity:
#Override
public void onLocationChanged(Location location) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onProviderDisabled(String provider) {
}
Try this:
locationManager = (LocationManager) getApplicationContext()
.getSystemService(Context.LOCATION_SERVICE);
Criteria locationCritera = new Criteria();
String providerName = locationManager.getBestProvider(locationCritera,
true);
if(providerName!=null)
location = locationManager.getLastKnownLocation(providerName);
locationListener = new MyLocationListener();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
0, locationListener);
and Implement Your Location Listener class:
private class MyLocationListener implements LocationListener {
public void onLocationChanged(Location loc) {
if (loc != null) {
location = loc;
latitude=location.getLatitude();
longitude=location.getLongitude();
}
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
check null value for getLastKnownLocation() method..

locationManager.isProviderEnabled is not working with Genymortion emulator

I am new to JAVA and I have to create an app that plot a route between where I am (found by GPS) and some pre-specified destination. I followed online tutorials such as http://androidexample.com/GPS_Basic__-__Android_Example/index.php?view=article_discription&aid=68&aaid=93 to learn how to read my own location.
I want my app to act differently when the GPS is turned on and off. To read the GPS status I used an LocationManager object as line below:
boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
I can manually turn the GPS off in Genymortion emulator by right clicking the GPS tab. The picture below shows how I turned on the GPS.
However, my app always say either isGPSEnabled = true all the time or isGPSEnabled = false all the time even though I change the GPS status. Can anyone tells me what I am missing? I attached most of my code below if that helps.
public class MapDisplayActivity extends Activity implements LocationListener {
private MapDisplayFragment fragment;
private DrawerLayout sideDrawerMenu;
private ListView sideDrawerMenuList;
private LocationManager locationManager;
private boolean isGPSEnabled;
private final static String LOG_TAG = "MapDisplayActivity";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.side_drawer_menu);
// some codes come here...
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (isGPSEnabled) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
5000, // 5 sec
10, this);
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
#Override
public void onLocationChanged(Location location) {
if (isGPSEnabled) {
String str = "Latitude: " +
location.getLatitude()
+ "Longitude: " + location.getLongitude();
Toast.makeText(getBaseContext(), str, Toast.LENGTH_LONG).show();
}else{
gpsLatlon = null;
}
}
#Override
public void onProviderDisabled(String provider) {
Toast.makeText(getBaseContext(), "Gps turned off ", Toast.LENGTH_LONG).show();
}
#Override
public void onProviderEnabled(String provider) {
Toast.makeText(getBaseContext(), "Gps turned on ", Toast.LENGTH_LONG).show();
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
// ~~~~~~~~~~ to imeplement LocationListener ~~~~~~~~~~~~~~
private class SideDrawerClickListener implements ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
selectItem(position);
}
}
// some more methods definitions....
}
It looks like you just need to set the isGPSEnabled flag in the callbacks:
#Override
public void onProviderDisabled(String provider) {
if(provider.equals(LocationManager.GPS_PROVIDER)){
isGPSEnabled = false;
}
Toast.makeText(getBaseContext(), "Gps turned off ", Toast.LENGTH_LONG).show();
}
#Override
public void onProviderEnabled(String provider) {
if(provider.equals(LocationManager.GPS_PROVIDER)){
isGPSEnabled = true;
}
Toast.makeText(getBaseContext(), "Gps turned on ", Toast.LENGTH_LONG).show();
}
One thing to note is that these callbacks will only be called if you currently have the Activity registered as a location listener.
So, as you have it now, only the onProviderDisabled() callback will be called if the Activity starts with GPS on and then GPS is disabled with the Activity still running.
One way to go would be to register for NETWORK_PROVIDER callbacks when GPS is disabled.
That way you would still have a location listener registered, and you would get the onProviderEnabled() callback for GPS. At that point you could unregister for NETWORK callbacks and register for GPS callbacks.

Service with Broadcast to say if GPS is enabled

I want to detect the switch of GPS.
My idea is to use IntentService and Broadcast intent.
My service is running but my broadcast receiver doesn't work.
I just wanna say to my HomeActivity if GPS is turn on/off by user. With this info, i will able to set up my GoogleMap with the current location.
Thx !
GpsService.java :
public class GpsService extends IntentService implements LocationListener {
private final static String TAG = "com.example.smartoo.gpsservice";
public GpsService () {
super(TAG);
Log.e("TAG", "GPS service k");
}
#Override
public void onLocationChanged(Location location) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public void onProviderEnabled(String provider) {
if (LocationManager.GPS_PROVIDER.equals(provider)) {
Intent i = new Intent("android.location.PROVIDERS_CHANGED");
i.putExtra("enabled", 1);
sendBroadcast(i);
}
}
#Override
public void onProviderDisabled(String provider) {
if (LocationManager.GPS_PROVIDER.equals(provider)) {
Intent i = new Intent("android.location.PROVIDERS_CHANGED");
i.putExtra("disnabled", 1);
sendBroadcast(i);
}
}
#Override
protected void onHandleIntent(Intent intent) {
}
}
HomeActivity.java
public class HomeActivity extends ActionBarActivity implements LocationListener,
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener {
//My attrs
private GoogleMap mMap = null;
Location currentLocation;
/*
* Initialize the Activity
*/
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_layout);
//Get the map from xml file
mMap = getMap();
Intent i = new Intent(HomeActivity.this, GpsService.class);
startService(i);
}
/*
* Called when the Activity is restarted, even before it becomes visible.
*/
#Override
public void onStart() {
super.onStart();
final LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
//Check if GPS is enable on device.
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
showGPSDisabledAlertToUser();
}
}
public class GpsLocationReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().matches("android.location.PROVIDERS_CHANGED")) {
int extra = intent.getIntExtra("enabled", -1);
//If GPS not enabled
if(extra == -1) {
extra = intent.getIntExtra("disabled", -1);
//If GPS not disabled
if (extra == -1) {
//Nothing
}
//If GPS disabled
else {
//Do something
}
}
//If GPS enabled
else {
Toast.makeText(HomeActivity.this, "GPS enabled", Toast.LENGTH_LONG).show();
currentLocation = getLocation();
setUpMapIfNeeded();
}
Toast.makeText(HomeActivity.this, "in onREceive", Toast.LENGTH_LONG).show();
}
}
}
}
For an example, first add the following permission to your manifest:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Then, a simple Activity:
public class MainActivity extends Activity
{
private GpsStatusReceiver receiver = new GpsStatusReceiver();
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
protected void onResume()
{
super.onResume();
registerReceiver(receiver, new IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION));
}
#Override
protected void onPause()
{
super.onPause();
unregisterReceiver(receiver);
}
private class GpsStatusReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
LocationManager lm = (LocationManager) context.getSystemService(Service.LOCATION_SERVICE);
boolean isEnabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
onGpsStatusChanged(isEnabled);
}
}
private void onGpsStatusChanged(boolean isEnabled)
{
// Do/start your work here.
Toast.makeText(this, "GPS enabled - " + isEnabled, 0).show();
}
}

Android service not stopping when stopService() is called

I'm having an issue stopping a service that I have started.
The service is called when the user logs in, and starts to track the user's location. This is working fine. The service is meant to stop when the user presses the logout button and is successfully logged out.
It's an android service that is being called through a JavaScript interface by a HTML button.
Here is my Main Class which contains the methods for starting and stopping the service:
public class CasesMain extends DroidGap {
Intent gpsTracking;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.init();
appView.addJavascriptInterface(this, "StartGPS");
super.loadUrl(Config.getStartUrl());
}
public void startGPS(){
gpsTracking = new Intent(this, MyService.class);
startService(gpsTracking);
}
public void stopGPS(){
stopService( gpsTracking );
}
}
Here is the MyService class:
public class MyService extends Service {
private LocationManager locManager;
private LocationListener locListener = new myLocationListener();
private boolean gps_enabled = false;
private boolean network_enabled = false;
private Handler handler = new Handler();
Thread t;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
}
#Override
public void onDestroy() {
}
#Override
public void onStart(Intent intent, int startid) {
}
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(getBaseContext(), "Service Started", Toast.LENGTH_SHORT)
.show();
final Runnable r = new Runnable() {
public void run() {
location();
handler.postDelayed(this, 10000);
}
};
handler.postDelayed(r, 10000);
return START_STICKY;
}
public void location() {
locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
try {
gps_enabled = locManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch (Exception ex) {
}
try {
network_enabled = locManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch (Exception ex) {
}
if (gps_enabled) {
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
0, locListener);
}
if (network_enabled) {
locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
0, 0, locListener);
}
}
private class myLocationListener implements LocationListener {
double lat_old = 0.0;
double lon_old = 0.0;
double lat_new;
double lon_new;
#Override
public void onLocationChanged(Location location) {
if (location != null) {
locManager.removeUpdates(locListener);
lon_new = location.getLongitude();
lat_new = location.getLatitude();
String longitude = "Longitude: " + location.getLongitude();
String latitude = "Latitude: " + location.getLatitude();
Log.v("Debug", "Latt: " + latitude + " Long: " + longitude);
Toast.makeText(getApplicationContext(),
longitude + "\n" + latitude, Toast.LENGTH_SHORT).show();
lat_old = lat_new;
lon_old = lon_new;
}
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
}
And here is my call to stop the service: window.StartGPS.stopGPS();
The start works perfectly, but when I log out, the app continues to show messages of the latt and long meaning the call to stopService() either didn't work or is not called.
Can anyone see where my mistake is or what is going wrong?
Any help would be greatly appreciated!
FIXED
Adding handler.removeCallbacksAndMessages(null); in the onDestroy() method of MyService fixed it for me.
Can anyone see where my mistake is or what is going wrong?
You have an empty onDestroy() method. You need to call removeCallbacks() on your Handler there.
If you are using any android.os.Handler in the service remember to call removeCallbacksAndMessages like this:
#Override
public void onDestroy() {
Toast.makeText(this, "service onDestroy", Toast.LENGTH_LONG).show();
Utils.cancelNotification(this);
customHandler.removeCallbacksAndMessages(null);
}
And the service will destroy successfully. It worked for me.
Here's what I would suggest
1) Put more log statements in calls leading to stopService.
2) Implement On ServiceConnection interface and see what's happening in OServiceConnected and onServiceDisconnected.

Android onLocationChanged and MainActivity class

I have the following code:
public class MyLocationListener implements LocationListener {
#Override
public void onLocationChanged(Location loc) {
// called when the listener is notified with a location update from the GPS
Log.d("Latitude", Double.toString(loc.getLatitude()));
Log.d("Longitude", Double.toString(loc.getLongitude()));
}
#Override
public void onProviderDisabled(String provider) {
// called when the GPS provider is turned off (user turning off the GPS on the phone)
}
#Override
public void onProviderEnabled(String provider) {
// called when the GPS provider is turned on (user turning on the GPS on the phone)
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
and in my MainActivity
LocationListener locationListener = new MyLocationListener();
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
Now, all I want is to receive the current position of the device ONCE to the MainActivity class (get altitude and longitude variables to use later in the application).
A. how do I stop receiving the location after a single time? The function lm.removeUpdates(listener) can only be called in the MainActivity class.
B. basically the same. How do I connect between the MyLocationListener class and the MainActivity one?
Sorry, I'm a newbie to Android and Java development.
And thanks!
You may use the following sample code:
public class LocationGetter {
private final Context context;
private Location location = null;
private final Cordinate gotLocationLock = new Cordinate();
private final LocationResult locationResult = new LocationResult() {
#Override
public void gotLocation(Location location) {
synchronized (gotLocationLock) {
LocationGetter.this.location = location;
gotLocationLock.notifyAll();
Looper.myLooper().quit();
}
}
};
public LocationGetter(Context context) {
if (context == null)
throw new IllegalArgumentException("context == null");
this.context = context;
}
public void getLocation(int maxWaitingTime, int updateTimeout) {
try {
final int updateTimeoutPar = updateTimeout;
synchronized (gotLocationLock) {
new Thread() {
public void run() {
Looper.prepare();
LocationResolver locationResolver = new LocationResolver();
locationResolver.prepare();
locationResolver.getLocation(context, locationResult, updateTimeoutPar);
Looper.loop();
}
}.start();
gotLocationLock.wait(maxWaitingTime);
}
} catch (InterruptedException e1) {
e1.printStackTrace();
}
gteAddress ();
}
public double getLatitude() {
return location.getLatitude();
}
public double getLongitude() {
return location.getLongitude();
}
In your activity use:
_locationGetter=new LocationGetter(context);
_locationGetter.getLocation(200000000, 10000000);
_locationGetter.getLongitude();
_locationGetter.getLatitude();
You can also use LocationManager.removeUpdates after obtining the coordinates (and possibly checking if the coordinates are sufficient for your needs):
#Override
public void onLocationChanged(Location loc) {
// called when the listener is notified with a location update from the GPS
Log.d("Latitude", Double.toString(loc.getLatitude()));
Log.d("Longitude", Double.toString(loc.getLongitude()));
lm.removeUpdates(this);
}

Categories