1

我创建了无法启动服务的简单 IntentService 框架。我已经添加了调用、主代码块和清单条目,但我仍然没有得到任何日志输出。我在这里检查了其他类似的答案,但都指向我已将条目添加到的清单:

显现:

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" 
    android:name=".ApplicationGlobal">

    <service android:name=".MyIntentServiceImpl" />

启动服务:

Intent intent = new Intent(ThisActivity.this, MyIntentServiceImpl.class);
startService(intent);

服务代码:

public class MyIntentServiceImpl extends IntentService implements MyIntentService {

private boolean result;
private Long pollingCount; 

public MyIntentServiceImpl (){
    super("MyIntentServiceImpl ");
    Log.d(TAG, "Creating Polling Service");
    this.result = false;
    this.pollingCount = 0L;
}

@Override
protected void onHandleIntent(Intent arg0) {
    Log.d(TAG, "Polling Service Started");

    while(!result && (pollingCount <= MAX_POLLING_VALUE)){
        pollingCount++;
        Log.d(TAG, "Polling for Result Request (" + pollingCount.toString() + ")");
    }

    Log.d(TAG, "Polling Service Finished");
4

1 回答 1

0

检查 LogCat。Android是否告诉您它找不到该服务?这意味着您的 Intent 编码不正确。

您确定要调用 startService 吗?您只提供了两行调用它的内容,并且您根本没有在清单中显示它。

我开始调试的方法是说服自己 startService 正在被调用。到目前为止,最好的方法是使用 Eclipse 中的调试器对其进行调试,您也可以注释掉“startService”并将其替换为日志语句。如果这未能显示在您的日志中,那么您就知道没有达到 startService 的位置。

于 2012-10-29T23:44:52.487 回答