Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In android is there a way to check if the service is already bound before binding to it?

I have multiple fragment activities and one of them is bound to a service that is constantly receiving messages. While I was switching between activities at one point I get the following error. I do not know how the List item is being clicked where the service is being bounded. In anyways I was wondering if I can add a check before I bind to see if the service is already running and connected.

04-08 10:34:54.674: E/ActivityThread(21091): Activity com.MyApp.MyFirstClass has leaked ServiceConnection com.MyApp.MyFirstClass$2@42c84158 that was originally bound here
04-08 10:34:54.674: E/ActivityThread(21091): android.app.ServiceConnectionLeaked: Activity com.MyApp has leaked ServiceConnection com.MyApp.MyFirstClass$2@42c84158 that was originally bound here
04-08 10:34:54.674: E/ActivityThread(21091):    at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:974)
04-08 10:34:54.674: E/ActivityThread(21091):    at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:868)
04-08 10:34:54.674: E/ActivityThread(21091):    at android.app.ContextImpl.bindServiceCommon(ContextImpl.java:1960)
04-08 10:34:54.674: E/ActivityThread(21091):    at android.app.ContextImpl.bindService(ContextImpl.java:1943)
04-08 10:34:54.674: E/ActivityThread(21091):    at android.content.ContextWrapper.bindService(ContextWrapper.java:529)
04-08 10:34:54.674: E/ActivityThread(21091):    at com.MyApp.MyFirstClass.onItemClick(MyFirstClass.java:1140)
04-08 10:34:54.674: E/ActivityThread(21091):    at android.widget.AdapterView.performItemClick(AdapterView.java:313)
04-08 10:34:54.674: E/ActivityThread(21091):    at android.widget.AbsListView.performItemClick(AbsListView.java:1529)
04-08 10:34:54.674: E/ActivityThread(21091):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:3560)
04-08 10:34:54.674: E/ActivityThread(21091):    at android.widget.AbsListView$3.run(AbsListView.java:5277)
04-08 10:34:54.674: E/ActivityThread(21091):    at android.os.Handler.handleCallback(Handler.java:733)
04-08 10:34:54.674: E/ActivityThread(21091):    at android.os.Handler.dispatchMessage(Handler.java:95)
04-08 10:34:54.674: E/ActivityThread(21091):    at android.os.Looper.loop(Looper.java:146)
04-08 10:34:54.674: E/ActivityThread(21091):    at android.app.ActivityThread.main(ActivityThread.java:5635)
04-08 10:34:54.674: E/ActivityThread(21091):    at java.lang.reflect.Method.invokeNative(Native Method)
04-08 10:34:54.674: E/ActivityThread(21091):    at java.lang.reflect.Method.invoke(Method.java:515)
04-08 10:34:54.674: E/ActivityThread(21091):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
04-08 10:34:54.674: E/ActivityThread(21091):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
04-08 10:34:54.674: E/ActivityThread(21091):    at dalvik.system.NativeStart.main(Native Method)
like image 774
JourneyWithAndroid Avatar asked Oct 20 '25 14:10

JourneyWithAndroid


1 Answers

Not sure if you ever found an adequate answer, but I just created a wrapper class I called ExtendedServiceConnection which basically had the ability to track and check whether it's Service was bound.

Just a tad cleaner then holding a global in Constants.

 private class ExtendedServiceConnection implements ServiceConnection {

    private boolean isServiceConnected = false;

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        Timber.d("Service Connection established with " + name.getShortClassName());
        isServiceConnected=true;
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        Timber.d("Service Connection destroyed with " + name.getShortClassName());
        isServiceConnected=false;
    }

    public boolean isServiceConnected() {
        return isServiceConnected;
    }
}

Usage

public void onReceivePause() {
    if (mExtendedServiceConnection.isServiceConnected()) {
        unbindService(orientationServiceConnection);
    }
    handleNotifications(mPackage.isIntentionalPaused());
}
like image 189
orange-lotus Avatar answered Oct 22 '25 03:10

orange-lotus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!