2012-06-22 109 views
2

我正在嘗試編寫監視sms消息的應用程序。我想從我的主Activity類啓動服務,但由於某種原因服務無法啓動。我認爲在清單文件中如何聲明我的服務可能存在問題,或者我如何從我的活動中調用它。 這是我的行爲守則的一部分:java.lang.RuntimeException:無法實例化服務

public class TablighBlockActivity extends Activity { 
    /** Called when the activity is first created. */ 
    private ToggleButton toggEnable; 

    public void onCreate(Bundle savedInstanceState) { 
     if (loadState("running")==false){startService(new Intent(this, SMSMonitor.class));} 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

,這裏是我的服務類的一部分:

public class SMSMonitor extends BroadcastReceiver { 


@Override 
public void onReceive(Context context, Intent intent) { 

    Intent i = new Intent(context,SMSMonitor.class); 
    i.setClass(context, SMSMonitor.class); 
    context.startService(i); 

這裏是我的清單文件:

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

    <uses-sdk android:minSdkVersion="3" /> 
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> 
    <uses-permission android:name="android.permission.RECEIVE_SMS" /> 
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/> 


    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" > 
     <receiver android:name="SMSMonitor"> 
      <intent-filter> 
       <action android:name="android.intent.action.BOOT_COMPLETED" /> 
       <action android:name="android.provider.Telephony.SMS_RECEIVED"></action> 
      </intent-filter> 

     </receiver> 

     <activity 
      android:name=".TablighBlockActivity" 
      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:enabled="true" android:name=".SMSMonitor" 
            android:process=":smsmonitor" 
            ></service> 
    </application> 

</manifest> 

這裏是錯誤日誌:

06-22 13:46:14.649: W/dalvikvm(1013): threadid=3: thread exiting with uncaught exception (group=0x4000fe70) 
06-22 13:46:14.668: E/AndroidRuntime(1013): Uncaught handler: thread main exiting due to uncaught exception 
06-22 13:46:15.038: E/AndroidRuntime(1013): java.lang.RuntimeException: Unable to instantiate service tabligh.block.SMSMonitor: java.lang.ClassCastException: tabligh.block.SMSMonitor 
06-22 13:46:15.038: E/AndroidRuntime(1013):  at android.app.ActivityThread.handleCreateService(ActivityThread.java:2449) 
06-22 13:46:15.038: E/AndroidRuntime(1013):  at android.app.ActivityThread.access$2800(ActivityThread.java:112) 
06-22 13:46:15.038: E/AndroidRuntime(1013):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1744) 
06-22 13:46:15.038: E/AndroidRuntime(1013):  at android.os.Handler.dispatchMessage(Handler.java:99) 
06-22 13:46:15.038: E/AndroidRuntime(1013):  at android.os.Looper.loop(Looper.java:123) 
06-22 13:46:15.038: E/AndroidRuntime(1013):  at android.app.ActivityThread.main(ActivityThread.java:3948) 
06-22 13:46:15.038: E/AndroidRuntime(1013):  at java.lang.reflect.Method.invokeNative(Native Method) 
06-22 13:46:15.038: E/AndroidRuntime(1013):  at java.lang.reflect.Method.invoke(Method.java:521) 
06-22 13:46:15.038: E/AndroidRuntime(1013):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782) 
06-22 13:46:15.038: E/AndroidRuntime(1013):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540) 
06-22 13:46:15.038: E/AndroidRuntime(1013):  at dalvik.system.NativeStart.main(Native Method) 
06-22 13:46:15.038: E/AndroidRuntime(1013): Caused by: java.lang.ClassCastException: tabligh.block.SMSMonitor 
06-22 13:46:15.038: E/AndroidRuntime(1013):  at android.app.ActivityThread.handleCreateService(ActivityThread.java:2446) 
06-22 13:46:15.038: E/AndroidRuntime(1013):  ... 10 more 

謝謝。

+0

你的服務所需要的權限添加到您的清單文件? –

+0

java.lang.ClassCastException由於權限? – jn1kk

回答

2
public class SMSMonitor extends BroadcastReceiver { 

你SMSMonitor這是一個BroadcastReceiver不是服務

Intent i = new Intent(context,SMSMonitor.class); 
    i.setClass(context, SMSMonitor.class); 
    context.startService(i); 
+1

謝謝這應該是問題的原因。當我從清單文件和start.Service(i)部分刪除服務標籤時,應用程序似乎工作。 – Wise

+0

你爲什麼不喜歡? – Siddharth

1

tabligh.block.SMSMonitor是您的清單文件中的接收器和服務?

我認爲它應該是Service.and將BroadCastReceiver擴展到SMSMonitor。

<receiver android:name="SMSMonitor"> **and** 

    <service android:enabled="true" android:name=".SMSMonitor" 
            android:process=":smsmonitor" 
            ></service> 

Intent i = new Intent(context,SMSMonitor.class); 
//i.setClass(context, SMSMonitor.class);<<Remove this line no need... 
context.startService(i); 
0

我有時不符合標準super.onCreate()setContentView()開始onCreate()遇到奇怪的錯誤。

和其他人一樣,請確保您已將Service添加到Manifest file。這並忘記添加活動後,我創建它們是我最大的錯誤!

編輯:對不起,不知何故錯過了你放在那裏的所有Manifest代碼。

相關問題