【发布时间】:2014-02-07 12:24:19
【问题描述】:
我在课堂上有这个IntentService。意图服务未启动。我究竟做错了什么?
public class getListOfUsersService extends IntentService {
public getListOfUsersService() {
super("DownloadListOfUsersIntentService");
System.out.println("intentService started");
}
@Override
protected void onHandleIntent(Intent intent) {
System.out.println("intentService onHandle started");
mService.sendMessage(Protocols.REQUEST_SEND_USER_LIST);
while(true) {
UserHolder UH = new UserHolder();
String usersdata = mService.getString();
System.out.println("userdata: "+ usersdata);
String array[] = usersdata.split("<!!>");
UH.setUserName(array[1]);
UH.setUserPhoneNum(array[2]);
userHolderList.add(UH);
System.out.println("added to usersList");
if(isDone) break;
}
}
}
我在一个方法中启动它:
public void update() {
System.out.println("update");
Intent intent = new Intent(this, getListOfUsersService.class);
startService(intent);
}
我在 Manifest 中注册了它:
<service
android:name=".getListOfUsersService"
android:exported="false"/>
【问题讨论】:
-
您的代码看起来不错,并且没有显示任何错误。您是否尝试从主线程启动它,sys.out 更新是否在 logcat 中显示?
-
是的,尝试从主线程中的方法启动它。 logcat 中没有任何显示。
-
这意味着您的 update() 方法未被调用,这就是您的服务无法运行的原因,请确保您调用了正确的方法或从您调用 update() 的位置发布代码。
-
makovkastar 的回答解决了这个问题。正在调用更新方法。我不知道该服务应该是静态的才能正常工作。没有在文档中阅读。谢谢
标签: android android-intent android-service android-intentservice