I want to implement my database helper class as a service, but it already extends SQLiteOpenHelper... is there an interface for Service available in android?
Dunno if it is a good idea, but anyways, using inheritance does not make a lot of sense in this case - you should go with composition. Your service should extend Service class and include DatabaseHelper as an instance variable.
Related
I have:
a MainActivity
a OtherActivity
a DatabaseService, which is instantiated in MainActivity and which holds Sqliteconnection and functions like List<data> getData()
How do I pass the DatabaseService to my OtherActivity ?
So far my DatabaseService was a Singleton and I referenced that Singleton from the OtherActivity, but I dont like that Solution.
I can't also pass it with putExtra, because the Object is too complex to serialize.
There should be a more easier way to inject or pass the Service to the activity.
Does anyone have any hint for me ?
What you need to do is binding your activities to the service (in your case: DatabaseService). Highly recommend you follow [this dev guide] https://developer.android.com/guide/components/bound-services and choose the simplest way to create a bound service: extending the binder class. So for short, what you need to do:
implement your service following the sample LocalService
config in AndroidManifest.xml:
<service android:name=".YourNewService" android:enabled="true"/>
for activities needs to interact with the service, just implement them similar to BindingActivity of the guide. (No worries on service instantiation, singleton, etc.)
Please note that you always can create a base BindingActivity class that handles all binding cores and let other activities extend the base binding class.
Happy coding!
I want to bind my custom firebase service to the MainActivity class. if it was a normal service, it would be easily done by making the service implement the onBind method, but it is not available because this method is set final in the superclass of FirebaseMessagingService class. I want to bind the service in order to use the LiveData variable set inside this class in the MainActivity class.
I have searched for best practices here and read this documentation about bound services but no luck. I tend to use LiveData somehow but I feel it is not working until I somehow manage to bind the service. am I missing something?
As suggested in a comment by #CôngHải, I can use a singleton object with LiveData inside it to communicate between service and activity without the need of binding the service to activity.
Hey guys so i want to use getReadableDatabase() in my main activity but the problem is that it is already extending appCompatActivity() and i need it to do that for my code. But now since i cant extend 2 things at a time ,i cant extend SQLiteOpenHelper and that leads to not being able to use getreadable. Can anyone help me with this issue?
You need to define create a second class that extends SQLiteOpenHelper and define methods within there that use getReadableDatabase(). You can set up SQLiteOpenHelper as a singleton so that you can access a single instance of the class from anywhere.
See http://blog.anorakgirl.co.uk/2018/01/refactoring-sqliteopenhelper-so-it-isnt-a-mile-long/ for how you can structure your SQLiteOpenHelper class.
I am new to Android and here i am stuck at a point .
There is a class (say class_A) , MainActivity and a Service class(which extends IntentService)
I have an instance of Class_A in my MainActivity which gets updated with user's response . I want to use the same instance in my Service class , so that on the basis of updated instance of Class_A i can perform task present in my Service Class (the service runs indefinitely) .
Any kind of help is appreciated . Or if the above approach is wrong please do suggest other methods of doing it .
I think I understand what you're asking. What you're probably looking for is a Singleton (which some people consider evil) or shared preferences http://developer.android.com/reference/android/content/SharedPreferences.html
This might be a silly question, but I'm struggling on how to make my class extend 2 classes at the same time. I'm trying to make a SERVICE that uses ListActivity. How could I do that?
Java or android Doesn't Support Multiple Inheritance
I assume you are coding in the Java programming language. Then the simple answer is: You don't. Java does not support deriving from multiple classes.
Make your ListActivity contain a Service.
class MyService extends Service{
...
}
class MyList extends ListActivity{
MyService service = new MyService();
}
In Java you can't extend more than 1 class at the same time. However, you can implement more than 1 interface (but this is not about your task).
You should look for bound service
No, it is not possible. You may need to re-architect your implementation.
You can never actually extend 2 classes directly, you need to use a few tricks and make a few interfaces to get it to work. Here is a guide: http://csis.pace.edu/~bergin/patterns/multipleinheritance.html
Assuming you mean this kind of Service, you cannot do this at all; irrespective of Java's lack of multiple inheritance. Services cannot also be Activities. You should create a Service, and a separate ListActivity, and use a binder to communicate between them.
When you do want to inherit the functionality of two classes, use the delegation pattern.