IPC with a subclass of WallpaperService - android

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

Related

Update ui from service without broadcast receiver

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).

How to create an interface to interact with service methods from activity

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
}
});

How to get notified about activity's lifecycle

I'm writing a helper class for my activity, which uses an external service. Like in a standard design pattern regarding bound services, I want to bind on activity creation and unbind on activity destruction. However, I want to isolate this logic to my helper class, so that activity would only use an instance of that helper and don't call bind and unbind explicitly.
I can pass the activity to the helper class, but I cannot find any way to schedule a callback on activity's lifecycle's events - there's just no such methods in Activity class. While this most probably means that I cannot achieve what I want to, and also that it's probably not a good idea, I still want to ask the community about this. Is it possible? Is it a good idea? Is it possible to achieve similar results with some other classes (not the Activity)?
I'm new to Android development and I'm seeking for the best practices. Any ideas are appreciated.
Thanks!
EDIT: Basically, I want to be notified on activity creation and destruction. I want to be able to schedule a callback on onCreate and onDestroy methods, but from outside of the Activity. Those methods are protected and therefore inaccessible from other classes.
You can use the Application.ActivityLifecycleCallbacks class. Keep in mind that the class was introduced in API level 14. For lower versions you could make hook methods in your library and require that the target Activity will call the appropriate hook method from its corresponding lifecycle method. Of course, this would be a very fragile implementation.
Lifecycle methods are the means to implement behaviour which will be executed when DalvikVM decides to do something with Activity (pause/resume/create/destroy), not to invoke that behaviour artificially.
If you want to externalise logic in helper/controller of some sort and be able to use service connection do initialization within ServiceConnection handler.
private ServiceConnection mConnection = new ServiceConnection() {
#Override
public void onServiceConnected(ComponentName className,IBinder service) {
...init helper here...
}
#Override
public void onServiceDisconnected(ComponentName arg0) {
...shutdown helper here...
}
};
then handle connections as usual.

Is there any way to bind to a live wallpaper service?

I have a live wallpaper which has some resources loaded and set up inside my custom WallpaperService class. Something like this:
class MyLiveWallpaperService extends WallpaperService {
MyResources sharedResources;
public onCreate() {
// set up some resources
}
public MyResources getSharedResources() {
return sharedResources;
}
}
And I also have a preferences activity for this live wallpaper that is properly set in res/xml/wallpaper.xml. What I want is to somehow provide this PreferencesActivity an access to these shared resources. I know I could make them public static fields but I prefer a cleaner solution - if only there was a way to bind to wallpaper service (so I could access a MyLiveWallpaperService instance) that would be rly cool.
In the end activity and service are in the same process in my case, so something described as accessing local service in dev guide should work, BUT WallpaperService docs discourage overriding onBind() method which makes this idea impossible to come true.
Is there any other way? :)
i also wanted to bind to a live wallpaper service but is not possible. If you want to pass data to a wallpaper service maybe you can use SharedPreferences as a alternative to use static field

Different Instance of Applicationcontext in Broadcastreceiver

I want to access a "global" variable in my MyApp(extends Application) from a broadcastreceiver (registered in the manifest) and e.g. multiple activities. Now I seem to have different instances of my MyApp: one for the BCR and one for the activities. Could sb help me with my problem?
thanks alot
Joerg
What I get from this is that you are trying to create a method to having a single Context object. First off, to do this you would need a Singleton Pattern of MyApp to create your "global" variable. However I would advice against this for these reasons:
Different application components by default have different contexts (base, application).
A BroadcastReceiver defined in the manifest is invoked by the OS, not by your application.
Using a Singleton Pattern for a context object will lead to some very nasty dependencies.
You are going against the design and beauty of the Android Framework.
I would suspect the reason you are doing this is so your MyApp class can start different activities. This makes sense, but... you can get a Context Object from almost anywhere. Many things in Android extend the ContextWrapper class (think Java Objects with the Object class). So there is really no reason to ever have a "global" instance of this. In fact your BroadcastReceiver's onReceive() method accepts a context parameter. You can use this to start activities and what not.
If this is not why you are wanting the MyApp singleton class - and there are justifiable reasons for needing it, I would look at the implementation designed by Bill Pugh as it is the safest in Java taking into account thread synchronization and locking.
Hope this helps. Remember, don't fight the SDK, let it work for you!
I had a similar problem, I was able to access an object in the activity using this pattern:
public class MyReceiver extends android.content.BroadcastReceiver {
private Object _object;
public MyReceiver(Someobject) {
_object = the object;
}
#Override
public void onReceive(Context context, Intent intent) {
Do something to the object.
}
}
Then call MyReceiver(theobject) instead of new BroadcastReceiver().

Categories

Resources