Bind Android services and callbacks to fields and methods.

Overview

ServiceConnector

Field and method binding for connecting with Android remote AIDL (or Remoter) services.

Replace the boiler plate service connection codes like -

public class ActivityWithoutServiceConnector extends Activity {

    //remote service interface
    private IEchoService mEchoService;

    /**
     * Service connection that gets call back when service is connected
     */
    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder service) {
            //get the service 
            mEchoService = IEchoService.Stub.asInterface(service);
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            mEchoService = null;
        }
    };


    /**
     * Connect with the remote service
     */
    private void conectWithService() {
        Intent serviceIntent = new Intent("util.serviceconnector.ECHO_SERVICE");
        serviceIntent.setComponent(new ComponentName("util.serviceconnector.service", "util.serviceconnector.service.EchoService"));
        startService(serviceIntent);
        bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
        conectWithService();
    }

}

Replace that with

public class ActivityWithServiceConnector extends Activity {

    @ServiceInfo(serviceIntent = "util.serviceconnector.ECHO_SERVICE")
    private IEchoService mEchoService;

    @ServiceConnectionCallback
    public void onServiceConnectionChanged(String serviceIntent, boolean connected) throws RemoteException {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
        ServiceConnector.bind(this, this);
    }
}

Getting ServiceConnector

dependencies {
  compile 'com.josesamuel:serviceconnector:1.0.6'
}

Java Doc

ServiceConnector Java Doc

License

Copyright 2018 Joseph Samuel

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
You might also like...
General purpose utilities and hash functions for Android and Java (aka java-common)

Essentials Essentials are a collection of general-purpose classes we found useful in many occasions. Beats standard Java API performance, e.g. LongHas

DiskCache - Simple and readable disk cache for kotlin and android applications

DiskCache Simple and readable disk cache for kotlin and android applications (with journaled lru strategy) This is a simple lru disk cache, based on t

Android Shared preference wrapper than encrypts the values of Shared Preferences. It's not bullet proof security but rather a quick win for incrementally making your android app more secure.
Android Shared preference wrapper than encrypts the values of Shared Preferences. It's not bullet proof security but rather a quick win for incrementally making your android app more secure.

Secure-preferences - Deprecated Please use EncryptedSharedPreferences from androidx.security in preferenced to secure-preference. (There are no active

Android library which makes it  easy to handle the different obstacles while calling an API (Web Service) in Android App.
Android library which makes it easy to handle the different obstacles while calling an API (Web Service) in Android App.

API Calling Flow API Calling Flow is a Android library which can help you to simplify handling different conditions while calling an API (Web Service)

Gesture detector framework for multitouch handling on Android, based on Android's ScaleGestureDetector

Android Gesture Detectors Framework Introduction Since I was amazed Android has a ScaleGestureDetector since API level 8 but (still) no such thing as

Use Android as Rubber Ducky against another Android device
Use Android as Rubber Ducky against another Android device

Use Android as Rubber Ducky against another Android device

Android Utilities Library build in kotlin Provide user 100 of pre defined method to create advanced native android app.

Android Utilities Library build in kotlin Provide user 100 of pre defined method to create advanced native android app.

a simple cache for android and java

ASimpleCache ASimpleCache 是一个为android制定的 轻量级的 开源缓存框架。轻量到只有一个java文件(由十几个类精简而来)。 1、它可以缓存什么东西? 普通的字符串、JsonObject、JsonArray、Bitmap、Drawable、序列化的java对象,和 b

A lightning fast, transactional, file-based FIFO for Android and Java.

Tape by Square, Inc. Tape is a collection of queue-related classes for Android and Java. QueueFile is a lightning-fast, transactional, file-based FIFO

Comments
  • Fix listener leak on ServiceFailureCallbacks

    Fix listener leak on ServiceFailureCallbacks

    Callbacks were added on initListerners method but never removed, which caused a leak on bind retries. The leak was fixed by removing failure callbacks at unbindTarget, along with service callbacks (using the same logic).

    We would appreciate if you could also publish a this fix :)

    Thanks!

    opened by rafaelrabeloit 1
  • How can I handle RemoteException?

    How can I handle RemoteException?

    how can i handle RemoteException that is thrown by

    @ServiceConnectionCallback public void onServiceConnectionChanged(String serviceIntent, boolean connected) throws RemoteException { }

    opened by rrodovalho-stone 1
Releases(v1.0.3)
Owner
Joseph Samuel
Joseph Samuel
A beautiful set of predefined colors and a set of color methods to make your Android development life easier.

Colours is a port of the Colours Library for iOS made by my good friend Ben Gordon. You can find that project here. Installation Maven Central Colours

Matthew York 634 Dec 28, 2022
Thrift for Android that saves you methods

Thrifty Thrifty is an implementation of the Apache Thrift software stack for Android, which uses 1/4 of the method count taken by the Apache Thrift co

Microsoft 528 Dec 28, 2022
Generate helper methods for compose navigation using KSP

Compose NavGen Generate helper methods for compose navigation using KSP. ?? You can try it now, but it's still under development. ?? TODO Support defa

Kenji Abe 6 Feb 5, 2022
A set of helper classes for using dagger 1 with Android components such as Applications, Activities, Fragments, BroadcastReceivers, and Services.

##fb-android-dagger A set of helper classes for using dagger with Android components such as Applications, Activities, Fragments, BroadcastReceivers,

Andy Dennie 283 Nov 11, 2022
Type safe intent building for services and activities

#IntentBuilder Type safe intent building for services and activities. IntentBuilder is a type safe way of creating intents and populating them with ex

Emil Sjölander 348 Oct 10, 2022
gRPC and protocol buffers for Android, Kotlin, and Java.

Wire “A man got to have a code!” - Omar Little See the project website for documentation and APIs. As our teams and programs grow, the variety and vol

Square 3.9k Dec 31, 2022
General purpose utilities and hash functions for Android and Java (aka java-common)

Essentials Essentials are a collection of general-purpose classes we found useful in many occasions. Beats standard Java API performance, e.g. LongHas

Markus Junginger 1.4k Dec 29, 2022
Access and process various types of personal data in Android with a set of easy, uniform, and privacy-friendly APIs.

PrivacyStreams PrivacyStreams is an Android library for easy and privacy-friendly personal data access and processing. It offers a functional programm

null 269 Dec 1, 2022
A simple and easy to use stopwatch and timer library for android

TimeIt Now with Timer support! A simple and easy to use stopwatch and timer library for android Introduction A stopwatch can be a very important widge

Yashovardhan Dhanania 35 Dec 10, 2022
Trail is a simple logging system for Java and Android. Create logs using the same API and the library will detect automatically in which platform the code is running.

Trail Trail is a simple logging system for Java and Android. Create logs using the same API and the library will detect automatically in which platfor

Mauricio Togneri 13 Aug 29, 2022