Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何用构造函数实例化android服务?_Java_Android_Service_Constructor - Fatal编程技术网

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

Java 如何用构造函数实例化android服务?,java,android,service,constructor,Java,Android,Service,Constructor,我有一个具有以下构造函数的服务: public ShimmerService(Context context, Handler handler) { mHandler = handler; } 我想实例化这个服务类。我有以下代码,但我不确定在哪里传递参数: private ServiceConnection mConnection = new ServiceConnection() { public void onServiceConnected(ComponentName c

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

public ShimmerService(Context context, Handler handler) {
    mHandler = handler;
}
我想实例化这个服务类。我有以下代码,但我不确定在哪里传递参数:

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;
    }
};
我有其他一切设置,包括绑定、启动等。但我在上面的代码中发现了错误:

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
如何解决此问题?我需要在哪里传递参数?以下代码可以工作,但它使用服务类作为类,而不是服务:

mShimmerService = new ShimmerService(this, mHandler);

您不应该显式地构造服务(或活动,或广播接收器)。安卓系统在内部实现了这一点。构建服务的正确方法是通过
startService()
有目的地构建服务;您可以随意为该意图添加额外的参数


编辑:或
bindService()
。然后,您可以选择使用AIDL构建自定义接口,或者使用原始的transact()

服务扩展上下文,这样您就不需要将其作为构造函数中的参数,因为您可以使用相同的实例


如果您有任何其他参数要传递给服务,我建议您将它们作为额外参数添加到startService intent中,并在service.onStartCommand方法中获取它们。

不要将处理程序传递给服务,处理程序不实现Parcelable或Serializable,因此我认为这是不可能的


在服务中创建处理程序,并通过Intent Extras将创建处理程序所需的任何数据传递给服务。

您的服务类需要一个无参数构造函数,否则系统不知道如何实例化它。

而不是将处理程序(或任何对象)传递给服务(顺便说一句,这是不可能的),您可以在活动类中创建并注册BroadcastReceiver。当您需要调用处理程序函数(或其他对象中的任何函数)时,请在注册的接收器上发送广播(sendBroadcast)。您还可以将其他额外参数添加到意图中,并且可以根据参数直接从“活动”处理所有需要的代码

也许,在这种情况下,您的处理程序将被完全删除(取决于您实际需要的内容)。对于广播接收器,我不知道在什么情况下您需要将某些对象传递给服务。另一方面,您做了一些不好的事情,您应该检查应用程序的设计

如果我们想让某些东西传递给服务,我们只能使用Intent中的额外参数启动服务。服务根据内部的这些参数处理状态

其思想是服务可以独立于应用程序的其他部分(例如活动)运行。我们可以在启动服务时使用额外的参数来控制它,或者发送广播以调用外部代码

intent service = new Intent(current context, your service name.class);
`service.putExtra(key,value);`// put your sightly variable type
`service.setAction("StartDownload");`// action that will detect in onStartCommand

 current context.startService(service);
在服务中,在onStartCommand中:

if (intent.getAction().equals("StartDownload")) {
`intent.getExtras().getString(key)`;// string in this sample should be your variable type as you used in your code
//do what you want
    }`

但是,我将如何使用和意图传递处理程序?有任何示例吗?您需要该处理程序做什么?您确定在服务中从头开始构造处理程序不会有帮助吗?服务不是线程;服务与用户代码在同一UI线程上执行。我不知道
bindService
之间是否有区别在这方面,tartService
,但根据文档和附加信息,当在
onBind
中接收到意图时,可能不存在。同样,如何使用意图传递处理程序?