2011-05-19 78 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