Android 开发者文档意图服务:我们是否两次定义同一个类



所以我在Android开发人员中遵循这个例子:http://developer.android.com/training/run-background-service/create-service.html使用 IntentService 创建后台服务。请注意,我们在第一个代码示例中定义了类RSSPullService

public class RSSPullService extends IntentService {
    @Override
    protected void onHandleIntent(Intent workIntent) {
        // Gets data from the incoming Intent
        String dataString = workIntent.getDataString();
        ...
        // Do work here, based on the contents of dataString
        ...
    }
}

在下一页中,报告工作状态:http://developer.android.com/training/run-background-service/report-status.html我很困惑,我们是否再次定义同一个类以获得状态?

public final class Constants {
    ...
    // Defines a custom Intent action
    public static final String BROADCAST_ACTION =
        "com.example.android.threadsample.BROADCAST";
    ...
    // Defines the key for the status "extra" in an Intent
    public static final String EXTENDED_DATA_STATUS =
        "com.example.android.threadsample.STATUS";
    ...
}
public class RSSPullService extends IntentService {
...
    /*
     * Creates a new Intent containing a Uri object
     * BROADCAST_ACTION is a custom Intent action
     */
    Intent localIntent =
            new Intent(Constants.BROADCAST_ACTION)
            // Puts the status into the Intent
            .putExtra(Constants.EXTENDED_DATA_STATUS, status);
    // Broadcasts the Intent to receivers in this app.
    LocalBroadcastManager.getInstance(this).sendBroadcast(localIntent);
...
}

不要混淆,两个类都是一样的

第一个是展示我们如何创建一个扩展IntentService的服务

然后他们举了一个示例将数据发送到此意图服务

最后,他们举了一个例子来展示相同的意图服务如何返回结果。

第二个代码只是他们更改旧意图服务类内容的另一个示例

这是两个独立的例子,不需要定义两次,只需使用一个定义即可。第一个示例中的代码(创建意向服务)刚刚与报告工作示例中的代码合并。

最新更新