Android locationManager - "location not changed" event - android

I'm building a service that will let me know if the location hasn't change in a period of time for a certain amount of metres.
Then thing is I have the event onLocationChanged on my Listener.. but I don't know how to do the opposite.. that is, send a broadcast if the location is within the distance I provided after some minutes.
This is the code I have so far
LocationService
public class LocationService extends Service {
public static final String LOC_INTENT = "com.xxx.intent.action.LOCATION";
private Thread triggerService;
protected LocationManager locationManager;
protected MyLocationListener MyLocationListener;
protected Criteria criteria;
public static final int MIN_TIME = 300000; // 5 Minutes
public static final long MIN_DISTANCE_MOTOR = 50; // 50 Metres
private SharedPreferences settings;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId)
{
settings = getSharedPreferences(getString(R.string.settings_prefsName), 0);
addLocationListener();
return START_STICKY;
}
private void addLocationListener()
{
triggerService = new Thread(new Runnable(){
public void run(){
try{
Looper.prepare();//Initialise the current thread as a looper.
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
final String PROVIDER = locationManager.getBestProvider(criteria, true);
updateLocation(getLastBestLocation(MIN_TIME, MIN_DISTANCE_MOTOR));
MyLocationListener = new MyLocationListener();
locationManager.requestLocationUpdates(PROVIDER, MIN_TIME, MIN_DISTANCE_MOTOR, MyLocationListener);
Log.d("LOC_SERVICE", "Service RUNNING! ("+PROVIDER+")");
Looper.loop();
}catch(Exception ex){
ex.printStackTrace();
}
}
}, "LocationThread");
triggerService.start();
}
public Location getLastBestLocation(int minDistance, long minTime) {
Location bestResult = null;
float bestAccuracy = Float.MAX_VALUE;
long bestTime = Long.MIN_VALUE;
List<String> matchingProviders = locationManager.getAllProviders();
for (String provider: matchingProviders) {
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
float accuracy = location.getAccuracy();
long time = location.getTime();
if ((time > minTime && accuracy < bestAccuracy)) {
bestResult = location;
bestAccuracy = accuracy;
bestTime = time;
}
else if (time < minTime && bestAccuracy == Float.MAX_VALUE && time > bestTime) {
bestResult = location;
bestTime = time;
}
}
}
return bestResult;
}
public static void updateLocation(Location location)
{
Context appCtx = MyApplication.getAppContext();
double latitude, longitude;
float speed;
latitude = location.getLatitude();
longitude = location.getLongitude();
speed = location.getSpeed();
Intent filterRes = new Intent();
filterRes.setAction(LOC_INTENT);
filterRes.putExtra("latitude", latitude);
filterRes.putExtra("longitude", longitude);
filterRes.putExtra("speed", speed);
appCtx.sendBroadcast(filterRes);
}
class MyLocationListener implements LocationListener
{
#Override
public void onLocationChanged(Location location)
{
if(settings.getBoolean("active", false))
updateLocation(location);
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
}

Set a timer for however long you want to test this. When it goes off, check if the last location you got in onLocationChanged is older than the timer length.
EDIT
Here is how I would imagine your service looking
Service starting
requestLocationUpdates called with appropriate minimum time and minimum distance that that you will be notified after
Repeating task set where you check if an update was received (check out Timer.scheduleAtFixedRate
Service running
Perform necessary actions when your timer goes off or onLocationChanged is called
Service stopping
Remove location updates with removeUpdates
Stop your timer

Related

Android AsyncTask & LocationListener

I created a class GPSController. The onReceive is called every Minute.
I want to get the current GPS Data with the GPSListener in my AsyncTask.
But the onLocationChanged Method is never called. :-/
Here is my Class:
public class GPSController extends SensorTimeController {
// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 0;
// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 2;
private boolean isGPSEnabled = false;
private boolean isNetworkEnabled = false;
private double latitude = -1;
private double longitude = -1;
private double altitude = -1;
private double speed = 0L;
private double bearing = 0L;
private LocationManager locationManager;
private GPSLocationTask backgroundTask;
public GPSController(GPSModule module) {
super(module);
backgroundTask = new GPSLocationTask();
}
private void SensorDataFinished(double longitude, double latitude, double altitude, double speed, double bearing) {
Date date = new Date(System.currentTimeMillis());
SensorRecord record = new SensorRecord(module.getNextIndex(), date, structure);
if (longitude != -1)
record.addData("longitude", String.valueOf(longitude));
else
record.addData("longitude", " ");
if (latitude != -1)
record.addData("latitude", String.valueOf(longitude));
else
record.addData("latitude", " ");
if (altitude != -1)
record.addData("altitude", String.valueOf(altitude));
else
record.addData("altitude", " ");
if (bearing != 0L)
record.addData("bearing", String.valueOf(bearing));
else
record.addData("bearing", " ");
if (speed != 0L)
record.addData("speed", String.valueOf(speed));
else
record.addData("speed", " ");
module.log(record);
}
#Override
public void onReceive(Context context, Intent intent) {
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
backgroundTask.execute();
}
private class GPSLocationTask extends AsyncTask<Void, Void, Void> implements LocationListener {
#Override
protected Void doInBackground(Void... params) {
System.out.println("THREAD STARTED");
try {
Looper.prepare();
Looper.loop();
if(isNetworkEnabled)
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
if(isGPSEnabled)
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
}catch(SecurityException e) {
e.printStackTrace();
}
return null;
}
public void onLocationChanged(Location location) {
if (location != null) {
longitude = location.getLongitude();
latitude = location.getLatitude();
altitude = location.getAltitude();
// don't do anything if we get a null reading for some reason
if (longitude == 0.0f && latitude == 0.0f)
return;
// any speed information?
if (location.hasSpeed())
speed = (double) location.getSpeed();
// any bearing information?
if (location.hasBearing())
bearing = (double) location.getBearing();
System.out.println(longitude+", "+ latitude+", "+ altitude+", "+ speed+", "+ bearing);
SensorDataFinished(longitude, latitude, altitude, speed, bearing);
}
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
}

Gps tracking in android

I'm trying to write application that track my gps coordinates.
Every 10 seconds i want to send my coordinates to server - for this i using AlarmManager.
For getting coordinates i'm using Service that implement onClickListener.
How i start service:
public void startAlarm() {
Intent intent = new Intent(this, GpsService.class);
PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0);
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, 0,
10 * 1000, pintent);
}
My gps service:
public class GpsService extends Service implements LocationListener {
// Declaring a Location Manager
private LocationManager locationManager;
Location location; // location
double latitude; // latitude
double longitude; // longitude
double accuracy;
// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 0; // 10 meters
// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 10 * 1; //10secs
#Override
public void onCreate() {
super.onCreate();
if(locationManager == null ) {
locationManager = (LocationManager) this
.getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
}
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
getLocation();
if (location != null) {
//send to server
}
return super.onStartCommand(intent, flags, startId);
}
#Override
public void onDestroy() {
locationManager.removeUpdates(this);
locationManager = null;
super.onDestroy();
}
#Override
public void onLocationChanged(Location location) {
Log.v("myLogs", "GetLocation service: ONLOCATIONCHANGED");
this.location = location;
}
public Location getLocation() {
try {
// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!isGPSEnabled ) {
stopSelf();
} else {
this.canGetLocation = true;
if (isGPSEnabled) {
if (location == null) {
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
}
It work perfect on emulator, but on real device it only show GPS Finding icon. What is problem?

Getting Speed from Android GPS

I want to get the speed out of the LocationManager in my app. I have a Criteria with setSpeedRequired(true) attribute. I am doing location.getSPeed(), but it gives me 0 all the time. Below is the code for the GPS which I am running as a Service.
public class Tracking extends Service implements LocationListener {
protected LocationManager locationManager;
Location location;
double latitude;
double longitude;
float velocity;
String provider;
private static final long minDist = 0;
private static final long minTime = 0;
LocationDatabaseHandler ldb;
#Override
public void onCreate() {
ldb = new LocationDatabaseHandler(this);
getLocation();
}
#Override
public void onDestroy() {
super.onDestroy();
}
#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 intent) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent,flags,startId);
//because we do not want to stop the service unless we explicitly say so.
return START_STICKY;
}
public Location getLocation() {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setSpeedRequired(true);
provider = locationManager.getBestProvider(criteria, false);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTime, minDist,
this);
location = locationManager.getLastKnownLocation(provider);
if(location != null) {
onLocationChanged(location);
}
return location;
}
#Override
public void onLocationChanged(Location location) {
latitude = location.getLatitude();
longitude = location.getLongitude();
batteryP = getBatteryPerc();
velocity = location.getSpeed();
insertIntoDb(latitude, longitude, velocity, "onchanged");
}
public void insertIntoDb(double latitude, double longitude, float velocity, String where) {
Date date = new Date();
String dateStr = date.toString();
ldb.addLocation(new Locations(latitude, longitude, dateStr, velocity));
}
}
When I see my database, the velocity is always 0.0. Is there something I am missing?
locationManager.requestLocationUpdates(provider, minTime, minDist, this);

onLocationChanged never gets called, even after requesting for updates

Start Service:
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent i = new Intent(this, LocationService.class);
pi = PendingIntent.getService(this, 0, i,
PendingIntent.FLAG_UPDATE_CURRENT);
am.cancel(pi);
am.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime(), 30*1000, pi);
Service:
public class LocationService extends Service implements LocationListener {
public static double curLat = 0.0;
public static double curLng = 0.0;
private LocationManager mgr;
private String best;
private Location location;
#Override
public IBinder onBind(Intent arg0) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
mgr = (LocationManager) getSystemService(LOCATION_SERVICE);
best = LocationManager.NETWORK_PROVIDER;
location = mgr.getLastKnownLocation(best);
if (location != null) {
dumpLocation(location);
mgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
5 * 1000, 0, this);
}
return START_NOT_STICKY;
}
}
private void dumpLocation(Location l) {
SimpleDateFormat s = new SimpleDateFormat("dd/MM/yyyy:hh:mm:ss",
Locale.ENGLISH);
String format = s.format(l.getTime());
//The above time is always 21/03/2013:09:53:41 which is more than a week old
curLat = l.getLatitude();
curLng = l.getLongitude();
}
#Override
public void onLocationChanged(Location location) {
//This never seems to be called
dumpLocation(location);
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
Every 30 secs the service is being called, but the requestLocationUpdates never seems to call onLocationChanged. And when I checked the time of the lastKnownLocation its a week old.
Applications like foursquare, local, maps etc gives the current location with GPS disabled.
What am I missing?
I think you need to move from somewhere with your device because onLocationChange only called if the location was change. And the network location was approximate near by 1000-2000 meter so need to check on street where the location was change and getting the new location. If you used gps then it will giving you accurate but it also nearby 20-30 meter after fixed all it.

keep application running in background

I want to keep my application running in background
I have an application that sends the user's location to our server
I have the following code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager locationManager;
String context = Context.LOCATION_SERVICE;
locationManager = (LocationManager)getSystemService(context);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
String provider = locationManager.getBestProvider(criteria, true);
updateWithNewLocation(null);
locationManager.requestLocationUpdates(provider, (10*60*1000), 10,
locationListener);
}
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
updateWithNewLocation(location);
}
public void onProviderDisabled(String provider){
updateWithNewLocation(null);
}
public void onProviderEnabled(String provider){ }
public void onStatusChanged(String provider, int status,
Bundle extras){ }
};
public void updateWithNewLocation(Location location) {
if (location != null) {
Dbhelper helper = new Dbhelper(this);
final SQLiteDatabase db = helper.getWritableDatabase();
long time = System.currentTimeMillis();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
final String curTime = df.format(time);
final double lat = location.getLatitude();
final double lng = location.getLongitude();
final double alt = location.getAltitude();
System.out.println(lat);
System.out.println(lng);
System.out.println(alt);
db.execSQL("INSERT INTO location (longitude,latitude,altitude,tgl_buat) VALUES " +
"('"+lng+"','"+lat+"','"+alt+"','"+curTime+"')");
db.close();
/*Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask(){
#Override
public void run(){
db.execSQL("INSERT INTO location (longitude,latitude,altitude,tgl_buat) VALUES " +
"('121.2149012','-6.217837','0.0','2012-05-07 10:20:01')");
db.close();
}
}, 10*60*1000, 10*60*1000);*/
}
}
I want my applicatioin to be running in the background. I want it to launch automatically when the phone is turned on
A very simple answer for your problem is to use Service. It will allow you to perform variety of tasks while being in background and is your best bet for sending your location to server silently.
Read this answer for help.
You can keep your application running in the background using Service
I hope this link will help you
Please read the documentation for further details
Run your background logic in a Service, and if you want to give a good UX experience (and to also have an higher priority) post a Notification to status-bar (using NotificationManager).
GrabLocationDetails.java
Use this code as your GrabLocationDetails.java
public class GrabLocationDetails extends Service implements LocationListener {
double lat,lng;
private LocationManager locationManager;
private String provider;
boolean isGps;
private ArrayList<String> mList;
Context GLDContext;
public GrabLocationDetails(Context cont){
this.GLDContext=cont;
}
public GrabLocationDetails(){}
#Override
public void onCreate() {
super.onCreate();
mList = new ArrayList<String>();
isGps = false;
lat=0.0;
lng=0.0;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
//super.onStart(intent, startId);
try {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
boolean enabled = service.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!enabled) {
isGps = false;
ListAddItem(isGps);
SendBroadcast();
} else {
isGps = true;
Location location = locationManager.getLastKnownLocation(provider);
lat=(location.getLatitude());
lng=(location.getLongitude());
ListAddItem(true);
SendBroadcast();
locationManager.requestLocationUpdates(provider, 400, 1, this);
}
} catch (Exception e) {
ListAddItem(isGps);
SendBroadcast();
}
return super.onStartCommand(intent, flags, startId);
}
#Override
public void onDestroy() {
super.onDestroy();
//locationManager.removeUpdates(this);
}
public void SendBroadcast(){
Intent broadcastIntent = new Intent();
broadcastIntent.setAction(CommandExecutionModule.LocationDetails);
broadcastIntent.putExtra("Data", mList);
sendBroadcast(broadcastIntent);
}
public void ListAddItem(boolean GPS) {
//if(!GPS)
//mList.add("0");
//else
//mList.add("1");
mList.add(Double.toString(lat));
mList.add(Double.toString(lng));
}
/**************************************************************************************************************/
#Override
public void onLocationChanged(Location location){
locationManager.requestLocationUpdates(provider, 400, 1, this);
mList.clear();
lat = (location.getLatitude());
lng = (location.getLongitude());
ListAddItem(isGps);
SendBroadcast();
locationManager.removeUpdates(this);
stopSelf();
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
locationManager.requestLocationUpdates(provider, 400, 1, this);
}
#Override
public void onProviderEnabled(String provider){
isGps=true;
}
#Override
public void onProviderDisabled(String provider){
isGps=false;
lat=0.0;
lng=0.0;
mList.clear();
ListAddItem(isGps);
//SendBroadcast();
}
You should use a Service and a BroadcastReceiver

Categories

Resources