I have a service with an following constructor:
public ShimmerService(Context context, Handler handler) {
mHandler = handler;
}
I want to instantiate this service class. I have following code but, I am not sure where to pass the paramater:
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder binder) {
mShimmerService = ((ShimmerService.ShimmerConfigureBinder) binder)
.getService();
Toast.makeText(ConfigureShimmer.this,
"Shimmer service has succesfully started.",
Toast.LENGTH_SHORT).show();
}
public void onServiceDisconnected(ComponentName className) {
mShimmerService = null;
}
};
I have everything else setup including binding, on start and so on. But I get error in above code:
04-03 19:06:10.285: E/AndroidRuntime(16837): java.lang.RuntimeException: Unable to instantiate service com.milanix.androidecg.services.ShimmerService: java.lang.InstantiationException: can't instantiate class com.milanix.androidecg.services.ShimmerService; no empty constructor
How do I fix this problem? Where will i need to pass parameter? Following code works but, it rather uses service class as a class, rather than service:
mShimmerService = new ShimmerService(this, mHandler);
Service extends Context, so you don't really need it as a parameter in your constructor, since you can use that same instance.
If you have any other parameters that you would like to pass in to the service, i would recommend adding them to the startService intent as extras and getting them in the service.onStartCommand method.
Dont pass the Handler to the Service, Handler doesnt implement Parcelable, or Serializable, so I dont think thats possible.
Create the Handler in the Service, and pass any data you need to create the Handler via Intent Extras to the Service.
Instead of passing Handler (or whatever object) to the service (what is not possible by the way), you create and register BroadcastReceiver in your Activity class. When you need to invoke Handler functions (or whatever functions in another object) then send broadcast on registered receiver (sendBroadcast). You can also put additional extra parameters to the intent and you can handle all needed code directly from Activity according parameters.
Maybe, in this case your handler will be completely removed (Depends what you actually need.). With broadcast receivers, I don't know to imagine situation when you would need to pass some object to the Service. On the other hand, you doing something not good and you should review the design of the application.
If we want something pass to the Service, we can only start Service with extra parameters in Intent. Service handles state according this parameters inside.
The idea is that Service may run independently from other parts of applications e.g Activities. We may control it with extra parameters when we start Service or with sending broadcasts for invoking outside code.
You should not construct services (or activities, or broadcast receivers) explicitly. The Android system does that internally. The proper way to construct a service is via
startService()
with an intent; feel free to add extra parameters to that intent.EDIT: or
bindService()
. Then you have options - either build a custom interface with AIDL, or use rawtransact()
.You need to have a no-argument constructor for your Service class, otherwise the systems doesn't know how to instantiate it.
in service, in onStartCommand: