2012-06-07 157 views
0

這是我的代碼:PhoneStateListener - 應用程序意外終止

private PhoneStateListener mPhoneStateListener = new PhoneStateListener() { 
    @Override 
    public void onCallStateChanged(int state, String incomingNumber) { 
     super.onCallStateChanged(state, incomingNumber); 
     switch (state) { 
     case TelephonyManager.CALL_STATE_OFFHOOK: 
      break; 
     case TelephonyManager.CALL_STATE_RINGING: 
      break; 
     case TelephonyManager.CALL_STATE_IDLE: 
      break; 
     } 
    } 
}; 

OnCreate事件:

mTelephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE); 
     mTelephonyManager.listen(mPhoneStateListener, 
       PhoneStateListener.LISTEN_CALL_STATE); 

的AndroidManifest.xml:

<uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.READ_PHONE_STATE" /> 
     <receiver android:name=".ServiceReceiver" > 
      <intent-filter> 
       <action android:name="android.intent.action.PHONE_STATE" /> 
      </intent-filter> 
     </receiver> 

編輯

錯誤日誌:

06-07 20:00:35.203: W/dalvikvm(1807): threadid=1: thread exiting with uncaught exception (group=0x409c01f8) 
06-07 20:00:36.253: E/AndroidRuntime(1807): FATAL EXCEPTION: main 
06-07 20:00:36.253: E/AndroidRuntime(1807): java.lang.RuntimeException: Unable to instantiate receiver company.group.ServiceReceiver: java.lang.ClassNotFoundException: company.group.ServiceReceiver 
06-07 20:00:36.253: E/AndroidRuntime(1807):  at android.app.ActivityThread.handleReceiver(ActivityThread.java:2100) 
06-07 20:00:36.253: E/AndroidRuntime(1807):  at android.app.ActivityThread.access$1500(ActivityThread.java:123) 
06-07 20:00:36.253: E/AndroidRuntime(1807):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1197) 
06-07 20:00:36.253: E/AndroidRuntime(1807):  at android.os.Handler.dispatchMessage(Handler.java:99) 
06-07 20:00:36.253: E/AndroidRuntime(1807):  at android.os.Looper.loop(Looper.java:137) 
06-07 20:00:36.253: E/AndroidRuntime(1807):  at android.app.ActivityThread.main(ActivityThread.java:4424) 
06-07 20:00:36.253: E/AndroidRuntime(1807):  at java.lang.reflect.Method.invokeNative(Native Method) 
06-07 20:00:36.253: E/AndroidRuntime(1807):  at java.lang.reflect.Method.invoke(Method.java:511) 
06-07 20:00:36.253: E/AndroidRuntime(1807):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
06-07 20:00:36.253: E/AndroidRuntime(1807):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
06-07 20:00:36.253: E/AndroidRuntime(1807):  at dalvik.system.NativeStart.main(Native Method) 
06-07 20:00:36.253: E/AndroidRuntime(1807): Caused by: java.lang.ClassNotFoundException: company.group.ServiceReceiver 
06-07 20:00:36.253: E/AndroidRuntime(1807):  at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61) 
06-07 20:00:36.253: E/AndroidRuntime(1807):  at java.lang.ClassLoader.loadClass(ClassLoader.java:501) 
06-07 20:00:36.253: E/AndroidRuntime(1807):  at java.lang.ClassLoader.loadClass(ClassLoader.java:461) 
06-07 20:00:36.253: E/AndroidRuntime(1807):  at android.app.ActivityThread.handleReceiver(ActivityThread.java:2095) 

問題:當我收到一個電話,我的應用程序將意外停止。這段代碼有什麼問題?

+0

logcat中的任何錯誤?如果是的話,然後plz發佈 –

+0

我的文章被編輯。謝謝 – Kermia

回答

0

使用服務或活動?

嘗試在onCreate()內部執行這兩個操作(爲PhoneStateListener和TelephonyManager創建內部類),如果您在Activity中執行此操作。