android.os.deadobjectexception in service android

13,354

Solution 1

you are using the instance of an object that no longer exist in memory as parent process is stopped.

Solution 2

Add this in Service Class. This will stop the app from crashing. 'onTaskRemoved' triggers when app is removed from ram

@Override
    public void onTaskRemoved(Intent rootIntent){
        stopSelf();
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        super.onTaskRemoved(rootIntent);
    }

 @Override
    public void onDestroy() {
        super.onDestroy();
//Your Runnable
        mRunnable=null;
    }
Share:
13,354
user3243651
Author by

user3243651

Updated on June 09, 2022

Comments

  • user3243651
    user3243651 almost 2 years

    I have an application which takes data values (coordinates) from a service, and works fine, but crashes after about seven or eight minutes.

    In logcat there appears a lot of this message:

    02-24 09:50:35.761: E/RemoteException(6395): android.os.DeadObjectException

    these messages are from the application not service, but I suppose that is because the service fails?

    [UPDATE]

    With the comments I understand better that the problems is caused by service's failure, but I read this question How to fix android.os.DeadObjectException android X (similar to mine) but the answer... is some confuse to me.

    this is my ondestroy():

    @Override
    public void onDestroy() {
    
     Toast.makeText(this, "Servicio destruido", Toast.LENGTH_SHORT).show();
     Log.d("SERVICEBOOT", "Servicio destruido");
     capture.control(0);
    
    }
    

    How can I know which element closed my service?

  • Ana
    Ana about 10 years
    I think you service is stopped as the Activity it has been bound to is destroyed.
  • ono
    ono over 9 years
    So the solution is to stop the service in the Activities onDestroy?
  • Ana
    Ana over 9 years
    yes if you dont need it after activity is finished otherwise you can unbind service in on Destroy and keep service running but to access services instance remember to make the instance static.
  • Alberto MQO
    Alberto MQO about 3 years
    Why is the Thread.sleep needed here? Thanks!