How to bind to a Service that was started by another instance of the application

Raphael Royer-Rivard

I have a background Service that need to be running even if the application gets killed by Android. This is currently working perfectly.

My problem is that when I restart the application (with the background service still running), I want my Activity to bind to the service to have access to some of its methods. When I try to bind with a ServiceConnection, the onServiceConnected is never called.

final private ServiceConnection serviceConnection = new ServiceConnection() {

    public void onServiceConnected(ComponentName className, IBinder service) {
        Log.d(TAG, "onServiceConnected");  //this is never called
        MyBackgroundService.ServiceBinder binder = (MyBackgroundService.ServiceBinder) service;
        backgroundService = binder.getService();
    }

    public void onServiceDisconnected(ComponentName className) {
        Log.d(TAG, "onServiceDisconnected");
        backgroundService = null;
    }

};

private void bindBackgroundService(){
    this.bindService(new Intent(this, MyBackgroundService.class), serviceConnection, Context.BIND_AUTO_CREATE);
}

Am I doing this the wrong way? Is it better to stop the Service and restart it?

Raphael Royer-Rivard

Since the class that binded the background service is a singleton and my alarm broadcast receiver that is making sure the background service is always running instantiates this singleton, I had access to this singleton and I was trying to bind to the service that was already binded.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Can an application bind on the intentService of another application, when the intent service is not started/running?

From Dev

How to start a Service when the Android application is started?

From Dev

How to start a Service when the Android application is started?

From Dev

How do I bind to Android service that is started after boot completed?

From Dev

How to call a service from another application?

From Dev

How to call a service from another application?

From Dev

How to capture output of a command window that is started by another application?

From Dev

how to bind Application controller property in another controller ember

From Dev

How to Bind the Property of an Instance?

From Dev

How to obtain service credentials for a service instance created on IBM Bluemix without binding the instance to an application on Bluemix?

From Dev

How to debug an application which is started from an another application ? (VB6)

From Dev

How to debug an application which is started from an another application ? (VB6)

From Dev

MS Access, How to determine the command line parameters that the current instance of application has started with?

From Dev

Is a separate Python instance started for each instance of a Django application via Apache?

From Dev

Getting a service started automatically when another gets started

From Dev

Angular6 How to bind share data with sibling with service so that each instance has its reference seperatly

From Dev

Android AIDL: How to get service instance in activity when service is in another process?

From Dev

How to have a Java application invoke PowerPoint through JACOB when started as a service?

From Dev

How to create single instance WPF Application that restores the open window when an attempt is made to open another instance?

From Dev

How to access instance of service?

From Dev

how to bind a command when fish shell is started

From Dev

confusing bind. how to bind an object to another?

From Dev

How to cancel a task if new instance of task is started?

From Dev

How to provide service in another service

From Dev

Android : Can bind to a service but cannot receive instance variables

From Dev

How to find if a particular service is run/started when JBOSS AS is started?

From Dev

Passing argument to another instance of the same application

From Dev

Expose variable to instance of web application in another tab?

From Dev

Register an instance of a service in Prism.Unity which relies on another service

Related Related

  1. 1

    Can an application bind on the intentService of another application, when the intent service is not started/running?

  2. 2

    How to start a Service when the Android application is started?

  3. 3

    How to start a Service when the Android application is started?

  4. 4

    How do I bind to Android service that is started after boot completed?

  5. 5

    How to call a service from another application?

  6. 6

    How to call a service from another application?

  7. 7

    How to capture output of a command window that is started by another application?

  8. 8

    how to bind Application controller property in another controller ember

  9. 9

    How to Bind the Property of an Instance?

  10. 10

    How to obtain service credentials for a service instance created on IBM Bluemix without binding the instance to an application on Bluemix?

  11. 11

    How to debug an application which is started from an another application ? (VB6)

  12. 12

    How to debug an application which is started from an another application ? (VB6)

  13. 13

    MS Access, How to determine the command line parameters that the current instance of application has started with?

  14. 14

    Is a separate Python instance started for each instance of a Django application via Apache?

  15. 15

    Getting a service started automatically when another gets started

  16. 16

    Angular6 How to bind share data with sibling with service so that each instance has its reference seperatly

  17. 17

    Android AIDL: How to get service instance in activity when service is in another process?

  18. 18

    How to have a Java application invoke PowerPoint through JACOB when started as a service?

  19. 19

    How to create single instance WPF Application that restores the open window when an attempt is made to open another instance?

  20. 20

    How to access instance of service?

  21. 21

    how to bind a command when fish shell is started

  22. 22

    confusing bind. how to bind an object to another?

  23. 23

    How to cancel a task if new instance of task is started?

  24. 24

    How to provide service in another service

  25. 25

    Android : Can bind to a service but cannot receive instance variables

  26. 26

    How to find if a particular service is run/started when JBOSS AS is started?

  27. 27

    Passing argument to another instance of the same application

  28. 28

    Expose variable to instance of web application in another tab?

  29. 29

    Register an instance of a service in Prism.Unity which relies on another service

HotTag

Archive