2011-05-19 136 views
5

我读过大概100个问题和对这个问题的答案,但我似乎无法得到这个工作。我正尝试从Activity开始Service。我的清单文件似乎确定,我开始Service的方式似乎也是正确的。下面的错误是没有显示在logcat中:无法启动服务意图

ActivityManager(1296): Unable to start service Intent 
{ cmp=com.exercise.AndroidClient/com.client.Communication }: not found 

我试图启动该服务由我Activity调用此:

startService(new Intent(getApplicationContext(), Communication.class)); 

Service如下:

public class Communication extends Service { 
    public Communication() { 
     super(); 
    } 
    @Override 
    public void onCreate() { 
     super.onCreate(); 
     Log.i("Service", "Created service"); 
    } 
    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     Log.i("Service", "onStartCommand called"); 
     return START_STICKY; 
    } 
    @Override 
    public IBinder onBind(Intent arg0) { 
     return null; 
    } 
} 

在我的清单文件中的条目是:

<?xml version="1.0" encoding="utf-8" ?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.exercise.AndroidClient" android:versionCode="1" 
    android:versionName="1.0"> 

    <application android:icon="@drawable/sms" android:label="@string/app_name" > 

     <activity> ... </activity> 

     <service android:enabled="true" android:name=".Communication" /> 

    </application> 
</manifest> 

任何建议是非常appriciated。

+0

这个问题是固定的,通过改变 'startService(新的意向书(getApplicationContext(),Communication.class));' 到 'startService(新的意图(getApplicationContext(),com.client.Communication.class));' 并且也在清单文件中进行相同的更改。我想因为所有的文件都在同一个软件包中,所以这可能会好起来......不要猜测。 – Shaun 2011-05-19 02:29:23

回答

4

Communication类在哪里?

在您的清单声明与android:name=".Communication"服务,这意味着你的服务类应位于com.exercise.AndroidClient.Communication

检查包装是否正确。请注意,“。” (点)指的是你的包的根(即在清单中声明的​​包)。因此,举例来说,如果你的包是com.exercise.AndroidClient和服务类是com.exercise.AndroidClient.services.Communication下你需要声明这样的服务:

<service android:enabled="true" android:name=".services.Communication" /> 

或指定完整包:

<service android:enabled="true" android:name="com.exercise.AndroidClient.services.Communication" /> 
+0

谢谢!我设法弄清楚了一下。但是,我无法在发布后的8小时内回答我自己的问题。 – Shaun 2011-05-19 03:53:52

0

我有同样的错误,原因是该服务未在AndroidManifest.xml中定义。

+0

这里同样的问题 – 2012-07-01 14:02:58

0

另外,如果你的服务发生在你是指的一个库项目存在,请检查您的project.properties文件。

你要加入这一行:

manifestmerger.enabled=true 
相关问题