Skip to content

Instantly share code, notes, and snippets.

Created February 2, 2011 16:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save anonymous/807968 to your computer and use it in GitHub Desktop.
Save anonymous/807968 to your computer and use it in GitHub Desktop.
Background Service
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Binder;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;
public class Background extends Service implements LocationListener{
private NotificationManager mNM;
private static LocationManager lm;
private static Timer timer1;
/**
* Class for clients to access. Because we know this service always
* runs in the same process as its clients, we don't need to deal with
* IPC.
*/
public class LocalBinder extends Binder {
Background getService() {
return Background.this;
}
}
@Override
public void onCreate() {
mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
showNotification();
Criteria criteria=new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(lm.getBestProvider(criteria, true), 8000, 1, this);
timer1=new Timer();
final Handler mHandler = new Handler(Looper.getMainLooper());
timer1.scheduleAtFixedRate(new TimerTask(){
@Override
public void run() {
mHandler.post(new Runnable() {
public void run() {
Coords coords=getBestLocation();
sendCoords(String.valueOf(coords.getLat()),String.valueOf(coords.getLng()));
}
});
}
}, 0, 3000);
}
private static Coords getBestLocation() {
Coords coords=new Coords();
List<String> providers = lm.getProviders(true);
//Loop over the array backwards, and if you get an accurate location, then break out the loop
Location l = null;
for (int i=providers.size()-1; i>=0; i--) {
l = lm.getLastKnownLocation(providers.get(i));
if (l != null) break;
}
if (l != null) {
coords.setLat(l.getLatitude());
coords.setLng(l.getLongitude());
}
return coords;
}
public static void sendCoords(String lat,String lng){
Handler handler = new Handler() {
public void handleMessage(Message message) {
switch (message.what) {
case HttpConnection.DID_START:
//do nothing
break;
case HttpConnection.DID_SUCCEED:
String response = (String) message.obj;
Log.i("response",response);
break;
case HttpConnection.DID_ERROR:
Exception e = (Exception) message.obj;
e.printStackTrace();
break;
}
}
};
List<NameValuePair> params = new ArrayList<NameValuePair>(2);
params.add(new BasicNameValuePair("lat", String.valueOf(lat)));
params.add(new BasicNameValuePair("lng", String.valueOf(lng)));
new HttpConnection(handler).post("http://xxx.xxx.xxx.xxx/driver/ajax/updateCoords.php",params);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Cookie.setPHPSessID(intent.getStringExtra("PHPSESSID"));
//Toast.makeText(this,PHPSESSID, Toast.LENGTH_SHORT).show();
// We want this service to continue running until it is explicitly stopped, so return sticky.
return START_STICKY;
}
@Override
public void onDestroy() {
timer1.cancel();
mNM.cancelAll();
lm.removeUpdates(this);
Toast.makeText(this,"Stopped", Toast.LENGTH_SHORT).show();
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
public boolean onUnbind(Intent intent){
timer1.cancel();
mNM.cancelAll();
lm.removeUpdates(this);
return true;
}
// This is the object that receives interactions from clients. See
// RemoteService for a more complete example.
private final IBinder mBinder = new LocalBinder();
private void showNotification() {
CharSequence text = "Active";
Notification notification = new Notification(R.drawable.icon, text,System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(this,0,new Intent(this,Main.class),0);
notification.setLatestEventInfo(this,"eoh",text,contentIntent);
notification.flags|=Notification.FLAG_ONGOING_EVENT;
mNM.notify("EOH", 0, notification);
}
@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) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment