I want to create an interface which will interact with an already running Service from the Activity in foreground.
viz, if there is a service called MyService running in background and I want to use the methods defined in the service from an activity called MyActivity then how will I do it.
There are several possibilities for an activity to communicate with a service and vice versa.
LocalBroadcast receiver that is provided by Android framework , v4 support library also provided.
AIDL for services in a different process
Handler and ResultReceiver or Messenger
To get details implementation visit following links :
http://www.vogella.com/tutorials/AndroidServices/article.html
http://developer.android.com/guide/components/services.html
You need to make your service bindable. or more specifically something like this LocalService taken from the android guide.
The above answers are pretty apt ....but if you are bent on interfaces then... below is how one can do using interfaces ...but a better and more preferred way is described here
Create a Interface with a function as below:
public interface OnChangeListener {
void onChange();
}
then in your service :
private OnChangeListener changeListener;
public void setChangeListener(OnChangeListener changeListener) {
this.changeListener = changeListener;
}
Some where in your service :
changeListener.onChange();
then from your Activity do this :
MyService.getInstance(this)
.setChangeListener(new OnChangeListener() {
#Override
public void onChange() {
// do something here
}
});
Related
In my app I have used IntentService and in it I call a webservice and parse my response. After my parsing is done I want to update the UI.
I saw that there is a way to update UI using broadcast receiver but is there any other way to update UI if I don't want to use broadcast receiver. If so then please share the link.
You could either make a bound Service, or use some lib like EventBus.
From the Android docs:
A bound service is the server in a client-server interface. A bound
service allows components (such as activities) to bind to the service,
send requests, receive responses, and even perform interprocess
communication (IPC). A bound service typically lives only while it
serves another application component and does not run in the
background indefinitely.
If you want to use this approach, you'll have to create a Service that implements the onBind() method. This method will return an IBinder that you'll also have to implement. And that Binder will use an interface, that again, you'll have to create.
Example:
MyService.java
public class MyService extends Service {
// ...
#Override
public IBinder onBind(Intent intent) {
return new MyBinder(this);
}
}
MyBinder.java
public class MyBinder extends Binder {
private MyServiceInterface mService;
public MyBinder(MyServiceInterface s) {
mService = s;
}
public MyServiceInterface getService() {
return mService;
}
}
MyServiceInterface.java
public interface MyServiceInterface {
int someMethod();
boolean otherMethod();
Object yetAnotherMethod();
}
For more details, you can check the docs I've linked above.
Downside of this approach: Regular Service class doesn't run on the background as IntentService do. So you'll also have to implement a way of running things out of the main thread.
And from EventBus page:
simplifies the communication between components
decouples event senders and receivers
performs well with Activities, Fragments, and background threads
avoids complex and error-prone dependencies and life cycle issues
To use EventBus, the best way to start is following the documentation.
So, either of those approaches seems to be a good choice for you, just as using the BroadcastManager (which I don't know why you can't use it).
I have an Android service which collects data from some external hardware. I am making that data available to my Activities using a callback structure shown here.
The data I'm making available is an ArrayList<MyClass>. While reading the Android API docs, it seems that a suggested way is to use broadcast events and Parcelable. I've also seen callbacks used in Android apps, so the question is what's the advantage of refactoring my code to use broadcasts?
Now, in the future, I am looking to allow external apps to access this service (Tasker, for example), so I'm guessing that the callback method I use will be for use in the local app only. So the question is how to do a callback from a Service to an Activity using an AIDL description.
The way to allow a service to callback into an Activity is to have the service aidl interface define a registration function that takes another aidl interface as a parameter.
ServiceAidlInterface.aidl:
package com.test;
import com.test.CallbackAidlInterface;
interface ServiceAidlInterface {
void registerCallback(in CallbackAidlInterface callback);
}
CallbackAidlInterface.aidl:
package com.test;
interface CallbackAidlInterface {
void doCallback();
}
In your activity you need to define the following:
ServiceAidlInterface mService = null;
private CallbackAidlInterface mCallback = new CallbackAidlInterface.Stub() {
#Override
public void doCallback() throws RemoteException {
}
};
So when the activity binds your onServiceConnected() gets called you can do the following:
mService = ServiceAidlInterface.Stub.asInterface(serviceBinder);
service.registerCallback(mCallback)
I am creating a android library, In that I want to find if the OnCreate of the main application is running? . I dont know whether oncreate fire any listener. Anybody knows will oncreate fires any listener. Any ideas? Thanks.
No this callback does not exist.
The way most libraries introduce this callback is by making a base type that they require another application to call back into. Like you could make your library do something like:
public class MyLibBaseApplication extends Application {
public void onCreate() {
super.onCreate();
// my callback stuff.
}
}
and then require all the users of your library either use or extend your Application object as the Application class in their manifest. This way sort of sucks for developers though, although there are a few libs that do this. It blocks you from using multiple libs with this same pattern (Super annoying).
Personally I think just asking for a callback from whoever is using the library is probably much better from a client and dev who would integrate a library.
Just a simple call or static function that would be like:
public class MyApplication extends Application {
public void onCreate() {
super.onCreate();
//register
YourLibrary.onCreateFired(this);
// or istantiate and register
YourLibraryCallback cb = new YourLibraryCallback(this);
cb.onCreateFired();
}
}
This way I think offers the most flexibility.
Inside my Application class, I have a long-running function (e.g. downloading new data). Once the operation finishes, I want to inform several related activities to refresh their UI. What is the best way to achieve this in Android (the equivalent of postNotification in iOS)?
public class MyApplication extends Application{
public void onCreate() {
downloadDataInBackground(); // call operationDone when finished
}
private void operationDone(){
sendMessageToAllRelatedActivities();
}
}
You can use: Broadcast Receivers for this. Please see following link:
http://www.vogella.com/articles/AndroidBroadcastReceiver/article.html
I think you should use the Observer design pattern in order to notify that a certain status is changed
Observer
I am trying to make a live wallpaper on Android. I have one class
public class MyWallpaperService extends WallpaperService {
}
And annother class:
public class SettingsActivity extends Activity {
}
I need to make the SettingsActivity communicate with MyWallpaperService in order to set values on the live wallpaper. I have used aidl before and have tried to apply it to this situation. However it seems like WallpaperService has the following method:
/**
* Implement to return the implementation of the internal accessibility
* service interface. Subclasses should not override.
*/
#Override
public final IBinder onBind(Intent intent) {
return new IWallpaperServiceWrapper(this);
}
Therefore I cannot return my own custom aidl defined binder in the onBind method of my service due to the final declaration on the superclass, WallpaperService's, onBind method. To me this seems like an oversight by the Android platform development team. Does this effectively removes all possible inter process communication abilities from any live wallpaper?
What are my options here? I know I can put the Activity and the Service in the same process and have the Activity set global variables on the Service, but that seems like it could get messy fast and I want to do this right. Is adding a Broadcast Receiver in the Service the right move here?
You could also use BroadCasts to achieve communication between a wallpaper service and some controller activity, source below.
http://developer.samsung.com/android/technical-docs/Effective-communication-between-Service-and-Activity