gpt4 book ai didi

java - Android开发者文档IntentService : Are we defining the same class twice?

转载 作者:行者123 更新时间:2023-12-02 03:55:07 25 4
gpt4 key购买 nike

所以我在 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 的服务

然后他们给出了一个向这个IntentService发送数据的例子

最后他们给出了示例来展示同一个 IntentService 如何返回结果。

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

关于java - Android开发者文档IntentService : Are we defining the same class twice?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35569310/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com