Hi All,
I am developing a location based application in android, main objective of
this application is to
retrieve location based information from server by Latitude and longitude.
This location change functionality is implemented in android background
service
with help of Location Manager, the location change will happen every
100meters that i am specified in location listener.
It is working fine while device is in use.
Application stops location change updates after device went to inactive
mode (after 1hr or 2hr).
the Location change get wake up After phone gets on.
Please find my sample code below.
public class BackgroundServiceForLocation extends Service
{
LocationManager locationManagerObj;
GeoLocationListener locationListener;
long minTime = 60000;
float minDistance =
Float.parseFloat(String.valueOf(Constants.GEOFENCE_VALUE_DEFAULT));
....Other Declerations
* Called by the system when the service is first created. Do not call
this method directly.
*/
@Override
public void onCreate()
{
super.onCreate();
getLocationUpdates();
}
/*
* Called by the system to notify a Service that it is no longer used
and is being removed
*/
@Override
public void onDestroy()
{
super.onDestroy();
try
{
if(locationManagerObj!=null && locationListener!=null)
{
locationManagerObj.removeUpdates(locationListener);
locationManagerObj = null;
locationListener = null;
}
}
catch(Exception e)
{
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
// TODO Auto-generated method stub
return START_STICKY;
}
/*
* Return the communication channel to the service.
* @params
* intent - The Intent that was used to bind to this service
*/
@Override
public IBinder onBind(Intent intent)
{
// TODO Auto-generated method stub
return null;
}
/*
* used to get the location based on LOCATION SERVCIE
*/
private void getLocationUpdates()
{
// TODO Auto-generated method stub
try {
// This class provides access to the system location services.
if (locationManagerObj == null)
{
locationManagerObj =
(LocationManager)getSystemService(Context.LOCATION_SERVICE);
try
{
network_enabled =
locationManagerObj.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
CommonFunctions.WriteLog("Network Provider is Enabled");
}
catch (Exception ex)
{
CommonFunctions.WriteLog("Error in Network Provider
enable, Message:" + ex.getMessage());
Toast.makeText(this.context,"Problem in
NETWORK_PROVIDER", 3).show();
}
if(!network_enabled)
{
try
{
gps_enabled =
locationManagerObj.isProviderEnabled(LocationManager.GPS_PROVIDER);
CommonFunctions.WriteLog("GPS provider Enabled");
}
catch (Exception ex)
{
CommonFunctions.WriteLog("Error in GPS provider
enable, Message:" + ex.getMessage());
Toast.makeText(this.context,"Problem in
GPS_PROVIDER", 3).show();
}
}
}
if(network_enabled)
{
locationListener = new GeoLocationListener(this.context);
locationListener.locationResult =locationResult;
//locationManagerObj.re
locationManagerObj.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, minTime,
minDistance,
locationListener);
}
else if(gps_enabled)
{
locationListener = new GeoLocationListener(this.context);
locationListener.locationResult =locationResult;
locationManagerObj.requestLocationUpdates(
LocationManager.GPS_PROVIDER, minTime, minDistance,
locationListener);
}
else
{
return;
}
}
catch (Exception ex)
{
ex.getMessage();
}
}
public Object sharedObject;
public GeoLocationListener.LocationResult locationResult = new
GeoLocationListener.LocationResult()
{
@Override
public void gotLocation(final Location location)
{
//Here to call a webservice and pass latitude and longitude
}
};
}
Joy..
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en