Intent Service派生调用显式超类构造函数

时间:2012-06-06 11:29:51

标签: android android-intent android-widget

我正在学习android,并遇到了像

这样的例子
public static class A extends IntentService {
    public A() {
        super("AppWidget$A");
    }
}

有人可以告诉我为什么我们必须显式调用超类(IntentService)的构造函数?参数字符串表示什么?

2 个答案:

答案 0 :(得分:3)

它仅用于调试。以下是使用此内容的IntentService源代码的一部分:

public abstract class IntentService extends Service {

    ...
    private String mName;
    ...

    /**
     * Creates an IntentService.  Invoked by your subclass's constructor.
     *
     * @param name Used to name the worker thread, important only for debugging.
     */
    public IntentService(String name) {
        super();
        mName = name;
    }

    ...

    @Override
    public void onCreate() {
        super.onCreate();
        HandlerThread thread = new HandlerThread("IntentService[" + mName + "]");
        thread.start();

        mServiceLooper = thread.getLooper();
        mServiceHandler = new ServiceHandler(mServiceLooper);
    }

    ...
}

答案 1 :(得分:0)

IntentService有一个构造函数,它接受一个字符串参数 " name"。我发现它唯一的用途是为IntentService命名工作线程。该线程名为IntentService [name]。