Why is my app showing high battery uses? - android

I am using google fused location api but still its showing high battery use. I am developing an app where I need to fetch user current location after that I don't need the fetch the location. Am an fetching the current location as below
public class MyLocationManager implements GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener {
private static MyLocationManager locationManager;
private static LocationClient locationclient;
public static final int LAT_KNOW_NLOCATION = 1005;
public static final int CURRENT_LOCATION = 1006;
public static final int CRON_LOCATION_UPDATE = 1007;
private static String LOCATION_CLIENT_NOT_FOUND_MESSAGE = "Error... Location client not found!";
private static Context context;
private static Activity currentActivity;
private static ProgressDialog progressDialog;
private static ILocationUpdator iLocationUpdator;
private static final int UPDATE_INTERVAL = 1000 * 30;
private static LocationManager androidLocationManager;
private static int CURRENTLY_LOOKING_FOR = 0;
private MyLocationManager(Context context) {
locationclient = new LocationClient(context, this, this);
MyLocationManager.context = context;
androidLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
}
public static synchronized MyLocationManager getLocationManagerInstance(Context context, ILocationUpdator iLocationUpdator, Activity activity) {
currentActivity = activity;
if (locationManager == null) {
locationManager = new MyLocationManager(context);
}
progressDialog = new ProgressDialog(currentActivity);
progressDialog.setCancelable(false);
progressDialog.setMessage("Please wait looking for current location");
MyLocationManager.iLocationUpdator = iLocationUpdator;
return locationManager;
}
public void getCurrentLocation() {
CURRENTLY_LOOKING_FOR = MyLocationManager.CURRENT_LOCATION;
callForLocationUpdate();
}
public void getlastKnownLocation() {
CURRENTLY_LOOKING_FOR = MyLocationManager.LAT_KNOW_NLOCATION;
callForLocationUpdate();
}
public void getCronLocationUpdate() {
CURRENTLY_LOOKING_FOR = MyLocationManager.CRON_LOCATION_UPDATE;
callForLocationUpdate();
}
private void callForLocationUpdate() {
System.out.println("callForLocationUpdate " + isProviderEnabled());
if (isProviderEnabled()) {
getReliventLocation(CURRENTLY_LOOKING_FOR);
}
else {
DialogUtility.showCallbackMessage("Please turn on GPS/Location service.", currentActivity, new AlertDialogCallBack() {
#Override
public void onSubmit() {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
currentActivity.startActivity(intent);
}
#Override
public void onCancel() {
}
#Override
public void onSubmitWithEditText(String text) {
}
});
}
}
private boolean isProviderEnabled() {
boolean gps_enabled = androidLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean network_enabled = androidLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (gps_enabled && network_enabled) {
return true;
}
else {
return false;
}
}
private void getReliventLocation(int lookingFor) {
Location location = null;
if (locationclient != null) {
currentActivity.runOnUiThread(new Runnable() {
#Override
public void run() {
progressDialog.show();
}
});
if (!locationclient.isConnected()) {
locationclient.connect();
}
else {
LocationRequest locationrequest = LocationRequest.create();
locationrequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationListener locationListener = new LocationListener() {
#Override
public void onLocationChanged(Location location) {
sendLocationUpdate(location, this);
}
};
switch (lookingFor) {
case MyLocationManager.LAT_KNOW_NLOCATION:
location = locationclient.getLastLocation();
sendLocationUpdate(location, null);
break;
case MyLocationManager.CURRENT_LOCATION:
locationrequest.setInterval(UPDATE_INTERVAL);
locationclient.requestLocationUpdates(locationrequest, locationListener);
break;
case MyLocationManager.CRON_LOCATION_UPDATE:
Intent mIntentService = new Intent(context, LocationService.class);
PendingIntent mPendingIntent = PendingIntent.getService(context, 1, mIntentService, 0);
locationrequest.setInterval(100);
locationclient.requestLocationUpdates(locationrequest, mPendingIntent);
break;
}
}
}
else {
showErrorMessage(LOCATION_CLIENT_NOT_FOUND_MESSAGE);
}
}
private void sendLocationUpdate(Location location, LocationListener locationListener) {
if (location != null) {
double lat = location.getLatitude();
double lon = location.getLongitude();
iLocationUpdator.getLocation(new LatLng(lat, lon), 0, "success..");
}
else {
iLocationUpdator.getLocation(new LatLng(0.0, 0.0), 1, "Some error occured..");
}
if (progressDialog != null) {
progressDialog.dismiss();
}
if (CURRENTLY_LOOKING_FOR == MyLocationManager.CURRENT_LOCATION) {
locationclient.removeLocationUpdates(locationListener);
}
}
private static void showErrorMessage(String message) {
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
}
#Override
public void onConnectionFailed(ConnectionResult connectionResult) {
if (iLocationUpdator != null)
iLocationUpdator.getLocation(new LatLng(0.0, 0.0), 1, "Connection Result...Error!");
if (progressDialog != null) {
progressDialog.dismiss();
}
}
#Override
public void onConnected(Bundle bundle) {
getReliventLocation(CURRENTLY_LOOKING_FOR);
}
#Override
public void onDisconnected() {
if (iLocationUpdator != null)
iLocationUpdator.getLocation(new LatLng(0.0, 0.0), 1, "Connection Disconnected...Error!");
if (progressDialog != null) {
progressDialog.dismiss();
}
}
}
Please point out what I am doing wrong.

