Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to instantiate android service with a constructor?

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);
like image 694
Milan Avatar asked Apr 03 '12 18:04

Milan


People also ask

How do I trigger a service in Android?

You can start a service from an activity or other application component by passing an Intent to startService() or startForegroundService() . The Android system calls the service's onStartCommand() method and passes it the Intent , which specifies which service to start.

What is Android constructor?

Create a Constructor for Class in Android Studio using Shortcuts. Constructors are used to initializing the state of an object. Like methods, constructors also contain a collection of statements(i.e. instructions) that are executed at the time of Object creation.

How do I start a service with Kotlin?

What is the correct way to start a service in kotlin? Is the service registered in the manifest? Make sure that startService returns non-null value, this will indicate that service was started. Keep in mind that startService is deprecated in Android O and will throw exception for targetSdk 26 .

What are the return value of onStartCommand () in Android services?

The documentation says: For backwards compatibility, the default implementation calls onStart(Intent, int) and returns either START_STICKY or START_STICKY_COMPATIBILITY. So returning super. onStartCommand() is equivalent to returning START_STICKY .


2 Answers

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 raw transact().

like image 97
Seva Alekseyev Avatar answered Sep 18 '22 18:09

Seva Alekseyev


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.

like image 20
Robert Estivill Avatar answered Sep 20 '22 18:09

Robert Estivill