我正在嘗試編寫監視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
謝謝。
你的服務所需要的權限添加到您的清單文件? –
java.lang.ClassCastException由於權限? – jn1kk