Accessing Instance of a class from IntentService Class - android

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

Related

How to pass a service-object to an activity in 2020

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!

Adding listner for a service

I was wondering if there is a way to have a listener for a Service.In my scenario I have a Service class S that is initiated by a class A . Service class S has a method S().I want to have a callback to class A when the method s() is done executing .Is it possible in any way .Any link or reference or even a code snipet will be realy appreciated.
Use a Localbroadcastreceiver to notify the activity when method S() finished execution.
Check this question for details about how to use a Localbroadcastreceiver
There is no such thing as listener for a service.

How to handle Android Java objects which "extends Activity"?

Let's say I have two classes:
Class Show is my normal Android activity.
Class Work is a Java class which does a lot of work:
public class Work extends Activity {...}
As you can see, Work extends Activity. Thats because it needs some methods that are only in Activity (I don't mean methods which regard the UI).
In my Activity Show I make an Objekt of class Work
protected void onCreate(){
Work mWork = new Work();
mWork.doSomething();
}
My question:
How shall I handle my object Work regarding its lifecycle? Is it like a normal Java object and I don't have to care about its lifecycle, or is it like a normal activity and i have to call finish()? I am confused because it's kind of both.
Based on our discussion in the comments, the answer here would be to pass the Context of your current activity to the target class, and use the accessible methods through that context instance to do whatever task it is you want, as the Activity class basically extends ApplicationContext.
For example (in a class not extending Activity) I can add:
myLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
and use myLocationManager as I please. Also, this way the object created locally would be set for garbage collection upon destruction of your main Activity.
Thought it'd be worth having an answer others could refer to rather than scrounging through the comments.
you should read this to understand activity lifecycle.

Interface for Service class?

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.

Application differs from activity to broadcastreceiver

I have my own singleton class which inherits from Application. In all my activities I get this shared class by using DataClass dc = (DataClass) context.getApplicationContext();.
this seems to work but when I trie to acces the singleton in a custom BroadCastReceiver I have an other instance of the Application.
Is that possible? Does anyone have a solution for this?
This is expected behaviour.
If i am not mistaken, a BroadcastReceiver runs on a different process then your activity. That means you'll have a different instance of Application on your BroadcastReceiver.

Categories

Resources