I am unfamiliar with android location services, however I think you would be able to diagnose your battery usage using this free tool provided by AT&T.
Application Resource Optimizer:
https://developer.att.com/application-resource-optimizer
It looks like you might be polling for current location updates, each of these will effect battery life. ATT ARO can help identify this.

Related

Localbroadcastmanager not working. Getting latitude and longitude

My app is about getting location in background service. However, it cannot return latitude and longitude. Here is the code.
Service side:
public class Map extends Service implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener {
private static final String TAG = Map.class.getSimpleName();
GoogleApiClient mLocationClient;
LocationRequest mLocationRequest = new LocationRequest();
public static final String ACTION_LOCATION_BROADCAST = Map.class.getName() + "LocationBroadcast";
public static final String EXTRA_LATITUDE = "extra_latitude";
public static final String EXTRA_LONGITUDE = "extra_longitude";
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
mLocationClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mLocationRequest.setInterval(10000);
mLocationRequest.setFastestInterval(5000);
int priority = LocationRequest.PRIORITY_HIGH_ACCURACY;
mLocationRequest.setPriority(priority);
mLocationClient.connect();
return START_STICKY;
}
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onConnected(Bundle dataBundle) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "== Error On onConnected() Permission not granted");
return;
}
LocationServices.FusedLocationApi.requestLocationUpdates(mLocationClient, mLocationRequest, this);
Log.d(TAG, "Connected to Google API");
}
#Override
public void onConnectionSuspended(int i) {
Log.d(TAG, "Connection suspended");
}
#Override
public void onLocationChanged(Location location) {
Log.d(TAG, "Location changed");
if (location != null) {
Log.d(TAG, "== location != null");
sendMessageToUI(String.valueOf(location.getLatitude()), String.valueOf(location.getLongitude()));
}
}
private void sendMessageToUI(String lat, String lng) {
Log.d(TAG, "Sending info...");
Intent intent = new Intent(ACTION_LOCATION_BROADCAST);
intent.putExtra(EXTRA_LATITUDE, lat);
intent.putExtra(EXTRA_LONGITUDE, lng);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
#Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.d(TAG, "Failed to connect to Google API");
}
}
MainActivity side:
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private static final int REQUEST_PERMISSIONS_REQUEST_CODE = 34;
private boolean mAlreadyStartedService = false;
private FirebaseUser user;
private FirebaseAuth mAuth;
private DatabaseReference mDatabase;
private double longitude;
private double latitude;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mAuth = FirebaseAuth.getInstance();
mDatabase = FirebaseDatabase.getInstance().getReference();
user = mAuth.getCurrentUser();
LocalBroadcastManager.getInstance(this).registerReceiver(
bReceiver, new IntentFilter("ACTION_LOCATION_BROADCAST"));
}
private BroadcastReceiver bReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String latitude = intent.getStringExtra(Map.EXTRA_LATITUDE);
String longitude = intent.getStringExtra(Map.EXTRA_LONGITUDE);
dd.setText("HI");
if (latitude != null && longitude != null) {
mAuth = FirebaseAuth.getInstance();
user = mAuth.getCurrentUser();
if (!String.valueOf(latitude).equals(""))
mDatabase.child("users").child(user.getUid()).child("latitude").setValue(latitude);
if (!String.valueOf(longitude).equals(""))
mDatabase.child("users").child(user.getUid()).child("longitude").setValue(longitude);
cc.setText("Latitude:" + latitude + " Longitude: " + longitude);
}
}
};
private void startStep3() {
Intent intent = new Intent(this, Map.class);
this.startService(intent);
ff.setText("3ok");
mAlreadyStartedService = true;
}
#Override
public void onResume() {
super.onResume();
startStep3();
ee.setText("Start");
}
#Override
public void onDestroy() {
stopService(new Intent(this, Map.class));
mAlreadyStartedService = false;
super.onDestroy();
}
}
AndroidManifest:
<service android:name=".Map" />
It can show "start" and "3ok" on the page but cannot get the location. It appears "TextView" instead of the location information. Did I miss any codes and where is the problem? Is it a correct way to use service?Thank you.

Android: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

I am making an android app where I am trying to get location using location manager and then I push that location to a server. When I try to do this I am getting the following error.
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:200)
at android.os.Handler.<init>(Handler.java:114)
at android.location.LocationManager$ListenerTransport$1.<init>(LocationManager.java:223)
at android.location.LocationManager$ListenerTransport.<init>(LocationManager.java:223)
at android.location.LocationManager.wrapListener(LocationManager.java:851)
at android.location.LocationManager.requestLocationUpdates(LocationManager.java:864)
at android.location.LocationManager.requestLocationUpdates(LocationManager.java:459)
at com.pickingo.fe.services.gps.AppLocationService.getLocation(AppLocationService.java:30)
at com.pickingo.fe.gps.CurrentLocationPusher.run(CurrentLocationPusher.java:60)
at java.lang.Thread.run(Thread.java:818)
This is my appLocationService
public class AppLocationService extends Service implements LocationListener {
protected LocationManager locationManager;
Location location;
private static final long MIN_DISTANCE_FOR_UPDATE = 50;
private static final long MIN_TIME_FOR_UPDATE = 1000*60*2;
public AppLocationService(Context context) {
locationManager = (LocationManager) context
.getSystemService(LOCATION_SERVICE);
}
public Location getLocation(String provider) {
if (locationManager.isProviderEnabled(provider)) {
locationManager.requestLocationUpdates(provider,
MIN_TIME_FOR_UPDATE, MIN_DISTANCE_FOR_UPDATE, this);
if (locationManager != null) {
location = locationManager.getLastKnownLocation(provider);
return location;
}
}
return null;
}
#Override
public void onLocationChanged(Location location) {
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public IBinder onBind(Intent arg0) {
return null;
}
}
This is the code I use to push the location after getting the location from the LocationManager.
public class CurrentLocationPusher implements Runnable {
private static CurrentLocationPusher _singleInstance;
private CurrentLocationPusher() {
this.apiClient = ApiClient.getInstance();
this.appLocationService = new AppLocationService(appContext);
this.runningThread = new Thread(this);
}
public static CurrentLocationPusher getInstance() {
if (_singleInstance == null)
synchronized (CurrentLocationPusher.class) {
if (_singleInstance == null)
_singleInstance = new CurrentLocationPusher();
}
return _singleInstance;
}
private boolean isStopThread = false;
public static final long ONE_MINUTE = 60 * 1000;
private static Context appContext;
private AppLocationService appLocationService;
private ApiClient apiClient;
private Thread runningThread;
public static void init(Context applicationContext) {
appContext = applicationContext;
}
#Override
public void run() {
Location location = appLocationService
.getLocation(LocationManager.GPS_PROVIDER);
while (!isStopThread) {
try {
if (location != null)
if (CommonUtils.isActiveToInternet(appContext))
apiClient.getApi(appContext).pushLocation(String.valueOf(location.getLatitude()),
String.valueOf(location.getLongitude()),String.valueOf(System.currentTimeMillis()),
String.valueOf(location.getAccuracy()));
Thread.sleep(ONE_MINUTE);
} catch (InterruptedException e) {
} catch (Exception e) {
Log.e(PickingoConstant.TAG, "error" + e.getMessage());
}
}
}
public void startPushingLocation() {
if (this.runningThread.getState() == Thread.State.NEW) {
runningThread.start();
isStopThread = false;
} else if (this.runningThread.getState() == Thread.State.TERMINATED) {
(runningThread = new Thread(this)).start();
isStopThread = false;
} else if (this.runningThread.getState() == Thread.State.RUNNABLE) {
return;
}
}
public void stopPushingLocation() {
isStopThread = true;
}
}
I am getting error at the following line in CurrentLocationPusher
.getLocation(LocationManager.GPS_PROVIDER);
If someone could give a hint or some assistance I would really appreciate it. Thanks in advance !!
As pointed out by Michael adding this code at starting of my run() method worked.
if (Looper.myLooper() == null) {
Looper.prepare();
}

Android Location Manager, Get GPS location ,if no GPS then get to Network Provider location

I am using this given below code to get locations:
public Location getLocation() {
try {
mLocationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
// getting GPS status
boolean isGPSEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
boolean isNetworkEnabled = mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
// First get location from Network Provider
if (isNetworkEnabled) {
mLocationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (mLocationManager != null) {
location = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
lat = location.getLatitude();
lng = location.getLongitude();
}
}
}
//get the location by gps
if (isGPSEnabled) {
if (location == null) {
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (mLocationManager != null) {location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
lat = location.getLatitude();
lng = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
It is working properly, but I would like to get GPS location first, and in case if it is unavailable , location manager should query for Network provider, in which I am getting trouble.
Please, recommend me the good way to do this.
You're saying that you need GPS location first if its available, but what you did is first you're getting location from network provider and then from GPS. This will get location from Network and GPS as well if both are available. What you can do is, write these cases in if..else if block. Similar to-
if( !isGPSEnabled && !isNetworkEnabled) {
// Can't get location by any way
} else {
if(isGPSEnabled) {
// get location from GPS
} else if(isNetworkEnabled) {
// get location from Network Provider
}
}
So this will fetch location from GPS first (if available), else it will try to fetch location from Network Provider.
EDIT:
To make it better, I'll post a snippet. Consider it is in try-catch:
boolean gps_enabled = false;
boolean network_enabled = false;
LocationManager lm = (LocationManager) mCtx
.getSystemService(Context.LOCATION_SERVICE);
gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
Location net_loc = null, gps_loc = null, finalLoc = null;
if (gps_enabled)
gps_loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (network_enabled)
net_loc = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (gps_loc != null && net_loc != null) {
//smaller the number more accurate result will
if (gps_loc.getAccuracy() > net_loc.getAccuracy())
finalLoc = net_loc;
else
finalLoc = gps_loc;
// I used this just to get an idea (if both avail, its upto you which you want to take as I've taken location with more accuracy)
} else {
if (gps_loc != null) {
finalLoc = gps_loc;
} else if (net_loc != null) {
finalLoc = net_loc;
}
}
Now you check finalLoc for null, if not then return it.
You can write above code in a function which returns the desired (finalLoc) location. I think this might help.
The recommended way to do this is to use LocationClient:
First, define location update interval values. Adjust this to your needs.
private static final int MILLISECONDS_PER_SECOND = 1000;
private static final long UPDATE_INTERVAL = MILLISECONDS_PER_SECOND * UPDATE_INTERVAL_IN_SECONDS;
private static final int FASTEST_INTERVAL_IN_SECONDS = 1;
private static final long FASTEST_INTERVAL = MILLISECONDS_PER_SECOND * FASTEST_INTERVAL_IN_SECONDS;
Have your Activity implement GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener, and LocationListener.
public class LocationActivity extends Activity implements
GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener, LocationListener {}
Then, set up a LocationClientin the onCreate() method of your Activity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mLocationClient = new LocationClient(this, this, this);
mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(UPDATE_INTERVAL);
mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
}
Add the required methods to your Activity; onConnected() is the method that is called when the LocationClientconnects. onLocationChanged() is where you'll retrieve the most up-to-date location.
#Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.w(TAG, "Location client connection failed");
}
#Override
public void onConnected(Bundle dataBundle) {
Log.d(TAG, "Location client connected");
mLocationClient.requestLocationUpdates(mLocationRequest, this);
}
#Override
public void onDisconnected() {
Log.d(TAG, "Location client disconnected");
}
#Override
public void onLocationChanged(Location location) {
if (location != null) {
Log.d(TAG, "Updated Location: " + Double.toString(location.getLatitude()) + "," + Double.toString(location.getLongitude()));
} else {
Log.d(TAG, "Updated location NULL");
}
}
Be sure to connect/disconnect the LocationClient so it's only using extra battery when absolutely necessary and so the GPS doesn't run indefinitely. The LocationClient must be connected in order to get data from it.
public void onResume() {
super.onResume();
mLocationClient.connect();
}
public void onStop() {
if (mLocationClient.isConnected()) {
mLocationClient.removeLocationUpdates(this);
}
mLocationClient.disconnect();
super.onStop();
}
Get the user's location. First try using the LocationClient; if that fails, fall back to the LocationManager.
public Location getLocation() {
if (mLocationClient != null && mLocationClient.isConnected()) {
return mLocationClient.getLastLocation();
} else {
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
if (locationManager != null) {
Location lastKnownLocationGPS = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (lastKnownLocationGPS != null) {
return lastKnownLocationGPS;
} else {
return locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
} else {
return null;
}
}
}
Main Class:
public class AndroidLocationActivity extends Activity {
Button btnGPSShowLocation;
Button btnNWShowLocation;
AppLocationService appLocationService;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
appLocationService = new AppLocationService(
AndroidLocationActivity.this);
btnGPSShowLocation = (Button) findViewById(R.id.btnGPSShowLocation);
btnGPSShowLocation.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Location gpsLocation = appLocationService
.getLocation(LocationManager.GPS_PROVIDER);
if (gpsLocation != null) {
double latitude = gpsLocation.getLatitude();
double longitude = gpsLocation.getLongitude();
Toast.makeText(
getApplicationContext(),
"Mobile Location (GPS): \nLatitude: " + latitude
+ "\nLongitude: " + longitude,
Toast.LENGTH_LONG).show();
} else {
showSettingsAlert("GPS");
}
}
});
btnNWShowLocation = (Button) findViewById(R.id.btnNWShowLocation);
btnNWShowLocation.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Location nwLocation = appLocationService
.getLocation(LocationManager.NETWORK_PROVIDER);
if (nwLocation != null) {
double latitude = nwLocation.getLatitude();
double longitude = nwLocation.getLongitude();
Toast.makeText(
getApplicationContext(),
"Mobile Location (NW): \nLatitude: " + latitude
+ "\nLongitude: " + longitude,
Toast.LENGTH_LONG).show();
} else {
showSettingsAlert("NETWORK");
}
}
});
}
public void showSettingsAlert(String provider) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
AndroidLocationActivity.this);
alertDialog.setTitle(provider + " SETTINGS");
alertDialog.setMessage(provider
+ " is not enabled! Want to go to settings menu?");
alertDialog.setPositiveButton("Settings",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(
Settings.ACTION_LOCATION_SOURCE_SETTINGS);
AndroidLocationActivity.this.startActivity(intent);
}
});
alertDialog.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Next Class:
public class AppLocationService extends Service implements LocationListener {
protected LocationManager locationManager;
Location location;
private static final long MIN_DISTANCE_FOR_UPDATE = 10;
private static final long MIN_TIME_FOR_UPDATE = 1000 * 60 * 2;
public AppLocationService(Context context) {
locationManager = (LocationManager) context
.getSystemService(LOCATION_SERVICE);
}
public Location getLocation(String provider) {
if (locationManager.isProviderEnabled(provider)) {
locationManager.requestLocationUpdates(provider,
MIN_TIME_FOR_UPDATE, MIN_DISTANCE_FOR_UPDATE, this);
if (locationManager != null) {
location = locationManager.getLastKnownLocation(provider);
return location;
}
}
return null;
}
#Override
public void onLocationChanged(Location location) {
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public IBinder onBind(Intent arg0) {
return null;
}
}
Don't forget to add in your manifest.
<!-- to get location using GPS -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- to get location using NetworkProvider -->
<uses-permission android:name="android.permission.INTERNET" />
use the fusion API that google developer have developed with fusion of GPS Sensor,Magnetometer,Accelerometer also using Wifi or cell location to calculate or estimate the location. It is also able to give location updates also inside the building accurately.
package com.example.ashis.gpslocation;
import android.app.Activity;
import android.location.Location;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
/**
* Location sample.
*
* Demonstrates use of the Location API to retrieve the last known location for a device.
* This sample uses Google Play services (GoogleApiClient) but does not need to authenticate a user.
* See https://github.com/googlesamples/android-google-accounts/tree/master/QuickStart if you are
* also using APIs that need authentication.
*/
public class MainActivity extends Activity implements LocationListener,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
private static final long ONE_MIN = 500;
private static final long TWO_MIN = 500;
private static final long FIVE_MIN = 500;
private static final long POLLING_FREQ = 1000 * 20;
private static final long FASTEST_UPDATE_FREQ = 1000 * 5;
private static final float MIN_ACCURACY = 1.0f;
private static final float MIN_LAST_READ_ACCURACY = 1;
private LocationRequest mLocationRequest;
private Location mBestReading;
TextView tv;
private GoogleApiClient mGoogleApiClient;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!servicesAvailable()) {
finish();
}
setContentView(R.layout.activity_main);
tv= (TextView) findViewById(R.id.tv1);
mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(POLLING_FREQ);
mLocationRequest.setFastestInterval(FASTEST_UPDATE_FREQ);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
if (mGoogleApiClient != null) {
mGoogleApiClient.connect();
}
}
#Override
protected void onResume() {
super.onResume();
if (mGoogleApiClient != null) {
mGoogleApiClient.connect();
}
}
#Override
protected void onPause() {d
super.onPause();
if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}
tv.setText(location + "");
// Determine whether new location is better than current best
// estimate
if (null == mBestReading || location.getAccuracy() < mBestReading.getAccuracy()) {
mBestReading = location;
if (mBestReading.getAccuracy() < MIN_ACCURACY) {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}
}
}
#Override
public void onConnected(Bundle dataBundle) {
// Get first reading. Get additional location updates if necessary
if (servicesAvailable()) {
// Get best last location measurement meeting criteria
mBestReading = bestLastKnownLocation(MIN_LAST_READ_ACCURACY, FIVE_MIN);
if (null == mBestReading
|| mBestReading.getAccuracy() > MIN_LAST_READ_ACCURACY
|| mBestReading.getTime() < System.currentTimeMillis() - TWO_MIN) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
//Schedule a runnable to unregister location listeners
#Override
public void run() {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, MainActivity.this);
}
}, ONE_MIN, TimeUnit.MILLISECONDS);
}
}
}
#Override
public void onConnectionSuspended(int i) {
}
private Location bestLastKnownLocation(float minAccuracy, long minTime) {
Location bestResult = null;
float bestAccuracy = Float.MAX_VALUE;
long bestTime = Long.MIN_VALUE;
// Get the best most recent location currently available
Location mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
//tv.setText(mCurrentLocation+"");
if (mCurrentLocation != null) {
float accuracy = mCurrentLocation.getAccuracy();
long time = mCurrentLocation.getTime();
if (accuracy < bestAccuracy) {
bestResult = mCurrentLocation;
bestAccuracy = accuracy;
bestTime = time;
}
}
// Return best reading or null
if (bestAccuracy > minAccuracy || bestTime < minTime) {
return null;
}
else {
return bestResult;
}
}
#Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
private boolean servicesAvailable() {
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (ConnectionResult.SUCCESS == resultCode) {
return true;
}
else {
GooglePlayServicesUtil.getErrorDialog(resultCode, this, 0).show();
return false;
}
}
}
If you want to run inside a background service and take the data in foreground use the below one, it is tested and verified.
public class MyService extends Service
implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
com.google.android.gms.location.LocationListener {
private static final int ASHIS = 1234;
Intent intentForPendingIntent;
HandlerThread handlerThread;
Looper looper;
GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRrequest;
private static final int UPDATE_INTERVAL = 1000;
private static final int FASTEST_INTERVAL = 100;
private static final int DSIPLACEMENT_UPDATES = 1;
;
private Handler handler1;
private Runnable runable1;
private Location mLastLocation;
private float waitingTime;
private int waiting2min;
private Location locationOld;
private double distance;
private float totalWaiting;
private float speed;
private long timeGpsUpdate;
private long timeOld;
private NotificationManager mNotificationManager;
Notification notification;
PendingIntent resultPendingIntent;
NotificationCompat.Builder mBuilder;
// Sets an ID for the notification
int mNotificationId = 001;
private static final String TAG = "BroadcastService";
public static final String BROADCAST_ACTION = "speedExceeded";
private final Handler handler = new Handler();
Intent intentforBroadcast;
int counter = 0;
private Runnable sendUpdatesToUI;
#Nullable
#Override
public IBinder onBind(Intent intent) {
Toast.makeText(MyService.this, "binder", Toast.LENGTH_SHORT).show();
return null;
}
#Override
public void onCreate() {
showNotification();
intentforBroadcast = new Intent(BROADCAST_ACTION);
Toast.makeText(MyService.this, "created", Toast.LENGTH_SHORT).show();
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
createLocationRequest();
mGoogleApiClient.connect();
}
#TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void showNotification() {
mBuilder =
(NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_media_play)
.setContentTitle("Total Waiting Time")
.setContentText(totalWaiting+"");
Intent resultIntent = new Intent(this, trackingFusion.class);
// Because clicking the notification opens a new ("special") activity, there's
// no need to create an artificial back stack.
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
this,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotifyMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Builds the notification and issues it.
mNotifyMgr.notify(mNotificationId, mBuilder.build());
startForeground(001, mBuilder.getNotification());
}
#Override
public void onLocationChanged(Location location) {
//handler.removeCallbacks(runable);
Toast.makeText(MyService.this, "speed" + speed, Toast.LENGTH_SHORT).show();
timeGpsUpdate = location.getTime();
float delta = (timeGpsUpdate - timeOld) / 1000;
if (location.getAccuracy() < 100) {
speed = location.getSpeed();
distance += mLastLocation.distanceTo(location);
Log.e("distance", "onLocationChanged: " + distance);
//mLastLocation = location;
//newLocation = mLastLocation;
Log.e("location:", location + "");
//speed = (long) (distance / delta);
locationOld = location;
mLastLocation = location;
diaplayViews();
}
diaplayViews();
/*if (map != null) {
map.addMarker(new MarkerOptions()
.position(new LatLng(location.getLatitude(), location.getLongitude()))
.title("Hello world"));
}*/
}
private void createLocationRequest() {
mLocationRrequest = new LocationRequest();
mLocationRrequest.setInterval(UPDATE_INTERVAL);
mLocationRrequest.setFastestInterval(FASTEST_INTERVAL);
mLocationRrequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRrequest.setSmallestDisplacement(DSIPLACEMENT_UPDATES);
}
private void methodToCalculateWaitingTime() {
if (handler1 != null) {
handler1.removeCallbacks(runable1);
}
Log.e("Here", "here1");
handler1 = new Handler(Looper.getMainLooper());
runable1 = new Runnable() {
public void run() {
Log.e("Here", "here2:" + mLastLocation.getSpeed());
if (mLastLocation != null) {
diaplayViews();
if ((mLastLocation.getSpeed() == 0.0)) {
increaseTime();
} else {
if (waitingTime <= 120) {
waiting2min = 0;
}
}
handler1.postDelayed(this, 10000);
} else {
if (ActivityCompat.checkSelfPermission(MyService.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(MyService.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
locationOld = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
mLastLocation = locationOld;
}
}
};
handler1.postDelayed(runable1, 10000);
}
private void diaplayViews() {
float price = (float) (14 + distance * 0.5);
//textDistance.setText(waitingTime);a
}
private void increaseTime() {
waiting2min = waiting2min + 10;
if (waiting2min >= 120)
{
if (waiting2min == 120) {
waitingTime = waitingTime + 2 * 60;
} else {
waitingTime = waitingTime + 10;
}
totalWaiting = waitingTime / 60;
showNotification();
Log.e("waiting Time", "increaseTime: " + totalWaiting);
}
}
#Override
public void onDestroy() {
Toast.makeText(MyService.this, "distroyed", Toast.LENGTH_SHORT).show();
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
mGoogleApiClient.disconnect();
}
#Override
public void onConnected(Bundle bundle) {
Log.e("Connection_fusion", "connected");
startLocationUpdates();
}
#Override
public void onConnectionSuspended(int i) {
}
private void startLocationUpdates() {
Location location = plotTheInitialMarkerAndGetInitialGps();
if (location == null) {
plotTheInitialMarkerAndGetInitialGps();
} else {
mLastLocation = location;
methodToCalculateWaitingTime();
}
}
private Location plotTheInitialMarkerAndGetInitialGps() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return null;
}
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRrequest, this);
locationOld = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if ((locationOld != null)) {
mLastLocation = locationOld;
timeOld = locationOld.getTime();
} else {
startLocationUpdates();
}
return mLastLocation;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
onStart(intent, startId);
Toast.makeText(MyService.this, "start command", Toast.LENGTH_SHORT).show();
sendUpdatesToUI = new Runnable() {
public void run() {
DisplayLoggingInfo();
handler.postDelayed(this, 10000); // 5 seconds
}
};
handler.postDelayed(sendUpdatesToUI, 10000); // 1 second
Log.i("LocalService", "Received start id " + startId + ": " + intent);
return START_NOT_STICKY;
}
#Override
public void onStart(Intent intent, int startId) {
sendUpdatesToUI = new Runnable() {
public void run() {
Log.e("sent", "sent");
DisplayLoggingInfo();
handler.postDelayed(this, 5000); // 5 seconds
}
};
handler.postDelayed(sendUpdatesToUI, 1000); // 1 second
Log.i("LocalService", "Received start id " + startId + ": " + intent);
super.onStart(intent, startId);
}
private void DisplayLoggingInfo() {
Log.d(TAG, "entered DisplayLoggingInfo");
intentforBroadcast.putExtra("distance", distance);
LocalBroadcastManager.getInstance(this).sendBroadcast(intentforBroadcast);
}
#Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
#Override
public void onMapReady(GoogleMap googleMap) {
}
}
I made some changes in above code to look for a location fix by both GPS and Network for about 5sec and give me the best known location out of it.
public class LocationService implements LocationListener {
boolean isGPSEnabled = false;
boolean isNetworkEnabled = false;
boolean canGetLocation = false;
final static long MIN_TIME_INTERVAL = 60 * 1000L;
Location location;
// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 0; // 10
// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1; // 1 minute
protected LocationManager locationManager;
private CountDownTimer timer = new CountDownTimer(5 * 1000, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
stopUsingGPS();
}
};
public LocationService() {
super(R.id.gps_service_id);
}
public void start() {
if (Utils.isNetworkAvailable(context)) {
try {
timer.start();
locationManager = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
this.canGetLocation = true;
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
Location tempLocation = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (tempLocation != null
&& isBetterLocation(tempLocation,
location))
location = tempLocation;
}
}
if (isGPSEnabled) {
locationManager.requestSingleUpdate(
LocationManager.GPS_PROVIDER, this, null);
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
Location tempLocation = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (tempLocation != null
&& isBetterLocation(tempLocation,
location))
location = tempLocation;
}
}
} catch (Exception e) {
onTaskError(e.getMessage());
e.printStackTrace();
}
} else {
onOfflineResponse(requestData);
}
}
public void stopUsingGPS() {
if (locationManager != null) {
locationManager.removeUpdates(LocationService.this);
}
}
public boolean canGetLocation() {
locationManager = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
return isGPSEnabled || isNetworkEnabled;
}
#Override
public void onLocationChanged(Location location) {
if (location != null
&& isBetterLocation(location, this.location)) {
this.location = location;
}
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public Object getResponseObject(Object location) {
return location;
}
public static boolean isBetterLocation(Location location,
Location currentBestLocation) {
if (currentBestLocation == null) {
// A new location is always better than no location
return true;
}
// Check whether the new location fix is newer or older
long timeDelta = location.getTime() - currentBestLocation.getTime();
boolean isSignificantlyNewer = timeDelta > MIN_TIME_INTERVAL;
boolean isSignificantlyOlder = timeDelta < -MIN_TIME_INTERVAL;
boolean isNewer = timeDelta > 0;
// If it's been more than two minutes since the current location,
// use the new location
// because the user has likely moved
if (isSignificantlyNewer) {
return true;
// If the new location is more than two minutes older, it must
// be worse
} else if (isSignificantlyOlder) {
return false;
}
// Check whether the new location fix is more or less accurate
int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation
.getAccuracy());
boolean isLessAccurate = accuracyDelta > 0;
boolean isMoreAccurate = accuracyDelta < 0;
boolean isSignificantlyLessAccurate = accuracyDelta > 200;
// Check if the old and new location are from the same provider
boolean isFromSameProvider = isSameProvider(location.getProvider(),
currentBestLocation.getProvider());
// Determine location quality using a combination of timeliness and
// accuracy
if (isMoreAccurate) {
return true;
} else if (isNewer && !isLessAccurate) {
return true;
} else if (isNewer && !isSignificantlyLessAccurate
&& isFromSameProvider) {
return true;
}
return false;
}
}
In the above class, I am registering a location listener for both GPS and network, so an onLocationChanged call back can be called by either or both of them multiple times and we just compare the new location fix with the one we already have and keep the best one.
there are better ways to do it as mentioned on android developer sites
http://developer.android.com/guide/topics/location/strategies.html

