2013-06-01 62 views
0

我有一個簡單的服務,但似乎並沒有啓動,因爲既不是我Log已經顯示在logcat的Android服務沒有啓動

public class MyService extends Service { 

    @Override 
    public void onCreate() { 
     // TODO Auto-generated method stub 
     super.onCreate(); 
     Log.d("ID", "Y"); 
    } 

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

    @Override 
    public void onStart(Intent intent, int startId) { 
     // TODO Auto-generated method stub 
     super.onStart(intent, startId); 
     Log.d("S", "Y"); 
    } 

    @Override 
    public void onDestroy() { 
     // TODO Auto-generated method stub 
     super.onDestroy(); 
    } 
} 

我打電話像這樣的服務:

Intent service = new Intent(this, MyService.class); 
     startService(service); 

的mainfest是:

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

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="17" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.example.servicetutorials.MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

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

</manifest> 

什麼我做錯了嗎?

回答

7

A)我不認爲服務有android:icon="@drawable/ic_launcher"
B)如果你開始你的服務onClickListener應該
startService(new Intent(Activity.this, MyService.class));
C)請確保您的logcat是顯示Log.d
d)你的AndroidManifest.xml服務聲明

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

我想它應該工作

覆蓋了非常詳細here

服務完全沒有必要爲一個圖標

+0

它的工作原理,非常感謝。 – user2387331

+0

非常高興提供幫助:D – MDMalik

+0

(B)中是否有拼寫錯誤? 'Activity.this'導致我編譯器錯誤。 – Ponkadoodle