如何使用构造函数实例化android服务?

Mil*_*lan 12 java service android constructor

我有一个服务与以下构造函数:

public ShimmerService(Context context, Handler handler) {
    mHandler = handler;
}
Run Code Online (Sandbox Code Playgroud)

我想实例化这个服务类.我有以下代码但是,我不知道在哪里通过参数:

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;
    }
};
Run Code Online (Sandbox Code Playgroud)

我有其他一切设置,包括绑定,开始等等.但我在上面的代码中得到错误:

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
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个问题?我需要在哪里传递参数?以下代码可以工作,但它更像是使用服务类作为类,而不是服务:

mShimmerService = new ShimmerService(this, mHandler);
Run Code Online (Sandbox Code Playgroud)

Sev*_*yev 16

您不应该明确地构建服务(或活动或广播接收器).Android系统在内部完成.构建服务的正确方法是通过startService()意图; 随意为该意图添加额外的参数.