Android LocationListner cannot find my location

I have the below location listner to get the users current location. I wait for aproximately 45 seconds before giving up, using both network and GPS. The problem is that sometimes (about 15% of the time) I get a null position even after 45 seconds. I have found no patterns on to when this happens. Can anyone shed some light on what might be happening?
public class MyLocationListener implements LocationListener {
private static final float DELTA_ACURACIDADE = 30;
public static final long DEFAULT_TIME_UPDATES = 0;
public static final float DEFAULT_DISTANCE_UPDATES = 0;
private static final long TIME_CONSIDERED_OLD_LOCATION = 1000 * 150;
private Location location;
#Override
public void onLocationChanged(Location newLocation) {
if (newLocation != null && newLocation.getLatitude() != 0.0d && newLocation.getLongitude() != 0.0d) {
boolean isOk = false;
if (this.location == null) {
this.location = newLocation;
} else {
long deltaTime = newLocation.getTime() - this.location.getTime();
boolean isNewer = deltaTempo > 0;
boolean isAlotNewer = deltaTempo > TIME_CONSIDERED_OLD_LOCATION;
int deltaAcuracidade = (int) (this.location.getAccuracy() - newLocation.getAccuracy());
boolean isEqualOrMoreAccurate = deltaAcuracidade >= DELTA_ACURACIDADE;
if ((isNewer && isEqualOrMoreAccurate) || isAlotNewer) {
aceitarNovaLocation = true;
}
if (aceitarNovaLocation) {
this.location = newLocation;
}
}
}
}
public Location getLocation() {
return this.location;
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
public class checkInActivity extends Activity {
private long WAIT_FOR_LOCATION = 1000 * 40;
private Location roteiroLocation;
private MyLocationListener locationListner;
private LocationManager locationManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
this.locationListner = new MyLocationListener();
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MyLocationListener.DEFAULT_TIME_UPDATES,
MyLocationListener.DEFAULT_DISTANCE_UPDATES, this.locationListner);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
MyLocationListener.DEFAULT_TIME_UPDATES, MyLocationListener.DEFAULT_DISTANCE_UPDATES,
this.locationListner);
}
protected void processLocation() {
new AsyncTask<Void, Void, Void>() {
private boolean wasLocationAccepted = false;
private ProgressDialog dialog = new ProgressDialog(checkInActivity.this);
#Override
protected void onPreExecute() {
this.dialog.setMessage("message");
this.dialog.setCancelable(false);
this.dialog.show();
}
#Override
protected Void doInBackground(Void... params) {
Long t = Calendar.getInstance().getTimeInMillis();
Location location = null;
while (Calendar.getInstance().getTimeInMillis() - t < WAIT_FOR_LOCATION) {
location = locationListner.getLocation();
if (location != null && location.getAccuracy() < MIN_ACCURACY_LOCATION
&& location.distanceTo(place) < MIN_ACCURACY_LOCATION) {
wasLocationAccepted = true;
break;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
locationManager.removeUpdates(locationListner);
if (this.dialog.isShowing(){
this.dialog.dismiss();
}
SigoMobileHelper.performOnBackgroundThread(runnable);
return null;
}
#Override
protected void onPostExecute(Void result) {
Log.i(TAG, "AsyncTask.onPostExecute");
finish();
}
}.execute();
}
#Override
protected void onStart() {
processLocation();
super.onStart();
}
}
Your code is OK.
You're not getting location, because there can be low GPS signal or no answer from satellite and app is unable to download location.

Can't find GPS Location

I have a singelton thread-class that sends the gps point to my server. The class holds and refresh the gps position.
FinderThread:
public class FinderThread extends Thread {
private static FinderThread SINGLETON;
public boolean isinterrupt = true;
public int maxage;
public int minage;
public int distance;
public String gender;
public String latitude;
public String longitude;
public String sid;
public Context ctx;
LocationManager gps;
boolean nav;
SharedPreferences pref;
private Handler dlh;
private Handler myLocationHandler;
boolean pause=false;
private FinderThread(Context ctx,Handler dlh, Handler myLocationHandler) {
this.ctx = ctx;
gps = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);
gps.requestLocationUpdates( LocationManager.GPS_PROVIDER,60000, 20, llistener);
pref = ctx.getSharedPreferences("NFF", 0);
sid = pref.getString("sid", "");
this.dlh = dlh;
if (myLocationHandler!=null)
{
this.myLocationHandler = myLocationHandler;
}
if(myLocationHandler != null)
{
myLocationHandler.sendEmptyMessage(0);
}
}
public static synchronized FinderThread getInstance(Context ctx,Handler dlh, Handler myLocationHandler)
{
if (SINGLETON == null) // falls null
{
SINGLETON = new FinderThread (ctx,dlh,myLocationHandler);//instanzieren
}
else
{
SINGLETON.myLocationHandler= myLocationHandler;
SINGLETON.dlh = dlh;
myLocationHandler.sendEmptyMessage(0);
}
return SINGLETON;
}
/*
#Override
public synchronized void start() {
if(SINGLETON != null)
{
SINGLETON.start();
}
}
public synchronized void pause()
{
if(SINGLETON.getState()==State.RUNNABLE || SINGLETON.getState()==State.WAITING)
{
SINGLETON.pause = true;
}
}
public synchronized void tcontinue ()
{
if(SINGLETON.getState()==State.RUNNABLE || SINGLETON.getState()==State.WAITING)
{
SINGLETON.pause = false;
}
}
public synchronized boolean isPaused()
{
return SINGLETON.pause;
}
*/
public synchronized void pause()
{
this.pause = true;
}
public synchronized boolean isPaused()
{
return this.pause;
}
public synchronized void tcontinue ()
{
this.pause = false;
}
#Override
public void run() {
//String locationProvider = LocationManager.GPS_PROVIDER;
//Location lastpoint = gps.getLastKnownLocation(locationProvider);
//longitude = String.valueOf(lastpoint.getLongitude());
//latitude = String.valueOf(lastpoint.getLatitude());
try
{
while (isinterrupt) {
if(!pause)
{
Log.e("NFF", "Finder Thread Begin");
RestConnection r = new RestConnection("finder", dlh, "POST");
r.setParameter("minage", String.valueOf(minage));
r.setParameter("maxage", String.valueOf(maxage));
r.setParameter("distance", String.valueOf(distance));
r.setParameter("gender", String.valueOf(gender));
r.setParameter("latitude", String.valueOf(latitude));
r.setParameter("longitude", String.valueOf(longitude));
r.setParameter("sid", sid);
Log.e("NFF Finder Paramater", String.valueOf(minage)+" "+String.valueOf(maxage)+" "+String.valueOf(distance)+" "+String.valueOf(gender) + " "+String.valueOf(latitude)+" " +String.valueOf(longitude)+ " " + sid );
r.start();
}
Thread.sleep(10000);
}
}
catch (InterruptedException e) {
Log.e("NFF FT", "InterruptedException");
isinterrupt = false;
}
}
private LocationListener llistener = new LocationListener (){
#Override
public void onLocationChanged(Location location) {
if(String.valueOf(location.getLatitude())!= null)
{
latitude = String.valueOf(location.getLatitude());
longitude = String.valueOf(location.getLongitude());
if(myLocationHandler != null)
{
myLocationHandler.sendEmptyMessage(0);
}
}
}
#Override
public void onProviderDisabled(String provider) {
Log.e("NFF", "GPS Disabled");
isinterrupt = true;
}
#Override
public void onProviderEnabled(String provider) {
Log.e("NFF", "GPS Enabled");
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
};
public void saveProperties ()
{
Editor editor = pref.edit();
editor.putInt("maxage", maxage);
editor.putInt("minage", minage);
editor.putInt("distance", distance);
editor.putString("gender", gender);
editor.putString("lat", latitude);
editor.putString("long", longitude);
editor.commit();
}
public void loadProperties ()
{
maxage = pref.getInt("maxage", -1);
minage = pref.getInt("minage", -1);
distance = pref.getInt("distance", -1);
gender = pref.getString("gender", "f");
latitude = pref.getString("lat", "f");
longitude = pref.getString("long", "f");
}
public synchronized void pushSettings()
{
SINGLETON.maxage = maxage;
SINGLETON.minage = minage;
SINGLETON.distance = distance;
SINGLETON.gender = gender;
SINGLETON.latitude = latitude;
SINGLETON.longitude = longitude;
}
}
When i test my app on the emulator and set the position with the emulator control everthing work fine. When i test it on my phone, the droid don't find the position. I have testet the app "GPS Test", there it finds the position within 3 minutes with 5 sattelites.
Please help
It sure looks like you have everything there.
You give yourself the permission in the manifest for FINE_LOCATION, right?
The one suggestion I would make is change your requestLocationUpdates() so that it requests fixes with 0 time between them and 0 distance between them. All GPS recievers I have seen will give you fixes about once a second with this setting, which is pretty much how most gps engines work. Just on the off-chance that the one-minute and 20 m you put in your request are screwing you up. If you get fixes with this suggestion, you can work from there to minimize amount of time gps is on.
Good luck!

Categories

Resources