2012-11-29 26 views
1

我正在嘗試啓動和關閉服務。我的服務是日誌。如何製作服務Android編程

package com.example.textsmslock; 

import android.app.Service; 
import android.content.Intent; 
import android.os.IBinder; 
import android.util.Log; 
public class Logs extends Service 
{ 
    @Override 
    public IBinder onBind(Intent arg0){ 
     // TODO Auto-generated method stub 
     return null; 
    } 
    @Override 
     public void onStart(Intent intent, int startId) { 
      // TODO Auto-generated method stub 
      super.onStart(intent, startId); 
      System.out.println("LOGS STARTED"); 
      Log.d("TAG", "FirstService started"); 
     } 
     @Override 
     public void onDestroy() { 
      // TODO Auto-generated method stub 
      super.onDestroy(); 
     } 
} 

調用它的活動是ConfirmPin。日誌在函數中被調用。

// imports... 
// public class... 

public void ConfirmingPin() 
{ 
    if(pinCorrect) 
    { 
     startService(new Intent("com.example.textsmslock.Logs")); 
    } 
} 

這裏是我的AndridManifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.textsmslock" 
android:versionCode="1" 
android:versionName="1.0" > 

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

<application 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name=".ConfirmPin" 
     android:label="@string/title_activity_confirm_pin" > 
     <intent-filter> 
      <action android:name="com.example.textsmslock.ConfirmPin" /> 

      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
    </activity> 
    <service android:name=".Logs"/> 
</application> 

的logcat的說:

無法啓動服務意向{行動= com.example.textsmslock.Logs}:不找到

有誰知道我爲什麼無法啓動服務意圖?

+0

@rekire我同意你的大部分編輯,但LogCats應格式化的代碼塊不引用秒。報價塊忽略多行LogCats中的換行符,導致堆棧跟蹤_very_難以閱讀。 – Sam

+0

@Sam通常我也這樣做,但不是一個班輪。 – rekire

+0

@rekire爲什麼使用兩個標準?除了我越來越喜歡技術色的LogCat。 :) – Sam

回答

1

的IntentFilter的在活動的清單看起來不正確,請嘗試:

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

,並啓動服務:

startService(new Intent(this, Logs.class)); 

(您張貼這幾分鐘前有不同logcat的和這個錯誤指向我直接這個。你刪除了這個問題,然後我可以發佈一個答案,雖然...)

+0

是的,我想我已經知道了,但我沒有。你的答案奏效。非常感謝! –

+0

很高興能幫到你!請點擊複選標記以接受此答案作爲解決方案。 – Sam