Error terminating the java android service

Asked

Viewed 116 times

2

I have a service running in the background that ends after a while after closing the app. I don’t want the service to end when closing 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)
  • What is line 48...?

  • I edited the question, Face the truth is I don’t know where is/happens the error that causes it to close when I’m not with the open application

  • 1

    Maybe the b.get("ID_CHAMADO") is returning null.

  • (I can not comment so write here). Use the Log. d("DEBUG","No Oncreate"), for example, to know where you are getting the error, so it is easier to know where you are getting the exception. Possibly it will be here: intent = new Intent(BROADCAST_ACTION);

  • The question is misspelled, It gives to understand that you want the service can not be removed from memory by Android. A solution to this problem would be to start the service with startForeground(), maintaining a "permanent" notification. The problem with this question is simply a bug.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.