Singleton service's init not accessible from AppComponentFactory? - android

I am using Kotlin to build an Android app. I have a Service that is also a singleton. When I call startService() in an Activity, I get the following exception: java.lang.IllegalAccessException: void com.mydomain.socket.SocketService.<init>() is not accessible from java.lang.Class<android.app.AppComponentFactory>
Does anyone know what is causing this and how to work with a singleton service? The reason I implemented this as a singleton is because I wanted to be able to get a reference from that service easily anywhere in my app. To implement the singleton, I simply declared my Service as object instead of class.

I ran into this same problem and I found that I have declared a default constructor for the service class but did not specify it as public.
Since it is private, the AppComponentFactory class from another namespace is not able to create an instance of the service.
Add "public" to the constructor.

Related

Android, for JNI should System.loadLibrary be called in an InputMethodService?

When sub-classing InputMethodService where native code is used, Android will kill the service when the input method is dismissed.
Later, Android will restart the service and JNI is not present causing Java unable to find the native functions and an exception thrown.
I had previously thought that only one System.loadLibrary in MainActivity was required. Should this be added to services too?
There is no extra cost in putting LoadLibrary in all relevant classes, so go for it. Use it in static constructors of each Java class, or companion object of Kotlin class
MainActivity is not the best choice to begin with, because every time you later add another activity or service that can be started independently, you must not forget to duplicate your LoadLibrary.

Interface between service and activity

I have a service bound to an activity. I defined an interface that is implemented by the activity. In the service I have an object of the interface that implements the activity, i give memory to this object when I call the method that returns the binder.
The service implements socket.io when I receive a message from the server, I call the interface method to update data in the activity.
My question is, am I using bad practices? Should I implement a LocalBroadcastReceiver to communicate with the activity instead of using the interface?
what do you suggest me?
Short answer is yes it is really a bad practice.
Long answer, even though you reference your activity over an interface it's still the same object in the memory. So let's say you have long running operation on your service then when the activity is recreated after a rotation or any kind of configuration change your old reference will be kept in the Service and it will be leaked.
So since your question is too generic I can just list the alternative methods, you can look through all of them and apply whichever fits on your style.
EventBus (Publish/Subscribe pattern, the easiest solution)
Dependency Injection (Use Dagger or similar to inject your model on
both Activity and Service
BroadcastReceiver
Messenger

Obtaining Context for Geolocation within an IntentService

I am creating an Android Class library that makes calls to an internal REST API and also utilizes Android's Geocoder class (specifically getFromLocation()) which requires Context. I was planning on making the library an IntentService to allow for it to be run asynchronously, but I can't figure out how to handle Context (the GeoLoc call is in a separate class that is part of the library that the IntentService calls).
My question is, how do I obtain context necessary to instantiate android.location.Geocoder from within an IntentService?
Every Service is a Context. You can use this or getApplicationContext().
IntentService Inherits Service, and Service Inherits Context.
You are free to use "this" as Context whenever you needs it inside your IntentService.

Binding and connecting an android service outside an activity

I would like to bind and connect a service but not within an android activity. Is there a class witch could be extended to have a context necessary for binding?
What i am trying to do is to provid a simple java library using an android service. My library does not use a UI. I only need to bind and connect my service inside a class witch necessary have application context necessary to the binding
Thanks
You can get the context from your application class. Derive your own class from Application, and give it a static getApplication method. You can use that for creating services.
Note that without an Activity, binding to a service may be a little hard - if, for example, you're in a BroadcastReceiver, it's not going to be alive long enough for you to receive the callback after the service has been bound.
Simply create an application without default activity. Then extend base Service class. And do not forget to describe it in the manifest file.
Service has its own context.

How can I share an Object between a running Service and Activity

I have an API Object that I made, and I would like to share it between a running Service and various Activities in my app, almost like if I was to make the class static. How could I go about sharing the created object between the two?
You may find the following helpful.
Binding a Service to an android.app.Activity vs Binding it to an android.app.Application
Android Service interacting with multiple activities
Alternatives for Pushing data from an Android Service to an Activity
I figured out the best way to do this is to have a class that holds all the information that you want, and to use that class through each activity and service.

Categories

Resources