2013-09-30 35 views
23

我有同樣的問題描述here但我不明白什麼是錯的。無法啓動服務意圖:找不到

我的問題是:無法啓動服務意向{行動= .connections.MoodyService} U = 0:未找到

編輯 我從連接在以服務改變了我的包源代碼,遺憾的混亂

我的清單`

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.moody" 
android:installLocation="auto" 
android:versionCode="0" 
android:versionName="0.6.7 alpha" > 

<uses-sdk 
    android:maxSdkVersion="18" 
    android:minSdkVersion="14" 
    android:targetSdkVersion="17" /> 

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

<application 
    android:allowBackup="true" 
    android:allowClearUserData="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name="activities.MainActivity" 
     android:label="@string/app_name" > 
    </activity> 
    <activity 
     android:name="activities.Menu_esq" 
     android:label="@string/title_activity_menu_esq" > 
    </activity> 
    <activity 
     android:name="activities.BaseActivity" 
     android:label="@string/title_activity_base" > 
    </activity> 
    <activity 
     android:name="activities.MainView" 
     android:label="@string/title_activity_main_view" > 
    </activity> 
    <activity 
     android:name="activities.LoginActivity" 
     android:label="@string/app_name" 
     android:noHistory="true" 
     android:windowSoftInputMode="adjustResize|stateVisible" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity 
     android:name="com.example.moody.LeftActivity" 
     android:label="@string/title_activity_left" > 
    </activity> 
    <activity 
     android:name="com.example.moody.RightActivity" 
     android:label="@string/title_activity_right" > 
    </activity> 
    <activity 
     android:name="activities.UserDetailsActivity" 
     android:label="@string/title_activity_user_details" > 
    </activity> 
    <activity 
     android:name="fragments.TopicsPreview" 
     android:label="@string/title_activity_copy_of_topics_preview" > 
    </activity> 
    <activity android:name="activities.Loading" > 
    </activity> 

    <service 
     android:name=".service.MoodyService" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/moody_service" > 
    </service> 
</application> 

服務包和MoodyService是類名

我的服務類

public class MoodyService extends Service { 

public MoodyService() { 
    // TODO Auto-generated constructor stub 
} 

private boolean isRunning = false; 
Object getContent; 

@Override 
public IBinder onBind(Intent intent) { 
    // TODO Auto-generated method stub 

    return null; 
} 

@Override 
public void onCreate() { 
    super.onCreate(); 
} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    super.onStartCommand(intent, flags, startId); 

    // Announcement about starting 
    Toast.makeText(this, "Starting the Demo Service", Toast.LENGTH_SHORT) 
      .show(); 

    // Start a Background thread 
    isRunning = true; 
    Thread backgroundThread = new Thread(new BackgroundThread()); 
    backgroundThread.start(); 

    // We want this service to continue running until it is explicitly 
    // stopped, so return sticky. 
    return START_STICKY; 
} 

@Override 
public void onDestroy() { 
    super.onDestroy(); 

    // Stop the Background thread 
    isRunning = false; 

    // Announcement about stopping 
    Toast.makeText(this, "Stopping the Demo Service", Toast.LENGTH_SHORT) 
      .show(); 
} 

private class BackgroundThread implements Runnable { 
    int counter = 0; 

    public void run() { 
     try { 
      counter = 0; 
      while (isRunning) { 
       System.out.println("" + counter++); 
       new Contents().getAll(getResources(), 
         getApplicationContext()); 
       Thread.currentThread().sleep(5000); 
      } 

      System.out.println("Background Thread is finished........."); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 

在我的主。

Intent start = new Intent(".service.MoodyService"); 
     this.startService(start); 

也試過

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

和完整路徑

<service 
    android:name="com.example.moody.service.MoodyService" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/moody_service" > 
+0

仍然有一些錯誤包「行動= com.example.moody.connections.MoodyService」但你howcome烏爾錯誤有'com.example.moody.service.MoodyService''作爲包名 –

+0

@ ShakeebS我已經改變了一個新的包,忘了更新帖子 – firetrap

+0

多數民衆贊成在今天投票後更新投票up..post本身info否則人們會感到困惑 –

回答

21

解決

我刪除了包名稱的開頭期間在清單和它的工作,換句話說:

這不起作用:

.yourPackage.YourClass 

但是這確實有效:

yourPackage.YourClass 

,並在主:

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

但它違背了什麼是寫在the documentation

的android:命名實現服務的服務子類的名稱。這應該是一個完整的類名(例如, 「com.example.project.RoomService」)。然而,作爲一個速記,如果名稱的 第一個字符是一個時間段(例如,「.RoomService」),它 被附加到在 元素中指定的包名。一旦你發佈你的應用程序,你不應該改變這種 名稱(除非你設置的android:出口=「假」)。

沒有默認。該名稱必須指定。

+1

Comlpetely錯過了,儘管這是你的錯誤。當你在清單的根目錄下有package =「com.example.moody」,並且你編寫了android:name =「。service.MoodyService」時,系統將查找名爲「com.example.moody.service」的服務。 MoodyService」。但是,在q頂部的輸出顯示爲「com.example.moody.connections.MoodyService」,因此我認爲在某個時候會出現複製粘貼錯誤。 – cYrixmorten

+0

是的,這是我的一個嘗試的舊副本。 – firetrap

+1

仍然有一些錯誤包裝'act = com.example.moody.connections.MoodyService',但你有''com.example.moody.service.MoodyService''作爲包名 –

0

你創建的服務一個空的構造試過嗎?

如果沒有,請嘗試。

也不確定如果你可以使用這樣的意圖。你可以嘗試'新的Inten(這個,MoodyService.class)'結構。

+0

是的,我也試過了:Intent intent = new Intent(this,MoodyService.class); this.startService(intent);我更新了我的問題與服務代碼 – firetrap

3

我不知道你爲什麼使用類名稱作爲服務名稱,但爲什麼不使用類名稱來啓動服務?

Intent intent = new Intent(context, YourService.class); 
context.startService(intent); 
+0

我試過沒有不同的相同的錯誤,我做了什麼:「意圖intent =新的意圖(這,MoodyService.class); \t \t \t \t this.startService(intent); 「也變成了一個新的軟件包,但結果相同,只是因爲該軟件包沒有被使用而已。 – firetrap

+0

這是我遇到的確切問題。 AndroidManifest.xml很好,但是我在BroadcastReceiver中傳遞了onReceive()接收到的意圖。 – GrandAdmiral

1

我覺得在清單包名稱服務是錯了,因爲你說你包的名字是connections所以它應該是這樣的

android:name ="connections.MoodyService" 

android:name="com.example.moody.connections.MoodyService" 

來調用服務做

Intent intent = new Intent(this, MoodyService.class); 
    this.startService(intent); 
+0

是的,你是對的,因爲我在我自己的答案中提到,但文檔說:「然而,作爲一個簡寫,如果名稱的第一個字符是一個時期例如,「.RoomService」),它被追加到元素中指定的包名稱「所以我有期限,因爲服務是包 – firetrap

+0

'連接'是包名或'service'是包名 –

+0

我已更新問題,MoodyService.java是在包 - 「連接」,現在它在包 - 「服務」,我的錯誤不是包名,我只忘了更新問題 – firetrap

-3

你嘗試使用Android:名稱您在清單中指定?

Android清單:

<service 
      android:enabled="true" 
      android:name="UploadService" /> 

的通話將某事像這樣:

Intent intent = new Intent("UploadService"); 
+0

這絕不會工作 –

11

的服務也必須包含在清單

<service android:name="com.x.x.serviceclass"></service> 
+1

如果這是什麼在androidTest目錄中?那裏是否需要單獨的Manifest? –

0

我服務在「服務」包中,我的清單服務就像這樣;

 <service 
     android:name="service.TimerService" 
     android:exported="false" > 
    </service> 
1

確保您在清單文件中聲明瞭您的服務。

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

,並嘗試寫getApplicationContext(),而不是「本」關鍵字

startService(new Intent(getApplicationContext(), MoodyService.class)); 
+0

什麼是「啓用」? –

相關問題