I have a service running in the background that closes after a while after closing the app. I do not want the service to shut down as the app
Service:
public class GpsService extends Service {
public static final String BROADCAST_ACTION = "com.emersonbarcellos.mototaxi.displayevent";
private final Handler handler = new Handler();
Intent intent;
int counter = 30;
String chamado=null;
private Controller aController = null;
@Override
public void onCreate() {
super.onCreate();
intent = new Intent(BROADCAST_ACTION);
}
@Override
public void onStart(Intent intent, int startId) {
handler.removeCallbacks(sendUpdatesToUI);
handler.postDelayed(sendUpdatesToUI, 1000); // 1 second
}
@Override
public int onStartCommand(Intent intent, int flags, int startId){
super.onStartCommand(intent, flags, startId);
Bundle b = intent.getExtras();
chamado = b.get("ID_CHAMADO").toString();
return startId;
}
private Runnable sendUpdatesToUI = new Runnable() {
public void run() {
DisplayLoggingInfo();
handler.postDelayed(this, 10000); // 30 seconds
}
};
private void DisplayLoggingInfo() {
new LeitoraDeCoordenadas(this);
}
private void tentativa(String lat, String lon){
final HttpPost Online = new HttpPost("http://fabianrepresentacoes.com.br/gcm/server.php?tipo=update_cordenadas&id="+chamado+"&latitude="+lat+"&longitude="+lon);
final HttpClient client = new DefaultHttpClient();
Log.v("aviso", "http://fabianrepresentacoes.com.br/gcm/server.php?tipo=update_cordenadas&id="+chamado+"&latitude="+lat+"&longitude="+lon);
new Thread(new Runnable() {
@Override
public void run() {
try {
HttpResponse responsePOST = client.execute(Online);
String responseBody = EntityUtils.toString(responsePOST.getEntity());
Log.v("aviso", "Resposta " + responseBody);
} catch (Exception e) {
// TODO: handle exception
}
}}).start();
}
private boolean isMyServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
handler.removeCallbacks(sendUpdatesToUI);
super.onDestroy();
}
public class LeitoraDeCoordenadas implements LocationListener {
public LeitoraDeCoordenadas(Context context) {
LocationManager manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 50, this);
}
@Override
public void onLocationChanged(Location location) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
String Lat = String.valueOf(latitude);
String Lon = String.valueOf(longitude);
Log.i("aviso", Lat);
Log.i("aviso", Lon);
tentativa(Lat, Lon);
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}}
LogCat:
03-11 13:00:29.832: E/test(17688): Exception
03-11 13:00:29.852: E/AndroidRuntime(17688): FATAL EXCEPTION: main
03-11 13:00:29.852: E/AndroidRuntime(17688): Process: com.emersonbarcellos.mototaxi, PID: 17688
03-11 13:00:29.852: E/AndroidRuntime(17688): java.lang.RuntimeException: Unable to start service com.emersonbarcellos.mototaxi.GpsService@41a5d2b8 with null: java.lang.NullPointerException
03-11 13:00:29.852: E/AndroidRuntime(17688): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2814)
03-11 13:00:29.852: E/AndroidRuntime(17688): at android.app.ActivityThread.access$2100(ActivityThread.java:145)
03-11 13:00:29.852: E/AndroidRuntime(17688): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1362)
03-11 13:00:29.852: E/AndroidRuntime(17688): at android.os.Handler.dispatchMessage(Handler.java:102)
03-11 13:00:29.852: E/AndroidRuntime(17688): at android.os.Looper.loop(Looper.java:136)
03-11 13:00:29.852: E/AndroidRuntime(17688): at android.app.ActivityThread.main(ActivityThread.java:5214)
03-11 13:00:29.852: E/AndroidRuntime(17688): at java.lang.reflect.Method.invokeNative(Native Method)
03-11 13:00:29.852: E/AndroidRuntime(17688): at java.lang.reflect.Method.invoke(Method.java:515)
03-11 13:00:29.852: E/AndroidRuntime(17688): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:814)
03-11 13:00:29.852: E/AndroidRuntime(17688): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:630)
03-11 13:00:29.852: E/AndroidRuntime(17688): at dalvik.system.NativeStart.main(Native Method)
03-11 13:00:29.852: E/AndroidRuntime(17688): Caused by: java.lang.NullPointerException
03-11 13:00:29.852: E/AndroidRuntime(17688): at com.emersonbarcellos.mototaxi.GpsService.onStartCommand(GpsService.java:48)
03-11 13:00:29.852: E/AndroidRuntime(17688): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2797)