Starting LocationManager as Service Android - android

What I'm attempting to do is when receiving a c2dm message, start a service that asks for location for 'x' amount of time and then hands that location off to our server. The c2dm message starts the service correctly, and the GPS location turns on, but it never updates. It just sits there for the length of time I specify (currently 12 seconds) in the thread and does nothing. I'm using the exact same code somewhere else in my app (not as a service) and it works perfectly. What am I doing wrong?
This starts the service when receiving a c2dm message.
context.startService(new Intent(context, ServicePingLocation.class));
This is the code for the service itself. All that ever gets called, is "onCreate" and "onStart".
public class ServicePingLocation extends Service implements LocationListener {
private final String DEBUG_TAG = "[GPS Ping]";
private boolean xmlSuccessful = false;
private boolean locationTimeExpired = false;
private LocationManager lm;
private double latitude;
private double longitude;
private double accuracy;
#Override
public void onLocationChanged(Location location) {
Log.d(DEBUG_TAG, "onLocationChanged");
latitude = location.getLatitude();
longitude = location.getLongitude();
accuracy = location.getAccuracy();
}
#Override
public void onProviderDisabled(String provider) {
Log.d(DEBUG_TAG, "onProviderDisabled");
Toast.makeText(
getApplicationContext(),
"Attempted to ping your location, and GPS was disabled.",
Toast.LENGTH_LONG).show();
}
#Override
public void onProviderEnabled(String provider) {
Log.d(DEBUG_TAG, "onProviderEnabled");
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 10f, this);
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d(DEBUG_TAG, "onStatusChanged");
}
#Override
public void onCreate() {
Log.d(DEBUG_TAG, "onCreate");
}
#Override
public void onDestroy() {
Log.d(DEBUG_TAG, "onDestroy");
}
#Override
public IBinder onBind(Intent intent) {
Log.d(DEBUG_TAG, "onBind");
return null;
}
#Override
public void onStart(Intent intent, int startid) {
Log.d(DEBUG_TAG, "onStart");
lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 10f, this);
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000,
300f, this);
Log.d(DEBUG_TAG, lm.toString());
new SubmitLocationTask(ServicePingLocation.this).execute();
}
private void locationTimer() {
new Handler().postDelayed(new Runnable() {
// #Override
#Override
public void run() {
locationTimeExpired = true;
}
}, 12000);
}
private class SubmitLocationTask extends AsyncTask<String, Void, Boolean> {
/** application context. */
private Context context;
private Service service;
public SubmitLocationTask(Service service) {
this.service = service;
context = service;
}
#Override
protected void onPreExecute() {
locationTimer(); // Start 12 second timer
}
#Override
protected void onPostExecute(final Boolean success) {
if (success && xmlSuccessful) {
lm.removeUpdates(ServicePingLocation.this);
onDestroy();
} else {
if (!GlobalsUtil.DEBUG_ERROR_MSG.equals(""))
Toast.makeText(getBaseContext(),
GlobalsUtil.DEBUG_ERROR_MSG, Toast.LENGTH_SHORT)
.show();
GlobalsUtil.DEBUG_ERROR_MSG = "";
}
}
#Override
protected Boolean doInBackground(final String... args) {
try {
DateFormat df = null;
df = new SimpleDateFormat("M/d/yy h:mm a");
Date todaysDate = new Date();// get current date time with
// Date()
String currentDateTime = df.format(todaysDate);
while ((accuracy > 100f || accuracy == 0.0)
&& !locationTimeExpired) {
// We just want it to sit here and wait.
}
return xmlSuccessful = SendToServerUtil.submitGPSPing(
0, longitude,
latitude, accuracy, currentDateTime);
} catch (Exception e) {
return false;
}
}
}
}
[Edit]
Fixed the issue I was having. Code was actually working. I added the network provider, adjusted the onDestroy() method to stop the service, and tweaked the time used to grab GPS signal.
Thank you for the advice, CommonsWare

Fixed the issue I was having. Code was actually working. I added the network provider, adjusted the onDestroy() method to stop the service, and tweaked the time used to grab GPS signal.
Thank you for the advice, CommonsWare

Related

how to timeout requestLocationUpdates?

When no GPS fix found, the code is getting stuck indefinitely in the looper.
I want to add a timeout so that if there is no GPS fix found, it should come out of the looper and execute the remaining part of the code.
I will really appreciate if you can help me in fixing this issue.
public class service_task extends Service {
#SuppressLint("MissingPermission")
#Override
public void onCreate() {
}
#Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
private class ServerThread extends Thread implements LocationListener {
public LocationManager locationManager = null;
public String msg = "default";
public String id = "default";
private Location mLocation = null;
public Socket socket = null;
public int serviceid;
public ServerThread(LocationManager locationManager, int startid) {
super("UploaderService-Uploader");
this.locationManager = locationManager;
this.serviceid=startid;
}
#SuppressLint("MissingPermission")
public void run() {
Looper.prepare();
this.locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
Looper.loop();
if (mLocation!=null) {
msg = "GPS data:" + mLocation;
}else{
msg ="No GPS data";
}
stopSelf(serviceid);
}
#Override
public void onLocationChanged(Location location) {
mLocation = location;
Log.d("D", String.valueOf(location));
this.locationManager.removeUpdates(this);
Looper.myLooper().quit();
}
#Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
#Override
public void onProviderEnabled(String s) {
}
#Override
public void onProviderDisabled(String s) {
}
}
#SuppressLint("MissingPermission")
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("D", "startcommand");
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
ServerThread thread = new ServerThread(locationManager,startId);
thread.start();
return START_REDELIVER_INTENT;
}
}
Create a Handler that uses your Looper: handler = new Handler(Looper.myLooper());
Then use handler.postDelayed(Runnable, long) to post a new Runnable that cancels location updates and quits your Looper after a given delay.

Why LocationManager is null after initialization? I'm trying to removeUpdates()

I have a class "OldLocationService" (generally I'm using fused location from GoogleApiClient but I keep that class in case of old Google Play):
public class OldLocationService {
static Location loc;
private static final String TAG = MainActivity.class.getSimpleName();
protected LocationManager service;
private final LocationListener mLocationListener = new LocationListener() {
#Override
public void onLocationChanged(final Location location) {
Log.d(TAG, "New location: " + location.toString());
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onProviderDisabled(String provider) {
}
};
public void EnableGPS(String provider, Context ctx) {
service = (LocationManager) ctx.getSystemService(ctx.LOCATION_SERVICE);
boolean enabled = service.isProviderEnabled(provider);
if (enabled) {
service.requestLocationUpdates(provider, 10000, 0, mLocationListener, Looper.getMainLooper());
}
else
{
Log.d(TAG, "GPS is not enabled");
}
}
public void DisableGPS() {
try {
if (!(service==null)) {
service.removeUpdates(mLocationListener);
} else {
Log.d(TAG, "service is null");
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
Somewhere in another class I'm calling:
OldLocationService OLS = new OldLocationService();
OLS.EnableGPS(LocationManager.NETWORK_PROVIDER, mContext);
And I'm getting locations correctly. However, when I'm trying to disable GPS:
OldLocationService ols = new OldLocationService();
ols.DisableGPS();
Then in logcat I get:
service is null
Why I can't remove GPS updates? How to do that?
Sorry for my English errors,
Defozo
First of all don't make 2 instances of your OldLocationService class.. you must remove updates from the same instance from which you started them.. because each instance has its own copy of variables/fields, in your case protected LocationManager service;
OldLocationService ols = new OldLocationService(); // declare it globally
// in middle of some code
OLS.EnableGPS(LocationManager.NETWORK_PROVIDER, mContext);
// more code here
If you're removing Location updates from a Service then you must put the code in it's onDestroy() method from the same instance:
public void onDestroy(){
ols.DisableGPS();
super.onDestroy();
}

Android location updates in a Service

I want to do something like this:
When my application starts I want to start a Service which should check my location
When the application goes to background I want to stop the service
I have two major problems:
How can I detect that my application goes to background? I haver several activities, and I tried that in my MainActivity overriding onPause, but the onPause is also called when I start an other activity.
This problem is more important: How should my Service which checks for my location look like? I tried several approaches, but no success.
My Service looks like this, and it's not working. What should I change to make it work?
package com.pivoscore.service;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
public class LocationService extends Service {
private LocationListener locationListener;
#Override
public IBinder onBind(final Intent intent) {
return null;
}
#Override
public int onStartCommand(final Intent intent, final int flags, final int startId) {
super.onStartCommand(intent, flags, startId);
return Service.START_STICKY;
}
#Override
public void onCreate() {
super.onCreate();
final LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
this.locationListener = new MyLocationListener();
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 100, 0, this.locationListener);
}
private static class MyLocationListener implements LocationListener {
#Override
public void onLocationChanged(final Location location) {
}
#Override
public void onProviderDisabled(final String provider) {
}
#Override
public void onProviderEnabled(final String provider) {
}
#Override
public void onStatusChanged(final String provider, final int status, final Bundle extras) {
}
}
}
This Will Help exaclty what your requirment is
but don't forget to Add the permission i've added in comment
and also don't forget to added service tag in manifest File
Code Snipet is Following
public class LocationService extends Service {
public static final String BROADCAST_ACTION = "Hello World";
private static final int TWO_MINUTES = 1000 * 60 * 2;
public LocationManager locationManager;
public MyLocationListener listener;
public Location previousBestLocation = null;
Intent intent;
int counter = 0;
#Override
public void onCreate() {
super.onCreate();
intent = new Intent(BROADCAST_ACTION);
}
#Override
public void onStart(Intent intent, int startId) {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
listener = new MyLocationListener();
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, listener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
protected 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 > TWO_MINUTES;
boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES;
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;
}
/** Checks whether two providers are the same */
private boolean isSameProvider(String provider1, String provider2) {
if (provider1 == null) {
return provider2 == null;
}
return provider1.equals(provider2);
}
#Override
public void onDestroy() {
// handler.removeCallbacks(sendUpdatesToUI);
super.onDestroy();
Log.v("STOP_SERVICE", "DONE");
locationManager.removeUpdates(listener);
}
public static Thread performOnBackgroundThread(final Runnable runnable) {
final Thread t = new Thread() {
#Override
public void run() {
try {
runnable.run();
} finally {
}
}
};
t.start();
return t;
}
public class MyLocationListener implements LocationListener
{
public void onLocationChanged(final Location loc)
{
Log.i("**************************************", "Location changed");
if(isBetterLocation(loc, previousBestLocation)) {
loc.getLatitude();
loc.getLongitude();
intent.putExtra("Latitude", loc.getLatitude());
intent.putExtra("Longitude", loc.getLongitude());
intent.putExtra("Provider", loc.getProvider());
sendBroadcast(intent);
/*final Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
String Text = "";
try {
List<Address> addresses = geocoder.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1);
Text = "My current location is: "+addresses.get(0).getAddressLine(0);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Text = "My current location is: " +"Latitude = " + loc.getLatitude() + ", Longitude = " + loc.getLongitude();
}
*/
//Toast.makeText( getApplicationContext(), "Location polled to server", Toast.LENGTH_SHORT).show();
}
}
public void onProviderDisabled(String provider)
{
Toast.makeText( getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT ).show();
}
public void onProviderEnabled(String provider)
{
Toast.makeText( getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show();
}
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
}
Application is not a visual component in Android. It is divided into Activities, each of them run when visible, paused and destroyed otherwise. So, there is no concept of whole Application going to background, Activities are paused and resumed on the basis of their individual visibility and are completely independent of other Activities in this matter.
Your Service shall register with Location Manager in its onCreate(), unregister from the same in its onDestroy(). In its onBind() it shall return a Messenger object. And, in onLocationChanged() it should send a message through its shared Messenger. No need to use START_STICKY as you don't want Service running all the time.
The Activity (can be any activity in the App) just needs to call bindService() in its onStart(), The service will start if not already, and Activity will get a Messenger from service. Also, Activity should call unbindService() from its onStop(). The Service will automatically stop when nothing is bound to it.
If you need to do the stuff in point 3 at App (Task) level, implement the Application class, and use its onCreate() and onTerminate(). Application class is not paused or stopped like an Activity.
I suggest you to use the latest location api provided by google(also compatible to version 2.2).
here is a example shows how to use that:
https://github.com/chenjishi/android_location_demo
when your app run to background, you can call disconnect to stop the location update.
Try This code.. By using this you can find whether your application is in foreground or background. Hope this will help you.
try {
foreground = new ForegroundCheckTask().execute(ctx).get();
}
======================================
class ForegroundCheckTask extends AsyncTask<Context, Void, Boolean> {
#Override
protected Boolean doInBackground(Context... params) {
final Context context = params[0].getApplicationContext();
return isAppOnForeground(context);
}
private boolean isAppOnForeground(Context context) {
ActivityManager activityManager = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> appProcesses = activityManager
.getRunningAppProcesses();
if (appProcesses == null) {
return false;
}
final String packageName = context.getPackageName();
String activePackageName = activityManager.getRunningTasks(1).get(0).topActivity.getPackageName();
if (activePackageName.equals(packageName)) {
return true;
}
else{
return false;
}
}
}

How to get location after every 5 minutes?

I am using this link for location service and it works
Now I want to create BackgroundService that make calls to a function that gets location after every 5 minutes.
I think I need to use Timer for this, please tell me how to manage this 5 minutes gap in between this location class gets called.
public class LocationService extends Service {
private Timer timer;
private long UPDATE_INTERVAL ;
public static final String Stub = null;
LocationManager mlocmag;
LocationListener mlocList ;
private double lat,longn;
#Override
public void onCreate() {
super.onCreate();
webService = new WebService();
mlocmag = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
mlocList = new MyLocationList();
Location loc = mlocmag.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (loc == null) {
loc = mlocmag.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
timer = new Timer(); // location.
UpdateWithNewLocation(loc); // This method is used to get updated
mlocmag.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,mlocList);
}
#Override
public IBinder onBind(Intent arg0) {
return null;
}
#Override
public void onDestroy() {
super.onDestroy();
if (timer != null) {
timer.cancel();
}
mlocmag.removeUpdates(mlocList);
}
#Override
public boolean stopService(Intent name) {
return super.stopService(name);
}
private void UpdateWithNewLocation(final Location loc) {
final SharedPreferences prefs = getSharedPreferences(Const.COMMON_SHARED, Context.MODE_PRIVATE);
userId = prefs.getString(Const.COMMON_USERID, null);
gps = prefs.getInt(Const.COMMON_GPS, 0);
UPDATE_INTERVAL = 500000;
timer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
if (loc != null) {
final double latitude = loc.getLatitude(); // Updated lat
final double longitude = loc.getLongitude(); // Updated long
String response = null ;
if (lat != latitude || longn != longitude ) {
response = webService.updateLatandLong(userId, latitude, longitude);
lat = latitude;
longn = longitude;
}
}
else {
String latLongStr = "No lat and longitude found";
}
}
}, 0, UPDATE_INTERVAL);
}
public class MyLocationList implements LocationListener {
public void onLocationChanged(Location arg0) {
UpdateWithNewLocation(arg0);
}
public void onProviderDisabled(String provider) {
Toast.makeText(getApplicationContext(), "GPS Disable ",
Toast.LENGTH_LONG).show();
}
public void onProviderEnabled(String provider) {
Toast.makeText(getApplicationContext(), "GPS enabled",
Toast.LENGTH_LONG).show();
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
}
use This:
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
//your code to get lat long
}
}, 0, 500000);

Android: GPS listener gets cached location data

I have a fairly straightforward piece of code, which is is a Service that runs periodically, records the current location (using network provider), sends it to the server, then goes back to sleep.
I am testing this on two different phones - an SGS2 with a regular monthly plan and a cheapo ZTE with a prepaid SIM card (has data, but minutes are 10c/min). I've found that when I take both phones and go for a drive, the SGS2 works perfectly fine, but ZTE seems to lose the ability to get a fix.
The ZTE wakes up, sets up the listener, gets a location fix, however the location points to my house (where it got the last wifi-based fix), not the true current location. The timestamp of the location is up to date, so when I receive a location update, I really can't tell whether the location is valid (as in the SGS2, or when the ZTE is at home) or bunk (such as when I'm driving with the ZTE).
Has anyone seem similar problems before? Does it have anything to do with the prepaid card, or the ZTE phone itself? Unfortunately, I can't swap the SIM cards (I would have to root/unlock the phones), so I can't test that out.
I've included the code below, but since it works fine on the SGS2, I don't think there's much of a problem.
public class LocationRecorder extends Service {
private volatile Location lastLocation;
private LocationManager locationManager;
private LocationListener locationListener;
private static volatile PowerManager.WakeLock wakeLock = null;
private static synchronized PowerManager.WakeLock getWakeLock(Context context) {
if (wakeLock == null) {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "LocationRecorder");
wakeLock.acquire();
}
return wakeLock;
}
public static void startLocationRecorder(Context context, Intent service) {
getWakeLock(context);
context.startService(service);
}
#Override
public void onCreate() {
Log.d("LocationRecorder", "Starting Location Service");
locationManager = ((LocationManager)getSystemService(LOCATION_SERVICE));
locationListener = new LocationListener() {
#Override
public void onLocationChanged(Location location) {
Log.d("Location Changed", location.toString());
if (location.getExtras()!=null) {
String x = "";
for (String key : location.getExtras().keySet()) {
x+=key+":"+location.getExtras().get(key).toString()+", ";
}
Log.d("Location Changed Extras", x);
}
setLocation(location);
}
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d("Status Changed", provider+" "+status);
if (extras!=null) {
String x = "";
for (String key : extras.keySet()) {
x+=key+":"+extras.get(key).toString()+", ";
}
Log.d("Status Changed Extras", x);
}
}
public void onProviderEnabled(String provider) {
Log.d("Provider Enabled", provider);
}
public void onProviderDisabled(String provider) {
Log.d("Provider Disabled", provider);
}
};
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
waitForLocation();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return Service.START_REDELIVER_INTENT;
}
#Override
public void onDestroy() {
locationManager.removeUpdates(locationListener);
try {
wakeLock.release();
wakeLock = null;
}
catch (Exception e) {
wakeLock = null;
}
Log.d("LocationRecorder", "Destroying service");
super.onDestroy();
}
protected void waitForLocation() {
new Thread() {
#Override
public void run() {
setLocation(null);
for (int i=0; i<3;i++) {
Log.d("LocationRecorder", "Waiting for location");
try {Thread.sleep(10000);} catch(Exception e) {};
if (getLocation() != null) {
Log.d("LocationRecorder", "Sending new location!");
new Utilities(LocationRecorder.this).updateLocation(getLocation().getLatitude(),
getLocation().getLongitude(), getLocation().getAccuracy());
break;
}
}
stopSelf();
}
}.start();
}
public synchronized void setLocation(Location newLocation) {
lastLocation = newLocation;
}
public synchronized Location getLocation() {
return lastLocation;
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
This is expected behaviour. You may just need to wait longer before you destroy the location manager to get a more up to date location.
Here's a better description from Google Developer Docs.

Categories

Resources