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.
Related
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.
I have an Activity called MainActivity that starts a Service called MainService. It is also binds the Service, so MainActivity can access methods and public variables within MainService. Is it possible to do it the other way round, i.e. the Service is able to access the Activity's methods?
I wish to implement it this way because I have a variable in MainActivity that is set upon onResume(), and on first startup the service has not yet started by the time onResume() runs, so at that point in time the service is still null.
This answer assumes that the Service in question runs in a different process:
Yes, it is possible. The general idea is that not only your Activity binds the remote Service through some AIDL defined interface, but it also implements additional AIDL interface which the Service is aware of, and sets itself as a callback target of the remote Service.
You'll have to have 2 AIDL files: the first one describes the interface of the Service, and the second one describes the interface of the Activity.
The implementation of such a scheme is very similar to "remote Service callbacks" described in this answer, though "callback" method would no longer be void, but return the value you're interested in.
Design considerations:
The above scheme will allow you to get values from Activity, but I don't think you should take this path. From the description of your use case, it looks that you only want to pass some value to the Service when Activity gets resumed. Since your Service is bound anyway, you can simply add a method setSomeValue(int value) to its AIDL definition and call this method from onServiceConnected() callback.
Yes it's possible.
You have to prepare method in your service to return back your activity just after service is bound:
public void bindActivity(MyActivity activity){...}
Then after service is bound to activity just call this method with MyActivity.this as parameter.
However...
You probably should not do it. Much more clear solution is using LocalBroadcastManager to pass events and data or use some more efficient solutions like Otto to do this same, but still - without direct access to one component's fields / methods from another.
I am executing something in an async class(from activity A), and before the result comes I start another activity B.
Now I want to execute a block of code in B based on result from the async class.
I couldn't find a proper solution over the internet. Thus posting it here.
Define your own MyApp extends Application class and declare it in your manifest as <application android:name="mypackage.MyApp></application>
Then have a public field myResult in your My App. Then you can store your result and get your result from that public field as ((MyApp)getApplication()).myResult
This is because MyApp singleton object exists all the time your app is running.
If you want notification you can use an event bus such as Otto
Or you can just send a broadcast and catch it by a broadcast receiver in your other activity..
There are a lot of possible solutions to do this. Let me add my favourites:
Event Bus (Square's Otto or GreenDroid's one). In this case you need to subscribe your activity to get the result.
Loaders - native android solution, no libs needed. To cook this one you need to use content provider, which contains the table with results and in your async task there should be a method to insert result into db.
I am new to android programming. I want to create a very simple widget in which I just have a single button and on click of that button, I want to execute some code.
My question is, is it necessary to create an activity for this?
Or can I just extend AppWidgetProvider class and write execution code in onUpdate or onReceive method?
Yes it is necessary to create a class that extends activity to accomplish interaction with a button.
In theory the provider is enough to handle appwidgets. In practice you will always need some kind of configuration, which is an Activity.
It is also very likely that the code to run, is too much for onUpdate and onReceive. At this moment you'll send Intent's out to some Actvity or Service.
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